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

  1. iOS 15
  2. Sentry 7.4.3
  3. As a workaround we forked and replaced self.queue with dispatch_get_main_queue()
  4. 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: Screen Shot 2021-10-07 at 3 44 42 p m

Screen Shot 2021-10-07 at 3 46 03 p m

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

Most upvoted comments

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.4 and 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.

pod 'Sentry',  :git => 'git@github.com:getsentry/sentry-cocoa.git', :commit => '953414914f7546894ad90b582f4e926db45a362e' 

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#initialize creates a new UIViewController. Our code calls MSACAlertController#initialize when we add MSACAlertController to an NSMutableArray here https://github.com/getsentry/sentry-cocoa/blob/dfce484724452f4ecf9f5b3307f802d818b17d92/Sources/Sentry/SentrySubClassFinder.m#L29

This means we call initialize for every UIViewController we 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 to initialize.

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#dispatchAsyncWithBlock will 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?