Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crash due to a background call to -[UIApplication applicationState] in -[SentryUIApplication init] #3836

Open
diogot opened this issue Apr 5, 2024 · 15 comments · Fixed by #3855

Comments

@diogot
Copy link

diogot commented Apr 5, 2024

Platform

tvOS

Environment

Other

Installed

Swift Package Manager

Version

8.23.0

Did it work on previous versions?

It works up to version 8.21.0

Steps to Reproduce

Call + (SentryId *)captureError:(NSError *)error withScopeBlock:(void (^)(SentryScope *_Nonnull))block in a background thread crashes the app due to Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState].

This is happening in our unit tests, we didn't released to production.
We are using Xcode 15.3.

Expected Result

App did not crash.

Actual Result

App crash with this stack trace:

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
PID: 16779, TID: 3695907, Thread name: (none), Queue name: tv.standard.watch-NetworkService, QoS: 25
Backtrace:
4 Services 0x000000010842a4b4 -[SentryUIApplication init] + 244
5 Services 0x000000010841eacc -[SentryDependencyContainer application] + 68
6 Services 0x00000001083d32b4 -[SentryCrashWrapper enrichScope:] + 1096
7 Services 0x00000001083eafc8 -[SentryHub initWithClient:andScope:] + 736
8 Services 0x00000001084107e0 +[SentrySDK currentHub] + 60
9 Services 0x000000010841183c +[SentrySDK captureError:withScopeBlock:] + 80

Are you willing to submit a PR?

No response

@philipphofmann
Copy link
Member

philipphofmann commented Apr 8, 2024

Thanks for reporting this, @diogot. This should be an easy fix.

There is no guarantee that SentryUIApplication init is called on the main thread. It usually gets called on the main thread like this:

-[SentryUIApplication init] at SentryUIApplication.m:31
-[SentryDependencyContainer application] at SentryDependencyContainer.m:261
-[SentryCrashWrapper enrichScope:] at SentryCrashWrapper.m:187
-[SentryHub initWithClient:andScope:] at SentryHub.m:80
+[SentrySDK startWithOptions:] at SentrySDK.m:209
+[SentrySDK startWithConfigureOptions:] at SentrySDK.m:249

But if for example the SDK gets started from a background thread or if somebody interacts with the hub without starting the SDK on a background thread, SentryUIApplication init might get called on a background thread.

@brustolin, could you please have a look?

@mlch911
Copy link
Contributor

mlch911 commented Apr 15, 2024

I'm experiencing this crash as well.
When a network request is triggered in the background, the monitoring of the sentry network request can trigger this crash.
I'm using sentry 8.23.0.

-[MaxWindow initWithFrame:] (MaxWindow.m:24)
-[AppDelegate window] (AppDelegate.m:184)
-[SentryUIApplication windows] (SentryUIApplication.m:84)
-[SentryCrashWrapper enrichScope:] (SentryCrashWrapper.m:187)
-[SentryHub initWithClient:andScope:] (SentryHub.m:80)
+[SentrySDK currentHub] (SentrySDK.m:76)
-[SentryRequestOperation initWithSession:request:completionHandler:]_block_invoke (SentryRequestOperation.m:37)

@kahest
Copy link
Member

kahest commented Apr 15, 2024

Thanks for the additional info @mlch911 we'll have a fix soon

@mlch911
Copy link
Contributor

mlch911 commented Apr 15, 2024

Thanks for the additional info @mlch911 we'll have a fix soon

I have commited a PR to fix this.

@brustolin brustolin linked a pull request Apr 17, 2024 that will close this issue
7 tasks
brustolin added a commit that referenced this issue Apr 18, 2024
Fix crash when call SentryUIApplication in background thread. #3836


Co-authored-by: Dhiogo Brustolin <[email protected]>
armcknight pushed a commit that referenced this issue Apr 18, 2024
Fix crash when call SentryUIApplication in background thread. #3836


Co-authored-by: Dhiogo Brustolin <[email protected]>
@diogot
Copy link
Author

diogot commented Apr 22, 2024

It seems that this was fixed in the main branch, it's great!
Do you know when we'll have a new release?

@brustolin
Copy link
Contributor

Hello @diogot, most likely until Thursday.

@diogot
Copy link
Author

diogot commented Apr 23, 2024

Awesome, thanks!

@diogot
Copy link
Author

diogot commented Apr 29, 2024

Hey, I tested with release 8.25.0 and I'm still experiencing crashes on my tests, here is the stack trace:

Thread 6 Crashed:
0 libsystem_kernel.dylib 0x1043913b0 __pthread_kill + 8
1 libsystem_pthread.dylib 0x103057124 pthread_kill + 256
2 libsystem_c.dylib 0x1802e35f4 abort + 100
3 libMainThreadChecker.dylib 0x103f9da44 ASSERT_API_MUST_BE_CALLED_FROM_MAIN_THREAD_FAILED + 872
4 libMainThreadChecker.dylib 0x103f9e5bc checker_c + 460
5 libMainThreadChecker.dylib 0x103f9d5ac trampoline_c + 72
6 libMainThreadChecker.dylib 0x103f5d37c handler_start + 52
7 Services 0x10610e5d4 -[SentryCrashWrapper enrichScope:] + 1120
8 Services 0x106126380 -[SentryHub initWithClient:andScope:] + 732
9 Services 0x10614eb18 +[SentrySDK currentHub] + 60
10 Services 0x10614fb74 +[SentrySDK captureError:withScopeBlock:] + 80

@diogot
Copy link
Author

diogot commented Apr 29, 2024

Should I open a new issues or this can be reopened?

@philipphofmann
Copy link
Member

I reopened the issue @diogot. Thanks for reaching out to us again.

dKasabwala pushed a commit to dKasabwala/sentry-cocoa that referenced this issue May 6, 2024
…try#3855)

Fix crash when call SentryUIApplication in background thread. getsentry#3836


Co-authored-by: Dhiogo Brustolin <[email protected]>
@diogot
Copy link
Author

diogot commented May 14, 2024

Tested on 8.26.0, the crash keeps happening:

Thread 5 Crashed:
0 libsystem_kernel.dylib 0x10654d3b0 __pthread_kill + 8
1 libsystem_pthread.dylib 0x1066f3124 pthread_kill + 256
2 libsystem_c.dylib 0x1802e35f4 abort + 100
3 libMainThreadChecker.dylib 0x10627da44 ASSERT_API_MUST_BE_CALLED_FROM_MAIN_THREAD_FAILED + 872
4 libMainThreadChecker.dylib 0x10627e5bc checker_c + 460
5 libMainThreadChecker.dylib 0x10627d5ac trampoline_c + 72
6 libMainThreadChecker.dylib 0x10623d37c handler_start + 52
7 Services 0x1086b2f34 -[SentryCrashWrapper enrichScope:] + 1120
8 Services 0x1086cb038 -[SentryHub initWithClient:andScope:] + 732
9 Services 0x1086f3b18 +[SentrySDK currentHub] + 68
10 Services 0x1086f4bbc +[SentrySDK captureError:withScopeBlock:] + 80

threema-matteo pushed a commit to threema-ch/sentry-cocoa that referenced this issue May 21, 2024
…try#3855)

Fix crash when call SentryUIApplication in background thread. getsentry#3836


Co-authored-by: Dhiogo Brustolin <[email protected]>
@diogot
Copy link
Author

diogot commented Jun 18, 2024

I looked at -[SentryCrashWrapper enrichScope:], I suspect that this is problem:

[osData setValue:[UIDevice currentDevice].systemVersion forKey:@"version"];

UIDevice is @MainActor, so calling this in a background thread will trap.

@brustolin
Copy link
Contributor

This can happen (and will be fixed) if you're trying to use SentrySDK from a background thread before starting it.

Can you confirm this scenario @diogot?

@diogot
Copy link
Author

diogot commented Jun 18, 2024

We're observing this crash in our CI, now that I looked at the code, I found a workaround.
I'm not running start in the simulator, but I'm calling capture.
I'll change our code to do not call any method from the SDK while in the simulator.
Should I leave this ticket open?

@brustolin
Copy link
Contributor

Should I leave this ticket open?

Yes. We still need to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Status: No status
Status: Needs Discussion
Development

Successfully merging a pull request may close this issue.

5 participants