Skip to content

Commit db533ee

Browse files
authored
Fix: Don't run onCrashedLastSession for nil Events (#3785)
Don't run onCrashedLastSession for nil Events. This could happen if beforeSend returns nil
1 parent 2cec7eb commit db533ee

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

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+
- Don't run onCrashedLastSession for nil Events (#3785)
8+
39
## 8.22.4
410

511
### Fixes

Sources/Sentry/SentryClient.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
664664
}
665665
}
666666

667-
if (isCrashEvent && nil != self.options.onCrashedLastRun && !SentrySDK.crashedLastRunCalled) {
667+
if (event != nil && isCrashEvent && nil != self.options.onCrashedLastRun
668+
&& !SentrySDK.crashedLastRunCalled) {
668669
// We only want to call the callback once. It can occur that multiple crash events are
669670
// about to be sent.
670671
SentrySDK.crashedLastRunCalled = YES;

Tests/SentryTests/SentryClientTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,22 @@ class SentryClientTest: XCTestCase {
14441444
XCTAssertTrue(onCrashedLastRunCalled)
14451445
}
14461446

1447+
func testOnCrashedLastRun_DontRunIfBeforeSendReturnsNill() {
1448+
let event = TestData.event
1449+
1450+
var onCrashedLastRunCalled = false
1451+
fixture.getSut(configureOptions: { options in
1452+
options.beforeSend = { _ in
1453+
return nil
1454+
}
1455+
options.onCrashedLastRun = { _ in
1456+
onCrashedLastRunCalled = true
1457+
}
1458+
}).captureCrash(event, with: fixture.session, with: fixture.scope)
1459+
1460+
XCTAssertFalse(onCrashedLastRunCalled)
1461+
}
1462+
14471463
func testOnCrashedLastRun_WithTwoCrashes_OnlyInvokeCallbackOnce() {
14481464
let event = TestData.event
14491465

0 commit comments

Comments
 (0)