sentry-cocoa: Call must be made on main thread
Environment
How do you use Sentry? Sentry SaaS (sentry.io)
Which SDK and version? sentry-cocoa 7.4.3
Steps to Reproduce
- iOS 15
- Sentry 7.4.3
- As a workaround we forked and replaced
self.queuewithdispatch_get_main_queue() - In the SDK initialisation:
SentrySDK.start { $0.dsn = sentryDSN }
Expected Result
I now you just release the update 7.4.3 but instead of creating a new serial queue that creates the View Controllers I think it should be done on the main thread.
Actual Result
The UIViewControllers are created on thread sentry-default.
Stack trace:


Update: The fork at my GitHub account has been deleted.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (6 by maintainers)
Commits related to this issue
- fix: Crash for Call Should be on Main Thread The swizzling of subclasses of UIViewController stored references to classes on a background thread, which led to calling the initialize method. Some UIVi... — committed to getsentry/sentry-cocoa by philipphofmann 3 years ago
- fix: Crash for Call Should be on Main Thread (#1371) The swizzling of subclasses of UIViewController stored references to classes on a background thread, which led to calling the initialize method.... — committed to getsentry/sentry-cocoa by philipphofmann 3 years ago
Thank you very much @philipphofmann @brustolin for your work and also thank you @dyegos for the input that actually was the key to solving it, this have being a real save for our team. It is now working corretly with SPM in our project too with commit
953414914f7546894ad90b582f4e926db45a362e.Update: I also tested with version
7.4.4and is working correctly.Thanks for the good news, @JCTec. I’m happy it works now.
I just tried your sample project with the fix, and it solves the problem @dyegos.
I’m waiting for a review now and will do a release soon. I guess that should also fix the other crashes. Sorry about the inconvenience.
Thanks @dyegos for the sample project with AppCenter. The reason we crash is because the
MSACAlertController#initializecreates a newUIViewController. Our code callsMSACAlertController#initializewhen we addMSACAlertControllerto anNSMutableArrayhere https://github.com/getsentry/sentry-cocoa/blob/dfce484724452f4ecf9f5b3307f802d818b17d92/Sources/Sentry/SentrySubClassFinder.m#L29This means we call
initializefor everyUIViewControllerwe add to our list. As we can’t control what the initializers are doing, we should make sure to either call them on the main thread or try a different way that avoids the call toinitialize.We are working on a fix right now.
@philipphofmann update. I ran the app and it’s not crashing anymore in our project as well. 👍🏽
@JCTec, what you do in your fork is a horrible idea. With the change in your fork, every call to
SentryDispatchQueueWrapper#dispatchAsyncWithBlockwill run on the main thread, including sending envelopes containing events, transactions, release health data. As pointed out by @brustolin, the code doesn’t create any UIViewControllers. Instead, the code swizzles the UIViewControllers.Hey @JCTec, did you encounter any error using the SDK?
This code in particular does not create any UIViewController, it does not do anything UI related to be necessary to run in the main thread.
But maybe I’m missing something here. Any reason why do you think this should run in the main thread?