Repository navigation
fix: pause and resume sessions when mobile apps move to the background - #5863
Conversation
Closes #5842 Co-Authored-By: Claude <noreply@anthropic.com>
Delay the Android background-launch pause past a cold start, resume on DidBecomeActive on iOS, use the Lock type, and test the platform trackers. Co-Authored-By: Claude <noreply@anthropic.com>
…enter Co-Authored-By: Claude <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5863 +/- ##
==========================================
+ Coverage 76.67% 76.71% +0.04%
==========================================
Files 528 531 +3
Lines 19527 19757 +230
Branches 3840 3899 +59
==========================================
+ Hits 14972 15157 +185
- Misses 3670 3715 +45
Partials 885 885 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
No red flags from my clunker (I haven't read in detail yet)... the only question it raised is a discrepancy between how we do this in .NET vs the native SDKs:
Given that this behaviour predates this PR, probably not something to address/change in this PR but more a question of whether we want to change this for consistency in a follow up @ric-oliv @bitsandfoxes ? |
|
Looking good! I'd actually do the follow up here... The work needed is not too big, and it removes both background-launch checks. |
Background launches (a push, a broadcast, a job, a background fetch) no longer start a session, matching sentry-java and sentry-cocoa. This replaces the Android 5-second and iOS at-init background checks. The previous run's session is still recovered at init, split out of Hub.StartSession, and its file is deleted once recovered. Otherwise a background launch that never starts a session would send it again on the next launch, as crashed if that background run crashed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ric-oliv / @bitsandfoxes I added a commit for this. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 77e0624. Configure here.
…not attached Apps that remove androidx.startup's InitializationProvider never attach ProcessLifecycleOwner, so ON_START never fires and no session would start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Thanks @jamescrosswell! One more thing:
A crash before the first foreground no longer counts as a crashed session. On MAUI that window covers the App and main page constructors. I threw from the App constructor on an Android emulator and an iOS simulator, and both sent the crash event but no session update. sentry-java and sentry-cocoa work the same way, so I'm fine with it. I'd still list it under the behavior changes and add a ### Changelog Entry, since session counts and crash-free rates can shift after an upgrade. Maybe something like this:
### Changelog Entry
- Sessions on iOS, Mac Catalyst and Android now pause when the app moves to the background. A background longer than `AutoSessionTrackingInterval` ends the session, and an app killed in the background reports `exited` instead of `abnormal`.
- On those platforms, sessions now start when the app first comes to the foreground, the same as in sentry-java and sentry-cocoa. Background launches no longer start a session, and a crash before the first foreground no longer counts as a crashed session.Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@ric-oliv Thanks. I added the crash before the first foreground to "Behaviour changes worth noting", added the I also re-ran launch, 45s background then foreground, and background kill then relaunch against a local envelope logger, on an Android emulator and an iOS simulator. Both gave the expected session updates. The PR description lists what has not been re-run. |
…e foreground Matches SentryAndroid.init in sentry-java: a foreground launch has a session from init, so a crash before ON_START counts as a crashed session, and a background launch never starts one, even when ProcessLifecycleOwner is not attached. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@ric-oliv One correction since your review: sentry-java doesn't wait for the first foreground in the way we both assumed. 947d62d does the same on Android:
I reworded the second |
|
Good catch, LGMT! |

On iOS, Mac Catalyst and Android, the SDK starts the session when the app comes to the foreground, pauses it when the app goes to the background and resumes it when the app comes back. A background longer than
AutoSessionTrackingIntervalends the session asexitedat the time it was paused and starts a new one. An app killed while in the background recovers its session asexitedon the next launch instead ofabnormal. A process launched in the background (a push, a broadcast, a job, a background fetch) doesn't start a session, the same as sentry-java and sentry-cocoa.Closes #5842
Changelog Entry
AutoSessionTrackingIntervalends the session, and an app killed in the background reportsexitedinstead ofabnormal.How it works
AutoSessionTrackingIntegrationrecovers the previous run's session at init. On mobile it then subscribes to the app lifecycle, which starts the session on the first foreground; other platforms still start it at init. The Hub disposes the subscription with the integration. This covers MAUI and plainnet-android/net-iosapps;Sentry.Mauiis unchanged.ProcessLifecycleOwnerobserver pauses onON_STOP, and onON_STARTresumes the session or starts one if there is none. It is process-wide, so switching between activities doesn't pause. LikeSentryAndroid.initin sentry-java, it also starts a session at init if the process has foreground importance, so a foreground launch has a session from the start. If the observer can't be added, orProcessLifecycleOwnerwas never initialized (the app removed androidx.startup'sInitializationProvider), a foreground launch gets a session that doesn't pause. A background launch gets none, even if the user opens the app later, the same as sentry-java.DidEnterBackgroundandWillTerminatepause;WillEnterForegroundandDidBecomeActiveresume or start. An app that is alreadyActiveat init (the SDK initialized late) starts its session straight away.AutoSessionTrackingis off, so nothing subscribes.GlobalSessionManager:crashedif that background run crashed.abnormal.errors: 1.Behaviour changes worth noting
SentrySdk.IsSessionActiveisfalsebetweenInitand the first foreground on iOS and Mac Catalyst, and after a background launch on Android.exitedsessions.Appand main page constructors. Session counts and crash-free rates can shift after an upgrade. sentry-cocoa works the same way. On Android, the same crash during a foreground launch still counts ascrashed.Verified on devices
MAUI sample pointed at a local envelope logger, default 30s interval. On
main, a Pixel 6 reproduces the issue (no split after 45s, background kill recovers asabnormalwitherrors: 0). With the first version of this change, before sessions waited for the foreground:exitedat pause time + new sessionexitedabnormalabnormal,errors: 1exited,errors: 1Re-run after the foreground-start change, with the same app and envelope logger. Launch, 45s background then foreground, and background kill then relaunch gave the same results on an Android emulator (API 37, 947d62d) and an iOS 26.5 simulator (34a4de1). The other rows haven't been re-run.
Also on the Android emulator at 947d62d:
InitializationProviderremovedBroadcastReceiver)exitedabnormalAppconstructorcrashedsessionBackground launches haven't been run on iOS.
🤖 Generated with Claude Code