getsentry / sentry-cocoa

The official Sentry SDK for iOS, tvOS, macOS, watchOS.
https://sentry.io/for/cocoa/
MIT License
779 stars 311 forks source link

Calling `addBreadcrumb` triggers `Main Thread Checker` warning #4015

Open ncltg opened 1 month ago

ncltg commented 1 month ago

Platform

iOS

Environment

Develop

Installed

Swift Package Manager

Version

8.26.0

Xcode Version

15.2

Did it work on previous versions?

not sure

Steps to Reproduce

Call SentrySDK.addBreadcrumb() from a background thread

Expected Result

No main thread checker issue is found.

Actual Result

Result

Xcode shows a Main Thread checker warning. The SDK is calling UIKit methods that require Main Thread execution on a background thread.

Logs and screenshots

Main Thread Checker: UI API called on a background thread: -[UIWindow screen]
PID: 45981, TID: 2549765, Thread name: (none), Queue name: com.apple.root.user-initiated-qos.cooperative, QoS: 25
Screenshot 2024-05-24 at 13 03 51 Screenshot 2024-05-24 at 13 03 42

Question

I checked the iOS documentation and it does not mention that breadcrumb should be added from the main thread. Is it an issue from Sentry SDK that needs to be addressed or does it actually recommended to call that method from the main thread ?

Are you willing to submit a PR?

No response

brustolin commented 1 month ago

Hello @ncltg, thanks for reaching out. Add breadcrumb is not meant to be used only in the main thread.

I believe this SentryLogRepository is an auxiliar class in your project correct? Could it be that this function its being called before starting SentrySDK? That's when we use enrichScope.

Although this is something we should improve, calling SentrySDK before starting it, creates a new SentryHub in any thread and this causes the problem.

ncltg commented 1 month ago

Hey @brustolin

I believe this SentryLogRepository is an auxiliar class in your project correct?

Yes 👍

Based on the information you provided it's very likely that some of our launch tasks produce calls to addBreadcrumb before we start SentrySDK ourselves.

Thanks a lot for the clarification 🙏

swhitty commented 1 month ago

I also see main thread checker issues within enrichScrope: 8.26.0 but only after calling SentrySDK.close().

crash
brustolin commented 1 month ago

Hello @swhitty, thanks for reaching out. By calling SentrySDK.close() you're cleaning up Sentry hub and its scope, by calling any function of the SDK a new one will be created.

But we need to improve this to not trigger a thread checker.

philipphofmann commented 1 month ago

We can change this to initialize a new SentryNoOpHub when there is none instead of creating a normal SentryHub. It's best to do this in the next major to avoid unwanted side effect.