Skip to content

fix(amplitude-session): recover lastEventTime on partial persist#1198

Open
abueide wants to merge 2 commits intomasterfrom
fix/ios-zero-second-sessions
Open

fix(amplitude-session): recover lastEventTime on partial persist#1198
abueide wants to merge 2 commits intomasterfrom
fix/ios-zero-second-sessions

Conversation

@abueide
Copy link
Copy Markdown
Contributor

@abueide abueide commented Apr 7, 2026

Summary

Fix 0-second sessions in Amplitude Session plugin on iOS when Background Fetch is enabled.

Changes

  • Add recovery logic in startNewSessionIfNecessary() for partial AsyncStorage persistence
  • Recover lastEventTime from sessionId when lastEventTime === -1 but sessionId >= 0
  • Remove lastEventTime === -1 from isSessionExpired check (now handled by recovery)
  • Remove incorrect 1-second timestamp guard from onForeground()
  • Add 8 hypothesis-driven tests for 0-second session reproduction

Why

Plugin persists sessionId and lastEventTime via independent fire-and-forget writes. If app is killed before all writes complete (common during iOS Background Fetch), sessionId may persist while lastEventTime doesn't. On next launch, lastEventTime === -1 with valid sessionId falsely triggers session expiration, ending the just-created session immediately.


🤖 Generated with Claude Code

abueide and others added 2 commits April 7, 2026 11:25
… Fetch

iOS Background Fetch can briefly trigger AppState 'active' without user
interaction, causing rapid session creation/destruction cycles. Add a
1-second timestamp guard in onForeground() to prevent new sessions from
starting within 1 second of the last session creation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace incorrect 1-second timestamp guard with proper root cause fix.

The 0-second session bug is caused by non-atomic AsyncStorage persistence:
when the app is killed (e.g. during iOS Background Fetch), sessionId may
persist while lastEventTime does not. On relaunch, lastEventTime === -1
with a valid sessionId falsely triggers session expiration, ending the
just-created session immediately.

The fix recovers lastEventTime from sessionId when partial persistence
is detected, preventing the false expiration. Also adds hypothesis-driven
reproduction tests that verify the fix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@abueide abueide changed the title fix(amplitude-session): prevent 0-second sessions from iOS Background Fetch fix(amplitude-session): recover lastEventTime on partial persist Apr 7, 2026
@abueide abueide added bug Bug fix review-needed needs-review Ready for review and removed review-needed labels Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bug fix needs-review Ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant