Skip to content

Dispatch access of UIWindow screen to main thread in SentryCrashWrapper #5424

@philprime

Description

@philprime

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.

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"];
}
}

- (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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions