Skip to content

Commit b117726

Browse files
committed
fix: Race condition in SentryApplication and Swift implementation
1 parent d1c0538 commit b117726

35 files changed

+419
-694
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 20 additions & 22 deletions
Large diffs are not rendered by default.

Sources/Sentry/SentryANRTrackingIntegration.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#import "SentryThread.h"
1818
#import "SentryThreadInspector.h"
1919
#import "SentryThreadWrapper.h"
20-
#import "SentryUIApplication.h"
2120
#import <SentryCrashWrapper.h>
2221
#import <SentryOptions+Private.h>
2322

@@ -120,7 +119,7 @@ - (void)anrDetectedWithType:(enum SentryANRType)type
120119

121120
// If the app is not active, the main thread may be blocked or too busy.
122121
// Since there is no UI for the user to interact, there is no need to report app hang.
123-
if (SentryDependencyContainer.sharedInstance.application.applicationState
122+
if (SentryDependencyContainer.sharedInstance.threadsafeApplication.applicationState
124123
!= UIApplicationStateActive) {
125124
return;
126125
}

Sources/Sentry/SentryClient.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#import "SentryTransport.h"
4646
#import "SentryTransportAdapter.h"
4747
#import "SentryTransportFactory.h"
48-
#import "SentryUIApplication.h"
4948
#import "SentryUseNSExceptionCallstackWrapper.h"
5049
#import "SentryUser.h"
5150
#import "SentryWatchdogTerminationTracker.h"
@@ -782,7 +781,7 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
782781
context[@"app"] = app;
783782

784783
UIApplicationState appState =
785-
[SentryDependencyContainer sharedInstance].application.applicationState;
784+
[SentryDependencyContainer sharedInstance].threadsafeApplication.applicationState;
786785
BOOL inForeground = appState == UIApplicationStateActive;
787786
app[@"in_foreground"] = @(inForeground);
788787
event.context = context;

Sources/Sentry/SentryCrashIntegration.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#import <SentrySDK+Private.h>
2626

2727
#if SENTRY_HAS_UIKIT
28-
# import "SentryUIApplication.h"
2928
# import <UIKit/UIKit.h>
3029
#endif
3130

Sources/Sentry/SentryCrashWrapper.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <mach/mach.h>
1616

1717
#if SENTRY_HAS_UIKIT
18-
# import "SentryUIApplication.h"
1918
# import <UIKit/UIKit.h>
2019
#endif
2120

@@ -185,7 +184,8 @@ - (void)enrichScope:(SentryScope *)scope
185184
// The UIWindowScene is unavailable on visionOS
186185
#if SENTRY_TARGET_REPLAY_SUPPORTED
187186

188-
NSArray<UIWindow *> *appWindows = SentryDependencyContainer.sharedInstance.application.windows;
187+
NSArray<UIWindow *> *appWindows =
188+
[SentryDependencyContainer.sharedInstance.application getWindows];
189189
if ([appWindows count] > 0) {
190190
UIScreen *appScreen = appWindows.firstObject.screen;
191191
if (appScreen != nil) {

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#import "SentryANRTrackerV1.h"
22

3-
#import "SentryApplication.h"
43
#import "SentryBinaryImageCache.h"
54
#import "SentryDefaultObjCRuntimeWrapper.h"
65
#import "SentryDispatchFactory.h"
@@ -43,15 +42,10 @@
4342
#if SENTRY_HAS_UIKIT
4443
# import "SentryANRTrackerV2.h"
4544
# import "SentryFramesTracker.h"
46-
# import "SentryUIApplication.h"
4745
# import <SentryViewHierarchyProvider.h>
4846
# import <SentryWatchdogTerminationBreadcrumbProcessor.h>
4947
#endif // SENTRY_HAS_UIKIT
5048

51-
#if TARGET_OS_OSX
52-
# import "SentryNSApplication.h"
53-
#endif
54-
5549
#if !TARGET_OS_WATCH
5650
# import "SentryReachability.h"
5751
#endif // !TARGET_OS_WATCH
@@ -161,15 +155,16 @@ - (instancetype)init
161155
_binaryImageCache = [[SentryBinaryImageCache alloc] init];
162156
_dateProvider = SentryDependencies.dateProvider;
163157

164-
_notificationCenterWrapper = [NSNotificationCenter defaultCenter];
158+
_notificationCenterWrapper = NSNotificationCenter.defaultCenter;
165159
#if SENTRY_HAS_UIKIT
166160
_uiDeviceWrapper =
167161
[[SentryDefaultUIDeviceWrapper alloc] initWithQueueWrapper:_dispatchQueueWrapper];
168-
_application = [[SentryUIApplication alloc]
169-
initWithNotificationCenterWrapper:_notificationCenterWrapper
170-
dispatchQueueWrapper:_dispatchQueueWrapper];
162+
_application = UIApplication.sharedApplication;
163+
_threadsafeApplication = [[SentryThreadsafeApplication alloc]
164+
initWithInitialState:_application.unsafeApplicationState
165+
notificationCenter:_notificationCenterWrapper];
171166
#elif TARGET_OS_OSX
172-
_application = [[SentryNSApplication alloc] init];
167+
_application = NSApplication.sharedApplication;
173168
#endif // SENTRY_HAS_UIKIT
174169

175170
_processInfoWrapper = [[SentryNSProcessInfoWrapper alloc] init];

Sources/Sentry/SentryDependencyContainerSwiftHelper.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#import "SentryDependencyContainer.h"
33
#import "SentrySDK+Private.h"
44
#import "SentrySwift.h"
5-
#import "SentryUIApplication.h"
65

76
@implementation SentryDependencyContainerSwiftHelper
87

98
#if SENTRY_HAS_UIKIT
109

1110
+ (NSArray<UIWindow *> *)windows
1211
{
13-
return SentryDependencyContainer.sharedInstance.application.windows;
12+
return [SentryDependencyContainer.sharedInstance.application getWindows];
1413
}
1514

1615
#endif // SENTRY_HAS_UIKIT

Sources/Sentry/SentryNSApplication.m

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/Sentry/SentrySDKInternal.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#import "SentrySerialization.h"
2626
#import "SentrySwift.h"
2727
#import "SentryTransactionContext.h"
28-
#import "SentryUIApplication.h"
2928
#import "SentryUseNSExceptionCallstackWrapper.h"
3029
#import "SentryUserFeedbackIntegration.h"
3130

Sources/Sentry/SentrySessionReplayIntegration.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# import "SentrySessionReplaySyncC.h"
2424
# import "SentrySwift.h"
2525
# import "SentrySwizzle.h"
26-
# import "SentryUIApplication.h"
2726
# import <UIKit/UIKit.h>
2827

2928
NS_ASSUME_NONNULL_BEGIN
@@ -332,7 +331,7 @@ - (void)startSession
332331

333332
- (void)runReplayForAvailableWindow
334333
{
335-
if (SentryDependencyContainer.sharedInstance.application.windows.count > 0) {
334+
if ([SentryDependencyContainer.sharedInstance.application getWindows].count > 0) {
336335
SENTRY_LOG_DEBUG(@"[Session Replay] Running replay for available window");
337336
// If a window its already available start replay right away
338337
[self startWithOptions:_replayOptions fullSession:_startedAsFullSession];
@@ -415,7 +414,8 @@ - (void)startWithOptions:(SentryReplayOptions *)replayOptions
415414
displayLinkWrapper:displayLinkWrapper];
416415

417416
[self.sessionReplay
418-
startWithRootView:SentryDependencyContainer.sharedInstance.application.windows.firstObject
417+
startWithRootView:[SentryDependencyContainer.sharedInstance.application getWindows]
418+
.firstObject
419419
fullSession:shouldReplayFullSession];
420420

421421
[_notificationCenter addObserver:self
@@ -771,7 +771,8 @@ - (void)showMaskPreview:(CGFloat)opacity
771771
return;
772772
}
773773

774-
UIWindow *window = SentryDependencyContainer.sharedInstance.application.windows.firstObject;
774+
UIWindow *window =
775+
[SentryDependencyContainer.sharedInstance.application getWindows].firstObject;
775776
if (window == nil) {
776777
SENTRY_LOG_WARN(@"[Session Replay] No UIWindow available to display preview");
777778
return;

0 commit comments

Comments
 (0)