-
-
Notifications
You must be signed in to change notification settings - Fork 372
Description
Description
As discussed in #3559, #5393 and #4015, there is a edge case where accessing the [SentryHub scope] before starting the SDK or after closing the SDK (when _scope is nil) will create a new scope and call [SentryCrashWrapper enrichScope:scope].
In enrichScope we are accessing the current app windows via SentryDependencyContainer.sharedInstance.application.windows, which correctly dispatches the access to the current application windows to the main thread as expected, because UIKit object must only be accessed from main thread.
sentry-cocoa/Sources/Sentry/SentryCrashWrapper.m
Lines 187 to 194 in 2e790c6
| NSArray<UIWindow *> *appWindows = SentryDependencyContainer.sharedInstance.application.windows; | |
| if ([appWindows count] > 0) { | |
| UIScreen *appScreen = appWindows.firstObject.screen; | |
| if (appScreen != nil) { | |
| [deviceData setValue:@(appScreen.bounds.size.height) forKey:@"screen_height_pixels"]; | |
| [deviceData setValue:@(appScreen.bounds.size.width) forKey:@"screen_width_pixels"]; | |
| } | |
| } |
sentry-cocoa/Sources/Sentry/SentryUIApplication.m
Lines 74 to 79 in 2e790c6
| - (NSArray<UIWindow *> *)windows | |
| { | |
| __block NSArray<UIWindow *> *windows = nil; | |
| [_dispatchQueueWrapper | |
| dispatchSyncOnMainQueue:^{ | |
| UIApplication *app = [self sharedApplication]; |
But the references to the windows are then transferred to the calling thread where enrichScope is executed, therefore the references are accessed from a background thread when calling appWindows.firstObject.screen and appScreen.bounds.size.height.
We need to move the access to the screen size to the main thread in SentryUIApplication, e.g. [SentryDependencyContainer.sharedInstance.application getActiveWindowScreenSize] and return a non-reference return value, i.e. a CGSize, instead.