Skip to content

Commit c2e34f8

Browse files
committed
fix: Race condition in SentryApplication and Swift implementation
1 parent 3ffd0e5 commit c2e34f8

36 files changed

+420
-696
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
@@ -42,7 +42,6 @@
4242
#import "SentryTransport.h"
4343
#import "SentryTransportAdapter.h"
4444
#import "SentryTransportFactory.h"
45-
#import "SentryUIApplication.h"
4645
#import "SentryUseNSExceptionCallstackWrapper.h"
4746
#import "SentryUser.h"
4847
#import "SentryWatchdogTerminationTracker.h"
@@ -785,7 +784,7 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
785784
context[@"app"] = app;
786785

787786
UIApplicationState appState =
788-
[SentryDependencyContainer sharedInstance].application.applicationState;
787+
[SentryDependencyContainer sharedInstance].threadsafeApplication.applicationState;
789788
BOOL inForeground = appState == UIApplicationStateActive;
790789
app[@"in_foreground"] = @(inForeground);
791790
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
@@ -14,7 +14,6 @@
1414
#include <mach/mach.h>
1515

1616
#if SENTRY_HAS_UIKIT
17-
# import "SentryUIApplication.h"
1817
# import <UIKit/UIKit.h>
1918
#endif
2019

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

187-
NSArray<UIWindow *> *appWindows = SentryDependencyContainer.sharedInstance.application.windows;
186+
NSArray<UIWindow *> *appWindows =
187+
[SentryDependencyContainer.sharedInstance.application getWindows];
188188
if ([appWindows count] > 0) {
189189
UIScreen *appScreen = appWindows.firstObject.screen;
190190
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 "SentryDispatchFactory.h"
54
#import "SentryDisplayLinkWrapper.h"
65
#import "SentryExtraContextProvider.h"
@@ -38,15 +37,10 @@
3837
#if SENTRY_HAS_UIKIT
3938
# import "SentryANRTrackerV2.h"
4039
# import "SentryFramesTracker.h"
41-
# import "SentryUIApplication.h"
4240
# import <SentryViewHierarchyProvider.h>
4341
# import <SentryWatchdogTerminationBreadcrumbProcessor.h>
4442
#endif // SENTRY_HAS_UIKIT
4543

46-
#if TARGET_OS_OSX
47-
# import "SentryNSApplication.h"
48-
#endif
49-
5044
#if !TARGET_OS_WATCH
5145
# import "SentryReachability.h"
5246
#endif // !TARGET_OS_WATCH
@@ -156,15 +150,16 @@ - (instancetype)init
156150
_binaryImageCache = [[SentryBinaryImageCache alloc] init];
157151
_dateProvider = SentryDependencies.dateProvider;
158152

159-
_notificationCenterWrapper = [NSNotificationCenter defaultCenter];
153+
_notificationCenterWrapper = NSNotificationCenter.defaultCenter;
160154
#if SENTRY_HAS_UIKIT
161155
_uiDeviceWrapper =
162156
[[SentryDefaultUIDeviceWrapper alloc] initWithQueueWrapper:_dispatchQueueWrapper];
163-
_application = [[SentryUIApplication alloc]
164-
initWithNotificationCenterWrapper:_notificationCenterWrapper
165-
dispatchQueueWrapper:_dispatchQueueWrapper];
157+
_application = UIApplication.sharedApplication;
158+
_threadsafeApplication = [[SentryThreadsafeApplication alloc]
159+
initWithInitialState:_application.unsafeApplicationState
160+
notificationCenter:_notificationCenterWrapper];
166161
#elif TARGET_OS_OSX
167-
_application = [[SentryNSApplication alloc] init];
162+
_application = NSApplication.sharedApplication;
168163
#endif // SENTRY_HAS_UIKIT
169164

170165
_processInfoWrapper = NSProcessInfo.processInfo;

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
@@ -23,7 +23,6 @@
2323
#import "SentrySerialization.h"
2424
#import "SentrySwift.h"
2525
#import "SentryTransactionContext.h"
26-
#import "SentryUIApplication.h"
2726
#import "SentryUseNSExceptionCallstackWrapper.h"
2827
#import "SentryUserFeedbackIntegration.h"
2928

Sources/Sentry/SentrySessionReplayIntegration.m

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

2827
NS_ASSUME_NONNULL_BEGIN
@@ -331,7 +330,7 @@ - (void)startSession
331330

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

416415
[self.sessionReplay
417-
startWithRootView:SentryDependencyContainer.sharedInstance.application.windows.firstObject
416+
startWithRootView:[SentryDependencyContainer.sharedInstance.application getWindows]
417+
.firstObject
418418
fullSession:shouldReplayFullSession];
419419

420420
[_notificationCenter addObserver:self
@@ -770,7 +770,8 @@ - (void)showMaskPreview:(CGFloat)opacity
770770
return;
771771
}
772772

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

0 commit comments

Comments
 (0)