Skip to content

fix(core): [Unhandled Sessions 2] Make session cache writes session-id aware - #5917

Merged
buenaflor merged 3 commits into
feat/unhandled-sessionsfrom
feat/unhandled-sessions-cache
Aug 10, 2026
Merged

fix(core): [Unhandled Sessions 2] Make session cache writes session-id aware#5917
buenaflor merged 3 commits into
feat/unhandled-sessionsfrom
feat/unhandled-sessions-cache

Conversation

@buenaflor

@buenaflor buenaflor commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

PR Stack (Unhandled Sessions)


📜 Description

Makes the session file cache aware of which session it is acting on.

Two paths in EnvelopeCache.storeInternal previously ignored session identity:

  • SessionEnd deleted session.json unconditionally. A delayed End(A) arriving after a newer session B was persisted would delete B's file.
  • SessionStart always rotated session.json into previous_session.json and overwrote it. A delayed Start(A) arriving after A's own state had already advanced on disk would overwrite the newer snapshot.

Both now compare session ids (and, for SessionEnd, start times) before deleting or rotating. A stale end only preserves the current file when the current session is genuinely newer — a different id and a later start time. Everything else keeps the existing behaviour, including malformed envelopes and unreadable session files, which still delete as before.

Also adds EnvelopeCache.persistCurrentSession(Session) so a caller can flush the active session to disk outside the envelope-store path, and extracts readSessionFromEnvelope / readSessionFromDisk helpers.

💡 Motivation and Context

Needed by the next PR in the stack, which mutates the live session in place (marking it pending-unhandled) and persists it so the marker survives process death. Without session-id awareness, an unrelated in-flight SessionEnd or SessionStart could clobber that snapshot.

In practice this only changes observable behaviour for sessions carrying the pending-unhandled marker from PR 1 — the preserve branch requires isPendingUnhandled() — so existing SDK flows are unaffected.

💚 How did you test it?

EnvelopeCacheTest grew coverage for delayed same-id SessionStart preserving a newer pending or higher-error snapshot, different-id starts still rotating, matching and mismatching SessionEnd, null session ids on both paths, malformed envelopes, and unreadable session files. Also verified the pre-existing AbnormalExit and NativeCrashExit recovery paths still win over a pending marker. 36 tests, all passing.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

The capture API that sets the marker, in the next PR in this stack.

#skip-changelog

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

SessionEnd previously deleted session.json unconditionally and
SessionStart always rotated it. A delayed end or start could therefore
drop a newer session snapshot. Both paths now compare session ids and
start times before deleting or rotating, and a new persistCurrentSession
lets callers flush the active session to disk.

Co-authored-by: Cursor <cursoragent@cursor.com>
@sentry

sentry Bot commented Aug 10, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.52.0 (1) release

⚙️ sentry-android Build Distribution Settings

Base automatically changed from feat/unhandled-sessions-protocol to feat/unhandled-sessions August 10, 2026 09:40
@buenaflor
buenaflor merged commit 68ca0a1 into feat/unhandled-sessions Aug 10, 2026
55 of 57 checks passed
@buenaflor
buenaflor deleted the feat/unhandled-sessions-cache branch August 10, 2026 09:40
@buenaflor
buenaflor restored the feat/unhandled-sessions-cache branch August 10, 2026 09:48
@buenaflor

Copy link
Copy Markdown
Contributor Author

Not actually merged into main. This PR was auto-closed when the collection branch feat/unhandled-sessions was accidentally fast-forwarded to the tip of the stack, which made GitHub consider every stack branch merged into its base.

The collection branch has been restored to its empty commit and the stack rebuilt unchanged. This PR continues as #5920.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant