Skip to content

fix(app): resync session stream after iOS PWA returns from background#26471

Open
ysm-dev wants to merge 1 commit intoanomalyco:devfrom
ysm-dev:fix/ios-pwa-stream-reconnect
Open

fix(app): resync session stream after iOS PWA returns from background#26471
ysm-dev wants to merge 1 commit intoanomalyco:devfrom
ysm-dev:fix/ios-pwa-stream-reconnect

Conversation

@ysm-dev
Copy link
Copy Markdown
Contributor

@ysm-dev ysm-dev commented May 9, 2026

Issue for this PR

Closes #17769

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When the web app runs as a PWA on iOS and is backgrounded, Safari suspends JS and quietly closes the SSE connection. On resume the existing visibility heuristic skipped reconnect because the last event timestamp was within the heartbeat window right before suspension, so the session UI stayed frozen until the user manually refreshed.

Changes:

  • `global-sdk.tsx`: track the `hidden` flag explicitly; on `visibilitychange` to visible (and `pageshow` with `event.persisted` for bfcache), force-reconnect with a 1s debounce. Detect successful reconnects (`server.connected` arriving while already connected) and emit a new `event.reconnect`.
  • `session.tsx`: subscribe to `event.reconnect` and force-resync the active session messages. The directory bootstrap re-fetches the session list but not message bodies, so events arriving during the gap would otherwise be lost.
  • `sync.tsx`: extend `runInflight` so a `force: true` call still re-runs after a non-force task currently in flight (queues after success or failure). Wired into `session.sync()`.
  • `sync-optimistic.test.ts`: tests for the new `runInflight` behavior.

How did you verify your code works?

Manual: installed the web app to home screen on iOS, started a streaming session, switched apps for ~30s, returned — the stream resumes and the session catches up without a refresh.

Automated:

  • `bun test src/context/sync-optimistic.test.ts` from `packages/app` (7 pass)
  • `bun typecheck` from `packages/app`

Screenshots / recordings

N/A — no UI surface changed.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

iOS suspends JS and closes the SSE connection when the PWA is backgrounded. The existing visibility heuristic skipped reconnect because the last event was recent right before suspension, leaving the session frozen. Force-reconnect on visibility/pageshow, detect successful reconnects via repeated server.connected, and resync the active session messages so events emitted during the gap are recovered.
@ysm-dev ysm-dev requested a review from adamdotdevin as a code owner May 9, 2026 03:39
@github-actions github-actions Bot added contributor needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels May 9, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 9, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session state stale after device sleep/resume — heartbeat mismatch causes premature SSE disconnect

1 participant