From ae9c51b0d6e71205648e9d44d9f4d61827ccd1e6 Mon Sep 17 00:00:00 2001 From: Sepehr Behroozi <3pehrbehroozi@gmail.com> Date: Tue, 24 Oct 2023 11:25:37 +0200 Subject: [PATCH] Fix: SDK detects OutOfMemory after device reboot (#3352) If the host app has been terminated due to device reboot, SentrySDK assumes it as a WatchdogTermination crash. In such cases, mostly, the boot time will be mentioned as "a few seconds before this event". ![Screenshot 2023-10-19 at 09 52 27](https://github.com/getsentry/sentry-cocoa/assets/1960884/c66f778a-cb4e-4687-991b-646de004b194) The idea behind this fix is that if the system boot time has been changed since the previous app launch, most likely the app has been terminated due to the reboot and the SentrySDK should not consider it as a WatchdogTermination crash. Co-authored-by: Dhiogo Brustolin --- CHANGELOG.md | 4 ++++ Sources/Sentry/SentryWatchdogTerminationLogic.m | 5 +++++ .../SentryCrash/SentryCrashIntegrationTests.swift | 1 + .../SentryWatchdogTerminationsTrackerTests.swift | 13 ++++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48c7cfb04a2..d6c3d0c25f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Enrich error events with any underlying NSErrors reported by Cocoa APIs (#3230) +### Features + +- Improve OOM detection by ignoring system reboot (#3352) + ### Fixes - Missing `mechanism.handled` is not considered crash (#3353) diff --git a/Sources/Sentry/SentryWatchdogTerminationLogic.m b/Sources/Sentry/SentryWatchdogTerminationLogic.m index a65397ff769..f80390e02f9 100644 --- a/Sources/Sentry/SentryWatchdogTerminationLogic.m +++ b/Sources/Sentry/SentryWatchdogTerminationLogic.m @@ -60,6 +60,11 @@ - (BOOL)isWatchdogTermination return NO; } + // The app may have been terminated due to device reboot + if (previousAppState.systemBootTimestamp != currentAppState.systemBootTimestamp) { + return NO; + } + // This value can change when installing test builds using Xcode or when installing an app // on a device using ad-hoc distribution. if (![currentAppState.vendorId isEqualToString:previousAppState.vendorId]) { diff --git a/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift b/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift index 8c78fdb1de9..57d93bc1109 100644 --- a/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift @@ -13,6 +13,7 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase { let sentryCrash: TestSentryCrashWrapper init() { + SentryDependencyContainer.sharedInstance().sysctlWrapper = TestSysctl() sentryCrash = TestSentryCrashWrapper.sharedInstance() sentryCrash.internalActiveDurationSinceLastCrash = 5.0 sentryCrash.internalCrashedLastLaunch = true diff --git a/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift b/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift index a53d539cc28..942b18fc6eb 100644 --- a/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift +++ b/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift @@ -216,7 +216,18 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase { sut.start() assertNoOOMSent() } - + + func testDifferentBootTime_NoOOM() { + sut = fixture.getSut(usingRealFileManager: true) + sut.start() + let appState = SentryAppState(releaseName: fixture.options.releaseName ?? "", osVersion: UIDevice.current.systemVersion, vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: fixture.sysctl.systemBootTimestamp.addingTimeInterval(1)) + + givenPreviousAppState(appState: appState) + fixture.mockFileManager.moveAppStateToPreviousAppState() + sut.start() + assertNoOOMSent() + } + func testAppWasInForeground_OOM() { sut = fixture.getSut(usingRealFileManager: true)