fix(mobile): reconnect on resume instead of trusting a stale status
Summary
On mobile, the relay session could come back from a real background stint believing it was still connected while sitting on a dead socket. No events arrived until some later write surfaced the error — which is why replies would appear all at once the moment the user typed something.
onAppResumed() returned early whenever state.status was already connected, on the assumption that a still-connected status meant the socket survived the 5-second background grace window:
// If still connected, nothing to do — the socket survived the background
// grace window.
if (state.status == SessionStatus.connected) return;
That assumption does not hold once the OS suspends the process. The grace timer never fires, and a socket torn down by the platform or by carrier NAT never delivers its onDisconnected callback, so the status is stale in exactly the case where it matters.
This change tracks whether the app actually backgrounded (onAppPaused() fired) and only trusts a connected status when it did not:
- Resume from
inactive— a Control Centre swipe or a notification banner — never callsonAppPaused(), so that path keeps its live socket and gains no reconnect churn. - Resume after a real background stint reconnects regardless of the reported status. When the status still claims
connected, the old socket is torn down first viareconnect()so the replacement isn't racing a half-open one.
Reconnecting is idempotent and replays missed events, so an occasional redundant reconnect is far cheaper than a silent stall.
Related issue
None found — searched open issues and PRs for existing reports of stale mobile sockets on resume.
Testing
Two new tests in mobile/test/shared/relay/relay_session_test.dart, covering both directions of the condition:
reconnects on resume even when the status still claims connected— the regression itselfdoes not rebuild the socket when the app never backgrounded— guards against trading a silent stall for reconnect churn on every Control Centre swipe
Full mobile suite on the rebased branch (not just the touched file):
dart format --output=none --set-exit-if-changed . → Formatted 331 files (0 changed)
flutter analyze → No issues found! (ran in 15.8s)
flutter test → +1024 ~1: All tests passed!
No UI change — behaviour only.