Skip to content

Commit 3540969

Browse files
authored
fix: UIWindow being possibly accessed from a background thread in SentryCrashWrapper (#6905)
* fix: UIWindow being possibly accesed from a background thread in SentryCrashWrapper * Update changelog * Fix test * Use `UIWindow.screen.bounds` * Fix build on visionOS * Fix build on visionOS * Fix changelog merge conflict
1 parent 5bc23b4 commit 3540969

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Fix UIWindow being possibly accessed from a background thread in SentryCrashWrapper (#6905)
78
- Ensure SentrySDK.close resets everything on the main thread (#6907)
89
- Allow transaction tags to be accessed and modified in `beforeSend` (#6910)
910

Sources/Sentry/SentryDependencyContainerSwiftHelper.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ @implementation SentryDependencyContainerSwiftHelper
1313
return [SentryDependencyContainer.sharedInstance.application getWindows];
1414
}
1515

16+
# if TARGET_OS_IOS || TARGET_OS_TV
17+
+ (CGSize)activeScreenSize
18+
{
19+
return [SentryDependencyContainer.sharedInstance.application getActiveWindowSize];
20+
}
21+
# endif // TARGET_OS_IOS || TARGET_OS_TV
22+
1623
#endif // SENTRY_HAS_UIKIT
1724

1825
+ (NSString *)release:(SentryOptions *)options

Sources/Sentry/include/SentryDependencyContainerSwiftHelper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ NS_ASSUME_NONNULL_BEGIN
3131

3232
+ (nullable NSArray<UIWindow *> *)windows;
3333

34+
# if TARGET_OS_IOS || TARGET_OS_TV
35+
+ (CGSize)activeScreenSize;
36+
# endif // TARGET_OS_IOS || TARGET_OS_TV
37+
3438
#endif // SENTRY_HAS_UIKIT
3539

3640
+ (NSString *_Nullable)release:(SentryOptionsObjC *)options;

Sources/Swift/Helper/SentryApplication.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import UIKit
2323
*/
2424
func getWindows() -> [UIWindow]?
2525

26+
#if (os(iOS) || os(tvOS))
27+
func getActiveWindowSize() -> CGSize
28+
#endif // os(iOS) || os(tvOS)
29+
2630
var connectedScenes: Set<UIScene> { get }
2731

2832
var delegate: UIApplicationDelegate? { get }

Sources/Swift/Helper/SentryApplicationExtensions.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import UIKit
1313
internal_getWindows()
1414
}
1515

16+
#if (os(iOS) || os(tvOS))
17+
@objc public func getActiveWindowSize() -> CGSize {
18+
internal_getActiveWindowSize()
19+
}
20+
#endif // os(iOS) || os(tvOS)
21+
1622
@objc public func relevantViewControllersNames() -> [String]? {
1723
internal_relevantViewControllersNames()
1824
}
@@ -55,6 +61,21 @@ extension SentryApplication {
5561
return Array(windows)
5662
}
5763

64+
#if (os(iOS) || os(tvOS))
65+
public func internal_getActiveWindowSize() -> CGSize {
66+
var size = CGSize.zero
67+
Dependencies.dispatchQueueWrapper.dispatchSyncOnMainQueue({ [weak self] in
68+
guard let self,
69+
let window = self.internal_getWindows()?.first else {
70+
return
71+
}
72+
73+
size = window.screen.bounds.size
74+
}, timeout: 0.01)
75+
return size
76+
}
77+
#endif // os(iOS) || os(tvOS)
78+
5879
// This cannot be declared with @objc so until we delete more ObjC code it needs a separate
5980
// function than the objc visible one.
6081
public func internal_relevantViewControllersNames() -> [String]? {

Sources/Swift/SentryCrash/SentryCrashWrapper.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ public final class SentryCrashWrapper: NSObject {
265265
private func setScreenDimensions(_ deviceData: inout [String: Any]) {
266266
// The UIWindowScene is unavailable on visionOS
267267
#if (os(iOS) || os(tvOS)) && !SENTRY_NO_UIKIT
268-
if let appWindows = SentryDependencyContainerSwiftHelper.windows(),
269-
let appScreen = appWindows.first?.screen {
270-
deviceData["screen_height_pixels"] = appScreen.bounds.size.height
271-
deviceData["screen_width_pixels"] = appScreen.bounds.size.width
268+
let screenSize = SentryDependencyContainerSwiftHelper.activeScreenSize()
269+
if screenSize != CGSize.zero {
270+
deviceData["screen_height_pixels"] = screenSize.height
271+
deviceData["screen_width_pixels"] = screenSize.width
272272
}
273273
#endif // (os(iOS) || os(tvOS)) && !SENTRY_NO_UIKIT
274274
}

Tests/SentryTests/TestSentryUIApplication.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ final class TestSentryUIApplication: SentryApplication {
99
return internal_getWindows()
1010
}
1111

12+
#if (os(iOS) || os(tvOS))
13+
func getActiveWindowSize() -> CGSize {
14+
return internal_getActiveWindowSize()
15+
}
16+
#endif // os(iOS) || os(tvOS)
17+
1218
private var _windows: [UIWindow]?
1319
private(set) var calledOnMainThread = true
1420
var windows: [UIWindow]? {

0 commit comments

Comments
 (0)