diff --git a/CHANGELOG.md b/CHANGELOG.md index e7c9bae6276..51ff6926bdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Fixes - Too long flush duration (#2370) +- Do not delete the app state when OOM tracking is disabled. The app state is needed to determine the app start type on the next app start. (#2382) ## 7.30.2 diff --git a/Sources/Sentry/SentryAppStartTracker.m b/Sources/Sentry/SentryAppStartTracker.m index 6aca705f566..85a4c7dfa13 100644 --- a/Sources/Sentry/SentryAppStartTracker.m +++ b/Sources/Sentry/SentryAppStartTracker.m @@ -285,10 +285,6 @@ - (void)stop [NSNotificationCenter.defaultCenter removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; - -# if SENTRY_HAS_UIKIT - [self.appStateManager stop]; -# endif } - (void)dealloc diff --git a/Sources/Sentry/SentryAppStateManager.m b/Sources/Sentry/SentryAppStateManager.m index 23889da8f66..0302320f5e1 100644 --- a/Sources/Sentry/SentryAppStateManager.m +++ b/Sources/Sentry/SentryAppStateManager.m @@ -114,8 +114,6 @@ - (void)stop removeObserver:self name:SentryNSNotificationCenterWrapper.willTerminateNotificationName object:nil]; - - [self deleteAppState]; } } @@ -124,7 +122,6 @@ - (void)dealloc // In dealloc it's safe to unsubscribe for all, see // https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver [NSNotificationCenter.defaultCenter removeObserver:self]; - [self deleteAppState]; } /** @@ -199,11 +196,6 @@ - (void)storeCurrentAppState [self.fileManager storeAppState:[self buildCurrentAppState]]; } -- (void)deleteAppState -{ - [self.fileManager deleteAppState]; -} - #endif @end diff --git a/Sources/Sentry/include/SentryAppStateManager.h b/Sources/Sentry/include/SentryAppStateManager.h index 3b9e9ac4efc..d50ab4ff1ad 100644 --- a/Sources/Sentry/include/SentryAppStateManager.h +++ b/Sources/Sentry/include/SentryAppStateManager.h @@ -35,8 +35,6 @@ SENTRY_NO_INIT - (void)storeCurrentAppState; -- (void)deleteAppState; - - (void)updateAppState:(void (^)(SentryAppState *))block; #endif diff --git a/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift b/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift index 98b5f854aa6..5ec2d90526d 100644 --- a/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift +++ b/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift @@ -65,39 +65,14 @@ class SentryAppStateManagerTests: XCTestCase { XCTAssertNil(fixture.fileManager.readAppState()) } - func testStopDeletesAppState() { + func testStopDoesNotDeleteAppState() { XCTAssertNil(fixture.fileManager.readAppState()) sut.start() XCTAssertNotNil(fixture.fileManager.readAppState()) - sut.stop() - XCTAssertNil(fixture.fileManager.readAppState()) - } - - func testStopOnlyRunsLogicWhenStartCountBecomesZero() { - XCTAssertNil(fixture.fileManager.readAppState()) - - sut.start() - XCTAssertNotNil(fixture.fileManager.readAppState()) - - sut.start() - sut.stop() XCTAssertNotNil(fixture.fileManager.readAppState()) - - sut.stop() - XCTAssertNil(fixture.fileManager.readAppState()) - } - - func testStoreAndDeleteAppState() { - XCTAssertNil(fixture.fileManager.readAppState()) - - sut.storeCurrentAppState() - XCTAssertNotNil(fixture.fileManager.readAppState()) - - sut.deleteAppState() - XCTAssertNil(fixture.fileManager.readAppState()) } func testUpdateAppState() { diff --git a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift index 7b4eadac10f..45a7f1bb04c 100644 --- a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift @@ -69,7 +69,7 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { func testSecondStart_AfterSystemReboot_IsColdStart() { let previousBootTime = fixture.currentDate.date().addingTimeInterval(-1) let appState = SentryAppState(releaseName: TestData.appState.releaseName, osVersion: UIDevice.current.systemVersion, vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: previousBootTime) - givenPreviousAppState(appState: appState) + store(appState: appState) startApp() @@ -84,10 +84,25 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { assertValidStart(type: .warm) } + + // Test for situation described in https://github.com/getsentry/sentry-cocoa/issues/2376 + func testSecondStart_SystemNotRebooted_OOM_disabled_IsWarmStart() { + givenSystemNotRebooted() + + fixture.options.enableOutOfMemoryTracking = false + + fixture.fileManager.moveAppStateToPreviousAppState() + startApp() + assertValidStart(type: .warm) + + fixture.fileManager.moveAppStateToPreviousAppState() + startApp() + assertValidStart(type: .warm) + } func testAppUpgrade_IsColdStart() { let appState = SentryAppState(releaseName: "0.9.0", osVersion: UIDevice.current.systemVersion, vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: fixture.currentDate.date()) - givenPreviousAppState(appState: appState) + store(appState: appState) startApp() @@ -95,7 +110,7 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { } func testAppWasInBackground_NoAppStartUp() { - givenPreviousAppState(appState: TestData.appState) + store(appState: TestData.appState) startApp() @@ -113,7 +128,7 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { terminateApp() let appState = SentryAppState(releaseName: "1.0.0", osVersion: "14.4.1", vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: self.fixture.currentDate.date()) - givenPreviousAppState(appState: appState) + store(appState: appState) fixture.fileManager.moveAppStateToPreviousAppState() startApp() @@ -126,7 +141,7 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { */ func testAppLaunches_PreviousBootTimeInFuture_NoAppStartUp() { let appState = SentryAppState(releaseName: TestData.appState.releaseName, osVersion: UIDevice.current.systemVersion, vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: fixture.currentDate.date().addingTimeInterval(1)) - givenPreviousAppState(appState: appState) + store(appState: appState) fixture.fileManager.moveAppStateToPreviousAppState() startApp() @@ -271,7 +286,7 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { assertValidHybridStart(type: .warm) } - private func givenPreviousAppState(appState: SentryAppState) { + private func store(appState: SentryAppState) { fixture.fileManager.store(appState) } @@ -279,7 +294,7 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { let systemBootTimestamp = fixture.currentDate.date() fixture.sysctl.setProcessStartTimestamp(value: fixture.currentDate.date()) let appState = SentryAppState(releaseName: TestData.appState.releaseName, osVersion: UIDevice.current.systemVersion, vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: systemBootTimestamp) - givenPreviousAppState(appState: appState) + store(appState: appState) } private func givenProcessStartTimestamp(processStartTimestamp: Date? = nil) {