Skip to content

Commit 7b14762

Browse files
denraseitaybrephilprime
authored
fix: Add foreground state to app context in app hang events (#7801)
* fix: Ensure app hangs have foreground app context Set app.in_foreground and app.is_active when creating new app hang events in SentryHangTrackingIntegration, so persisted and fatal app hangs always carry the expected foreground context. * add cl entry * Update CHANGELOG.md Co-authored-by: Philip Niedertscheider <phil.niedertscheider@sentry.io> --------- Co-authored-by: Itay Brenner <itay.brenner@sentry.io> Co-authored-by: Philip Niedertscheider <phil.niedertscheider@sentry.io>
1 parent 77a335b commit 7b14762

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Keep replayType as `buffer` for Session Replay triggered by an error (#7804)
1313
- Fix race condition in scope observer notifications causing EXC_BAD_ACCESS during cold launch (#7807)
1414
- Unsubscribe to system event during background to avoid reporting breadcrumbs with wrong timestamps on return to foreground (#7702)
15+
- Add foreground state to app context in app hang events (#7801)
1516

1617
## 9.10.0
1718

Sources/Swift/Integrations/SentryHangTrackingIntegration.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ final class SentryHangTrackingIntegration<Dependencies: HangTrackingIntegrationS
161161
mechanism.data = [sentryANRMechanismDataAppHangDuration: appHangDurationInfo]
162162
let scope = SentrySDKInternal.currentHub().scope
163163
scope.applyTo(event: event, maxBreadcrumbs: options.maxBreadcrumbs)
164+
addAppState(to: event)
164165
apply(options: SentrySDK.startOption, toEvent: event)
165166
fileManager.storeAppHang(event)
166167
#else
@@ -204,6 +205,15 @@ final class SentryHangTrackingIntegration<Dependencies: HangTrackingIntegrationS
204205
}
205206

206207
#if (os(iOS) || os(tvOS) || os(visionOS)) && !SENTRY_NO_UI_FRAMEWORK
208+
private func addAppState(to event: Event) {
209+
var context = event.context ?? [:]
210+
var appContext = context["app"] ?? [:]
211+
appContext["in_foreground"] = true
212+
appContext["is_active"] = true
213+
context["app"] = appContext
214+
event.context = context
215+
}
216+
207217
private func captureStoredAppHangEvent() {
208218
dispatchQueueWrapper.dispatchAsync { [weak self] in
209219
guard let self else { return }

Tests/SentryTests/Integrations/ANR/SentryHangTrackingIntegrationTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ class SentryHangTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
379379
XCTAssertEqual(event.releaseName, "my-release-name-test")
380380
XCTAssertEqual(event.environment, "testing-environment")
381381
XCTAssertEqual(event.dist, "adhoc")
382+
let appContext = try XCTUnwrap(event.context?["app"] as? [String: Any])
383+
XCTAssertEqual(true, appContext["in_foreground"] as? Bool)
384+
XCTAssertEqual(true, appContext["is_active"] as? Bool)
382385
}
383386

384387
func testV2_ANRDetected_DoesNotCaptureEvent() throws {

0 commit comments

Comments
 (0)