[SwiftUI][Crash] Weird crash with FocusBrigde.hasHostingController.getter

This is a bug I have been trying to identify and fix for more than 3 weeks. This bug mostly happens in production.

The stack trace doesn't help that much, because it looks like the app is crashing due to some SwiftUI internal methods.

I tried everything in my power to debug and find this bug, still nothing. Due to the lack of 100% reproducibility, it made me think that this bug is either memory-pressure related or Swift concurrency related.

When does it happen:

  • This bug happens when presenting a SwiftUI modal (UIViewControllerRepresentable)[A]
  • which in terms presents a UIHostingController modal[B]
  • which then presents a modally a SwiftUI sheet [C]

The bug happens somewhere around staying in [C] or after dismissing it.

Context: The app lifecycle is SwiftUI. The main SwiftUI screen in the app body is a UIViewControllerRepresentable containing a UISplitViewController.

Here is a screenshot from the crash log in Xcode.

Anyone facing a similar issue?