Skip to content

Commit 3b189e1

Browse files
authored
fix: Save app context information on app hang events before saving to disk (#6998)
* fix: Save app context information on app hang events before saving to disc * Improve testing * Update changelog * Fix PR number
1 parent 0af71b2 commit 3b189e1

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
### Fixes
1515

16+
- Save app context information (release name, dist, environment) on app hang events before saving to disk to prevent incorrect version information when an app hang turns fatal (#6998)
1617
- Disabled automatic session tracking in system extensions to prevent extension blocking and unwanted dock icon behavior (#6962) (#6962)
1718
- Fixes crash when null values are passed to `UIApplication sendAction:to:from:forEvent:` (#6970)
1819
- Fixes `user.id` not set to installationId if no user is set (#7005)

Sources/Sentry/SentryANRTrackingIntegration.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,31 @@ - (void)anrDetectedWithType:(enum SentryANRType)type
180180
[scope applyToEvent:event maxBreadcrumb:options.maxBreadcrumbs];
181181
}
182182

183+
[self applyOptions:options toEvent:event];
184+
183185
[self.fileManager storeAppHangEvent:event];
184186
#else
185187
[SentrySDK captureEvent:event];
186188
#endif
187189
}
188190

191+
- (void)applyOptions:(SentryOptions *)options toEvent:(SentryEvent *)event
192+
{
193+
// We need to apply the release name now, if the app hang turns into a fatal one
194+
// we might en up submitting a wrong version.
195+
event.releaseName = options.releaseName;
196+
197+
// Only apply dist if it wasn't set by the Scope previously
198+
if (event.dist == nil && options.dist != nil) {
199+
event.dist = options.dist;
200+
}
201+
202+
// Only apply environment if it wasn't set by the Scope previously
203+
if (event.environment == nil && options.environment != nil) {
204+
event.environment = options.environment;
205+
}
206+
}
207+
189208
- (void)anrStoppedWithResult:(SentryANRStoppedResult *_Nullable)result
190209
{
191210
#if SENTRY_HAS_UIKIT

Tests/SentryTests/Integrations/ANR/SentryANRTrackingIntegrationTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
2121
options.dsn = SentryANRTrackingIntegrationTests.dsn
2222
options.enableAppHangTracking = true
2323
options.appHangTimeoutInterval = 4.5
24+
options.releaseName = "release-name-test"
2425

2526
debugImageProvider.debugImages = [TestData.debugImage]
2627
}
@@ -364,6 +365,24 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
364365
}
365366

366367
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
368+
func testV2_ANRDetected_StoresAppHangEventInFile() throws {
369+
// Arrange
370+
options.releaseName = "my-release-name-test"
371+
options.environment = "testing-environment"
372+
options.dist = "adhoc"
373+
setUpThreadInspector()
374+
givenInitializedTracker()
375+
376+
// Act
377+
Dynamic(sut).anrDetectedWithType(SentryANRType.nonFullyBlocking)
378+
379+
// Assert
380+
let event = try XCTUnwrap(SentrySDKInternal.currentHub().client()?.fileManager.readAppHangEvent())
381+
XCTAssertEqual(event.releaseName, "my-release-name-test")
382+
XCTAssertEqual(event.environment, "testing-environment")
383+
XCTAssertEqual(event.dist, "adhoc")
384+
}
385+
367386
func testV2_ANRDetected_DoesNotCaptureEvent() throws {
368387
// Arrange
369388
setUpThreadInspector()
@@ -424,6 +443,8 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
424443
// Ensure we capture the event with an empty scope
425444
XCTAssertEqual(scope?.tags.count, 0)
426445
XCTAssertEqual(scope?.breadcrumbs().count, 0)
446+
447+
XCTAssertEqual(event?.releaseName, "release-name-test")
427448
}
428449
}
429450

@@ -472,6 +493,8 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
472493
// Ensure we capture the event with an empty scope
473494
XCTAssertEqual(scope?.tags.count, 0)
474495
XCTAssertEqual(scope?.breadcrumbs().count, 0)
496+
497+
XCTAssertEqual(event?.releaseName, "release-name-test")
475498
}
476499
}
477500

@@ -529,6 +552,8 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
529552
let breadcrumbs = try XCTUnwrap(event?.breadcrumbs)
530553
XCTAssertEqual(1, breadcrumbs.count)
531554
XCTAssertEqual("crumb", breadcrumbs.first?.message)
555+
556+
XCTAssertEqual(event?.releaseName, "release-name-test")
532557
}
533558
}
534559

@@ -566,6 +591,8 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
566591
let breadcrumbs = try XCTUnwrap(event?.breadcrumbs)
567592
XCTAssertEqual(1, breadcrumbs.count)
568593
XCTAssertEqual("crumb", breadcrumbs.first?.message)
594+
595+
XCTAssertEqual(event?.releaseName, "release-name-test")
569596
}
570597
}
571598

@@ -618,6 +645,8 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
618645
XCTAssertEqual("distinct", actualSession.distinctId)
619646
XCTAssertEqual(fixture.currentDate.date(), actualSession.timestamp)
620647
XCTAssertEqual("anr_foreground", actualSession.abnormalMechanism)
648+
649+
XCTAssertEqual(event.releaseName, "release-name-test")
621650
}
622651

623652
func testV2_ANRDetected_StopNotCalledAndCrashed_SendsNormalAppHangEvent() throws {

0 commit comments

Comments
 (0)