Skip to content

Commit 13d625a

Browse files
authored
fix: Ensure SentrySDK.close resets everything on the main thread (#6907)
* fix: Ensure SentrySDK.close resets everything on the main thread * Update changelog * Use `dispatchSyncOnMainQueue` + update startTimestamp inside the block
1 parent 94f3691 commit 13d625a

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- fix: Ensure SentrySDK.close resets everything on the main thread (#6907)
8+
39
## 9.0.0-rc.1
410

511
### Breaking Changes

Sources/Sentry/SentrySDKInternal.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -613,36 +613,39 @@ + (void)close
613613
{
614614
SENTRY_LOG_DEBUG(@"Starting to close SDK.");
615615

616+
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper dispatchSyncOnMainQueue:^{
617+
616618
#if SENTRY_TARGET_PROFILING_SUPPORTED
617-
[SentryContinuousProfiler stop];
619+
[SentryContinuousProfiler stop];
618620
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
619621

620-
startTimestamp = nil;
622+
startTimestamp = nil;
621623

622-
SentryHubInternal *hub = SentrySDKInternal.currentHub;
623-
[hub removeAllIntegrations];
624+
SentryHubInternal *hub = SentrySDKInternal.currentHub;
625+
[hub removeAllIntegrations];
624626

625-
SENTRY_LOG_DEBUG(@"Uninstalled all integrations.");
627+
SENTRY_LOG_DEBUG(@"Uninstalled all integrations.");
626628

627629
#if SENTRY_HAS_UIKIT
628-
// force the AppStateManager to unsubscribe, see
629-
// https://github.com/getsentry/sentry-cocoa/issues/2455
630-
[[SentryDependencyContainer sharedInstance].appStateManager stopWithForce:YES];
630+
// force the AppStateManager to unsubscribe, see
631+
// https://github.com/getsentry/sentry-cocoa/issues/2455
632+
[[SentryDependencyContainer sharedInstance].appStateManager stopWithForce:YES];
631633
#endif
632634

633-
[hub close];
634-
[hub bindClient:nil];
635+
[hub close];
636+
[hub bindClient:nil];
635637

636-
[SentrySDKInternal setCurrentHub:nil];
638+
[SentrySDKInternal setCurrentHub:nil];
637639

638-
[SentryDependencyContainer.sharedInstance.crashWrapper stopBinaryImageCache];
639-
[SentryDependencyContainer.sharedInstance.binaryImageCache stop];
640+
[SentryDependencyContainer.sharedInstance.crashWrapper stopBinaryImageCache];
641+
[SentryDependencyContainer.sharedInstance.binaryImageCache stop];
640642

641643
#if TARGET_OS_IOS && SENTRY_HAS_UIKIT
642-
[SentryDependencyContainer.sharedInstance.uiDeviceWrapper stop];
644+
[SentryDependencyContainer.sharedInstance.uiDeviceWrapper stop];
643645
#endif // TARGET_OS_IOS && SENTRY_HAS_UIKIT
644646

645-
[SentryDependencyContainer reset];
647+
[SentryDependencyContainer reset];
648+
}];
646649
SENTRY_LOG_DEBUG(@"SDK closed!");
647650
}
648651

Tests/SentryTests/SentrySDKInternalTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,26 @@ class SentrySDKInternalTests: XCTestCase {
667667
XCTAssertEqual(flushTimeout, transport.flushInvocations.first ?? 0.0, accuracy: 0.2)
668668
}
669669

670+
func testClose_DispatchesOnMainThread() {
671+
// Arrange
672+
let dispatchQueueWrapper = TestSentryDispatchQueueWrapper()
673+
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = dispatchQueueWrapper
674+
675+
SentrySDK.start { options in
676+
options.dsn = SentrySDKInternalTests.dsnAsString
677+
options.removeAllIntegrations()
678+
}
679+
680+
// Reset the invocation count after start since start also uses the main thread dispatch
681+
dispatchQueueWrapper.blockOnMainInvocations = Invocations<() -> Void>()
682+
683+
// Act
684+
SentrySDK.close()
685+
686+
// Assert
687+
XCTAssertEqual(1, dispatchQueueWrapper.blockOnMainInvocations.count, "Close should dispatch exactly once to main queue")
688+
}
689+
670690
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
671691

672692
func testSetAppStartMeasurementConcurrently() {

0 commit comments

Comments
 (0)