sentry-cocoa icon indicating copy to clipboard operation
sentry-cocoa copied to clipboard

Dispatch access of UIWindow screen to main thread in SentryCrashWrapper

Open philprime opened this issue 6 months ago • 1 comments

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.

https://github.com/getsentry/sentry-cocoa/blob/2e790c65299f50859e726bdbaa93dafe178bf2fb/Sources/Sentry/SentryCrashWrapper.m#L187-L194 https://github.com/getsentry/sentry-cocoa/blob/2e790c65299f50859e726bdbaa93dafe178bf2fb/Sources/Sentry/SentryUIApplication.m#L74-L79

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.

philprime avatar Jun 17 '25 10:06 philprime

From Cocoa Sync 08/16:

We need to investigate if the windows are not used anywhere else and instead just return the data that is requested from the windows so all access to UIKit objects is done on the main thread

philprime avatar Jun 18 '25 13:06 philprime