Skip to content

Commit

Permalink
feat: Add field (#3644)
Browse files Browse the repository at this point in the history
This property returns true if the SDK detected a start-up crash during
SDK initialization.

Fixes GH-3639
  • Loading branch information
philipphofmann authored Feb 19, 2024
1 parent 62fbecd commit 1bdf1af
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Add field `SentrySDK.detectedStartUpCrash` (#3644)
- Automatically profile app launches (#3529)

### Improvements
Expand Down
1 change: 1 addition & 0 deletions SentryTestUtils/ClearTestState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TestCleanup: NSObject {
SentrySDK.setCurrentHub(nil)
SentrySDK.crashedLastRunCalled = false
SentrySDK.startInvocations = 0
SentrySDK.setDetectedStartUpCrash(false)
PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = false
SentryNetworkTracker.sharedInstance.disable()

Expand Down
10 changes: 10 additions & 0 deletions Sources/Sentry/Public/SentrySDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ SENTRY_NO_INIT
*/
@property (nonatomic, class, readonly) BOOL crashedLastRun;

/**
* Checks if the SDK detected a start-up crash during SDK initialization.
*
* @note The SDK init waits synchronously for up to 5 seconds to flush out events if the app crashes
* within 2 seconds after the SDK init.
*
* @return @c YES if the SDK detected a start-up crash and @c NO if not.
*/
@property (nonatomic, class, readonly) BOOL detectedStartUpCrash;

/**
* Set user to the current Scope of the current Hub.
* @param user The user to set to the current Scope.
Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/SentryCrashReportSink.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ - (void)filterReports:(NSArray *)reports
if (durationFromCrashStateInitToLastCrash > 0
&& durationFromCrashStateInitToLastCrash <= SENTRY_APP_START_CRASH_DURATION_THRESHOLD) {
SENTRY_LOG_WARN(@"Startup crash: detected.");

[SentrySDK setDetectedStartUpCrash:YES];

[self sendReports:reports onCompletion:onCompletion];

[SentrySDK flush:SENTRY_APP_START_CRASH_FLUSH_DURATION];
Expand Down
12 changes: 12 additions & 0 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @implementation SentrySDK
static BOOL crashedLastRunCalled;
static SentryAppStartMeasurement *sentrySDKappStartMeasurement;
static NSObject *sentrySDKappStartMeasurementLock;
static BOOL _detectedStartUpCrash;

/**
* @brief We need to keep track of the number of times @c +[startWith...] is called, because our OOM
Expand All @@ -59,6 +60,7 @@ + (void)initialize
if (self == [SentrySDK class]) {
sentrySDKappStartMeasurementLock = [[NSObject alloc] init];
startInvocations = 0;
_detectedStartUpCrash = NO;
}
}

Expand Down Expand Up @@ -382,6 +384,16 @@ + (BOOL)crashedLastRun
return SentryDependencyContainer.sharedInstance.crashReporter.crashedLastLaunch;
}

+ (BOOL)detectedStartUpCrash
{
return _detectedStartUpCrash;
}

+ (void)setDetectedStartUpCrash:(BOOL)value
{
_detectedStartUpCrash = value;
}

+ (void)startSession
{
[SentrySDK.currentHub startSession];
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentrySDK+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ SentrySDK ()
*/
@property (nonatomic, class) BOOL crashedLastRunCalled;

+ (void)setDetectedStartUpCrash:(BOOL)value;

+ (void)setAppStartMeasurement:(nullable SentryAppStartMeasurement *)appStartMeasurement;

+ (nullable SentryAppStartMeasurement *)getAppStartMeasurement;
Expand Down
14 changes: 14 additions & 0 deletions Tests/SentryTests/SentryCrash/SentryCrashReportSinkTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Nimble
import SentryTestUtils
import XCTest

Expand All @@ -20,6 +21,11 @@ class SentryCrashReportSinkTests: SentrySDKIntegrationTestsBase {

givenSdkWithHub()
}

override func tearDown() {
super.tearDown()
clearTestState()
}

func testFilterReports_withScreenShots() {
filterReportWithAttachment()
Expand Down Expand Up @@ -58,6 +64,14 @@ class SentryCrashReportSinkTests: SentrySDKIntegrationTestsBase {
XCTAssertEqual(0, fixture.dispatchQueue.dispatchAsyncCalled)
}

func testAppStartCrash_LowerBound_SetsDetectedStartUpCrash() {
fixture.crashWrapper.internalDurationFromCrashStateInitToLastCrash = 0.001

filterReportWithAttachment()

expect(SentrySDK.detectedStartUpCrash) == true
}

func testAppStartCrash_UpperBound_CallsFlush() {
fixture.crashWrapper.internalDurationFromCrashStateInitToLastCrash = 2.0

Expand Down
14 changes: 13 additions & 1 deletion Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,19 @@ class SentrySDKTests: XCTestCase {
}

func testCrashedLastRun() {
XCTAssertEqual(SentryDependencyContainer.sharedInstance().crashReporter.crashedLastLaunch, SentrySDK.crashedLastRun)
expect(SentryDependencyContainer.sharedInstance().crashReporter.crashedLastLaunch) == SentrySDK.crashedLastRun
}

func testDetectedStartUpCrash_DefaultValue() {
expect(SentrySDK.detectedStartUpCrash) == false
}

func testDetectedStartUpCrash() {
SentrySDK.setDetectedStartUpCrash(true)
expect(SentrySDK.detectedStartUpCrash) == true

SentrySDK.setDetectedStartUpCrash(false)
expect(SentrySDK.detectedStartUpCrash) == false
}

func testCaptureCrashEvent() {
Expand Down

0 comments on commit 1bdf1af

Please sign in to comment.