Skip to content

Commit 0a54d3f

Browse files
committed
fix: Race condition in SentryApplication and Swift implementation
1 parent 8d944ac commit 0a54d3f

35 files changed

+441
-686
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 20 additions & 20 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
@@ -46,7 +46,6 @@
4646
#import "SentryTransport.h"
4747
#import "SentryTransportAdapter.h"
4848
#import "SentryTransportFactory.h"
49-
#import "SentryUIApplication.h"
5049
#import "SentryUseNSExceptionCallstackWrapper.h"
5150
#import "SentryUser.h"
5251
#import "SentryWatchdogTerminationTracker.h"
@@ -770,7 +769,7 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
770769
context[@"app"] = app;
771770

772771
UIApplicationState appState =
773-
[SentryDependencyContainer sharedInstance].application.applicationState;
772+
[SentryDependencyContainer sharedInstance].threadsafeApplication.applicationState;
774773
BOOL inForeground = appState == UIApplicationStateActive;
775774
app[@"in_foreground"] = @(inForeground);
776775
event.context = context;

Sources/Sentry/SentryCrashIntegration.m

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

2828
#if SENTRY_HAS_UIKIT
29-
# import "SentryUIApplication.h"
3029
# import <UIKit/UIKit.h>
3130
#endif
3231

Sources/Sentry/SentryCrashWrapper.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import "SentryNSProcessInfoWrapper.h"
88
#import "SentryScope+PrivateSwift.h"
99
#import "SentryScope.h"
10+
#import "SentrySwift.h"
1011
#import "SentryUIDeviceWrapper.h"
1112
#import <SentryCrashCachedData.h>
1213
#import <SentryCrashDebug.h>
@@ -15,7 +16,6 @@
1516
#include <mach/mach.h>
1617

1718
#if SENTRY_HAS_UIKIT
18-
# import "SentryUIApplication.h"
1919
# import <UIKit/UIKit.h>
2020
#endif
2121

@@ -185,7 +185,8 @@ - (void)enrichScope:(SentryScope *)scope
185185
// The UIWindowScene is unavailable on visionOS
186186
#if SENTRY_TARGET_REPLAY_SUPPORTED
187187

188-
NSArray<UIWindow *> *appWindows = SentryDependencyContainer.sharedInstance.application.windows;
188+
NSArray<UIWindow *> *appWindows =
189+
[SentryDependencyContainer.sharedInstance.application getWindows];
189190
if ([appWindows count] > 0) {
190191
UIScreen *appScreen = appWindows.firstObject.screen;
191192
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"
@@ -44,7 +43,6 @@
4443
#if SENTRY_HAS_UIKIT
4544
# import "SentryANRTrackerV2.h"
4645
# import "SentryFramesTracker.h"
47-
# import "SentryUIApplication.h"
4846
# import <SentryViewHierarchyProvider.h>
4947
# import <SentryWatchdogTerminationBreadcrumbProcessor.h>
5048
#endif // SENTRY_HAS_UIKIT
@@ -53,10 +51,6 @@
5351
# import "SentryUIDeviceWrapper.h"
5452
#endif // TARGET_OS_IOS
5553

56-
#if TARGET_OS_OSX
57-
# import "SentryNSApplication.h"
58-
#endif
59-
6054
#if !TARGET_OS_WATCH
6155
# import "SentryReachability.h"
6256
#endif // !TARGET_OS_WATCH
@@ -166,14 +160,15 @@ - (instancetype)init
166160
_binaryImageCache = [[SentryBinaryImageCache alloc] init];
167161
_dateProvider = SentryDependencies.dateProvider;
168162

169-
_notificationCenterWrapper = [NSNotificationCenter defaultCenter];
163+
_notificationCenterWrapper = NSNotificationCenter.defaultCenter;
170164
#if SENTRY_HAS_UIKIT
171165
_uiDeviceWrapper = [[SentryUIDeviceWrapper alloc] init];
172-
_application = [[SentryUIApplication alloc]
173-
initWithNotificationCenterWrapper:_notificationCenterWrapper
174-
dispatchQueueWrapper:_dispatchQueueWrapper];
166+
_application = UIApplication.sharedApplication;
167+
_threadsafeApplication = [[SentryThreadsafeApplication alloc]
168+
initWithInitialState:_application.unsafeApplicationState
169+
notificationCenter:_notificationCenterWrapper];
175170
#elif TARGET_OS_OSX
176-
_application = [[SentryNSApplication alloc] init];
171+
_application = NSApplication.sharedApplication;
177172
#endif // SENTRY_HAS_UIKIT
178173

179174
_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)