From 6b808289780cfcfeef0097a78e85b08fc4671d2b Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Tue, 25 Nov 2025 16:39:04 -0300 Subject: [PATCH 1/7] fix: UIWindow being possibly accesed from a background thread in SentryCrashWrapper --- .../SentryDependencyContainerSwiftHelper.m | 5 +++++ .../SentryDependencyContainerSwiftHelper.h | 1 + Sources/Swift/Helper/SentryApplication.swift | 2 ++ .../Helper/SentryApplicationExtensions.swift | 17 +++++++++++++++++ .../Swift/SentryCrash/SentryCrashWrapper.swift | 8 ++++---- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Sources/Sentry/SentryDependencyContainerSwiftHelper.m b/Sources/Sentry/SentryDependencyContainerSwiftHelper.m index ff437690247..44eb06b77ae 100644 --- a/Sources/Sentry/SentryDependencyContainerSwiftHelper.m +++ b/Sources/Sentry/SentryDependencyContainerSwiftHelper.m @@ -13,6 +13,11 @@ @implementation SentryDependencyContainerSwiftHelper return [SentryDependencyContainer.sharedInstance.application getWindows]; } ++ (CGSize)activeScreenSize +{ + return [SentryDependencyContainer.sharedInstance.application getActiveWindowSize]; +} + #endif // SENTRY_HAS_UIKIT + (NSString *)release:(SentryOptions *)options diff --git a/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h b/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h index b67d1680e45..580d7ac3417 100644 --- a/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h +++ b/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h @@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN #if SENTRY_HAS_UIKIT + (nullable NSArray *)windows; ++ (CGSize)activeScreenSize; #endif // SENTRY_HAS_UIKIT diff --git a/Sources/Swift/Helper/SentryApplication.swift b/Sources/Swift/Helper/SentryApplication.swift index 681ae87eb20..fa367d2a040 100644 --- a/Sources/Swift/Helper/SentryApplication.swift +++ b/Sources/Swift/Helper/SentryApplication.swift @@ -23,6 +23,8 @@ import UIKit */ func getWindows() -> [UIWindow]? + func getActiveWindowSize() -> CGSize + var connectedScenes: Set { get } var delegate: UIApplicationDelegate? { get } diff --git a/Sources/Swift/Helper/SentryApplicationExtensions.swift b/Sources/Swift/Helper/SentryApplicationExtensions.swift index 72ca06c3c3a..48facafc142 100644 --- a/Sources/Swift/Helper/SentryApplicationExtensions.swift +++ b/Sources/Swift/Helper/SentryApplicationExtensions.swift @@ -13,6 +13,10 @@ import UIKit internal_getWindows() } + @objc public func getActiveWindowSize() -> CGSize { + internal_getActiveWindowSize() + } + @objc public func relevantViewControllersNames() -> [String]? { internal_relevantViewControllersNames() } @@ -55,6 +59,19 @@ extension SentryApplication { return Array(windows) } + public func internal_getActiveWindowSize() -> CGSize { + var size = CGSize.zero + Dependencies.dispatchQueueWrapper.dispatchSyncOnMainQueue({ [weak self] in + guard let self, + let window = self.internal_getWindows()?.first else { + return + } + + size = window.bounds.size + }, timeout: 0.01) + return size + } + // This cannot be declared with @objc so until we delete more ObjC code it needs a separate // function than the objc visible one. public func internal_relevantViewControllersNames() -> [String]? { diff --git a/Sources/Swift/SentryCrash/SentryCrashWrapper.swift b/Sources/Swift/SentryCrash/SentryCrashWrapper.swift index bb9fb53195b..3f02c126d67 100644 --- a/Sources/Swift/SentryCrash/SentryCrashWrapper.swift +++ b/Sources/Swift/SentryCrash/SentryCrashWrapper.swift @@ -265,10 +265,10 @@ public final class SentryCrashWrapper: NSObject { private func setScreenDimensions(_ deviceData: inout [String: Any]) { // The UIWindowScene is unavailable on visionOS #if (os(iOS) || os(tvOS)) && !SENTRY_NO_UIKIT - if let appWindows = SentryDependencyContainerSwiftHelper.windows(), - let appScreen = appWindows.first?.screen { - deviceData["screen_height_pixels"] = appScreen.bounds.size.height - deviceData["screen_width_pixels"] = appScreen.bounds.size.width + let screenSize = SentryDependencyContainerSwiftHelper.activeScreenSize() + if screenSize != CGSize.zero { + deviceData["screen_height_pixels"] = screenSize.height + deviceData["screen_width_pixels"] = screenSize.width } #endif // (os(iOS) || os(tvOS)) && !SENTRY_NO_UIKIT } From 9d7d118432236fbeb9f435206a9d0ce899ebf085 Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Tue, 25 Nov 2025 16:43:26 -0300 Subject: [PATCH 2/7] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88f8f59ae75..b04fe4230b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Fix UIWindow being possibly accessed from a background thread in SentryCrashWrapper (#6905) + ## 9.0.0-rc.1 ### Breaking Changes From 804ceb2bbbc75a40935408ec99e12478471b4719 Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Tue, 25 Nov 2025 17:41:27 -0300 Subject: [PATCH 3/7] Fix test --- Tests/SentryTests/TestSentryUIApplication.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/SentryTests/TestSentryUIApplication.swift b/Tests/SentryTests/TestSentryUIApplication.swift index 2e3cb31453c..9848a52432d 100644 --- a/Tests/SentryTests/TestSentryUIApplication.swift +++ b/Tests/SentryTests/TestSentryUIApplication.swift @@ -9,6 +9,10 @@ final class TestSentryUIApplication: SentryApplication { return internal_getWindows() } + func getActiveWindowSize() -> CGSize { + return internal_getActiveWindowSize() + } + private var _windows: [UIWindow]? private(set) var calledOnMainThread = true var windows: [UIWindow]? { From 051051d6de2b75600924d9d58b4fe39102062c58 Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Wed, 26 Nov 2025 15:26:52 -0300 Subject: [PATCH 4/7] Use `UIWindow.screen.bounds` --- Sources/Swift/Helper/SentryApplicationExtensions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Swift/Helper/SentryApplicationExtensions.swift b/Sources/Swift/Helper/SentryApplicationExtensions.swift index 48facafc142..88ae0770edd 100644 --- a/Sources/Swift/Helper/SentryApplicationExtensions.swift +++ b/Sources/Swift/Helper/SentryApplicationExtensions.swift @@ -67,7 +67,7 @@ extension SentryApplication { return } - size = window.bounds.size + size = window.screen.bounds.size }, timeout: 0.01) return size } From d2c924435958d7330aabe166ccdbce7cebed8e9d Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Wed, 26 Nov 2025 15:45:59 -0300 Subject: [PATCH 5/7] Fix build on visionOS --- Sources/Swift/Helper/SentryApplication.swift | 2 ++ Sources/Swift/Helper/SentryApplicationExtensions.swift | 4 ++++ Tests/SentryTests/TestSentryUIApplication.swift | 2 ++ 3 files changed, 8 insertions(+) diff --git a/Sources/Swift/Helper/SentryApplication.swift b/Sources/Swift/Helper/SentryApplication.swift index fa367d2a040..0fc6cf012fc 100644 --- a/Sources/Swift/Helper/SentryApplication.swift +++ b/Sources/Swift/Helper/SentryApplication.swift @@ -23,7 +23,9 @@ import UIKit */ func getWindows() -> [UIWindow]? +#if (os(iOS) || os(tvOS)) func getActiveWindowSize() -> CGSize +#endif // os(iOS) || os(tvOS) var connectedScenes: Set { get } diff --git a/Sources/Swift/Helper/SentryApplicationExtensions.swift b/Sources/Swift/Helper/SentryApplicationExtensions.swift index 88ae0770edd..3d5bddb7a06 100644 --- a/Sources/Swift/Helper/SentryApplicationExtensions.swift +++ b/Sources/Swift/Helper/SentryApplicationExtensions.swift @@ -13,9 +13,11 @@ import UIKit internal_getWindows() } +#if (os(iOS) || os(tvOS)) @objc public func getActiveWindowSize() -> CGSize { internal_getActiveWindowSize() } +#endif // os(iOS) || os(tvOS) @objc public func relevantViewControllersNames() -> [String]? { internal_relevantViewControllersNames() @@ -59,6 +61,7 @@ extension SentryApplication { return Array(windows) } +#if (os(iOS) || os(tvOS)) public func internal_getActiveWindowSize() -> CGSize { var size = CGSize.zero Dependencies.dispatchQueueWrapper.dispatchSyncOnMainQueue({ [weak self] in @@ -71,6 +74,7 @@ extension SentryApplication { }, timeout: 0.01) return size } +#endif // os(iOS) || os(tvOS) // This cannot be declared with @objc so until we delete more ObjC code it needs a separate // function than the objc visible one. diff --git a/Tests/SentryTests/TestSentryUIApplication.swift b/Tests/SentryTests/TestSentryUIApplication.swift index 9848a52432d..f058aa3c410 100644 --- a/Tests/SentryTests/TestSentryUIApplication.swift +++ b/Tests/SentryTests/TestSentryUIApplication.swift @@ -9,9 +9,11 @@ final class TestSentryUIApplication: SentryApplication { return internal_getWindows() } +#if (os(iOS) || os(tvOS)) func getActiveWindowSize() -> CGSize { return internal_getActiveWindowSize() } +#endif // os(iOS) || os(tvOS) private var _windows: [UIWindow]? private(set) var calledOnMainThread = true From bfe8ff093643f94072c286517890ebdea4a3f23b Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Wed, 26 Nov 2025 16:13:18 -0300 Subject: [PATCH 6/7] Fix build on visionOS --- Sources/Sentry/SentryDependencyContainerSwiftHelper.m | 2 ++ Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/Sources/Sentry/SentryDependencyContainerSwiftHelper.m b/Sources/Sentry/SentryDependencyContainerSwiftHelper.m index 44eb06b77ae..1108668cb6a 100644 --- a/Sources/Sentry/SentryDependencyContainerSwiftHelper.m +++ b/Sources/Sentry/SentryDependencyContainerSwiftHelper.m @@ -13,10 +13,12 @@ @implementation SentryDependencyContainerSwiftHelper return [SentryDependencyContainer.sharedInstance.application getWindows]; } +# if TARGET_OS_IOS || TARGET_OS_TV + (CGSize)activeScreenSize { return [SentryDependencyContainer.sharedInstance.application getActiveWindowSize]; } +# endif // TARGET_OS_IOS || TARGET_OS_TV #endif // SENTRY_HAS_UIKIT diff --git a/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h b/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h index 580d7ac3417..c28159bfe74 100644 --- a/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h +++ b/Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h @@ -30,7 +30,10 @@ NS_ASSUME_NONNULL_BEGIN #if SENTRY_HAS_UIKIT + (nullable NSArray *)windows; + +# if TARGET_OS_IOS || TARGET_OS_TV + (CGSize)activeScreenSize; +# endif // TARGET_OS_IOS || TARGET_OS_TV #endif // SENTRY_HAS_UIKIT From 6929b4dc6ee581dc4f970d93644a4023538b758d Mon Sep 17 00:00:00 2001 From: Itay Brenner Date: Thu, 27 Nov 2025 11:49:53 -0300 Subject: [PATCH 7/7] Fix changelog merge conflict --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23857b56ab7..72987d17926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ - Fix UIWindow being possibly accessed from a background thread in SentryCrashWrapper (#6905) - Ensure SentrySDK.close resets everything on the main thread (#6907) -- Ensure SentrySDK.close resets everything on the main thread (#6907) - Allow transaction tags to be accessed and modified in `beforeSend` (#6910) ## 9.0.0-rc.1