Repository navigation
fix(phone-calls): prevent infinite reconnect loop and log WebSocket closure (#21278) - #21305
Conversation
…ackoff and max attempts (BasedHardware#21278)
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks for the clean diagnosis — the root-cause analysis in the description matches the code, and the production fix is the right shape. Verified against app/lib/providers/phone_call_provider.dart at the PR head:
- Moving
_wsReconnectAttempts = 0into the first-message handler (next to_wsAccepted = true) is correct: the counter now only resets when the server actually accepts the connection, and the premature reset right afterstream.listenis gone. With the counter incrementing monotonically in_scheduleReconnect(), the give-up guard (_wsReconnectAttempts >= _maxWsReconnectAttempts→TranscriptionStatus.failed) can now actually be reached — that breaks the infinite 1s reconnect loop from #21278. - Logging
closeCode/closeReasoninonDonebefore nulling_transcriptionSocketis a nice diagnostic addition for exactly the server-rejection case in the issue. - The three new
@visibleForTestinghooks (connectTranscriptionSocketForTesting,wsReconnectAttemptsForTesting,wsReconnectTimerForTesting) are delegate/read-only and don't alter production behavior.
One concrete issue in the new tests blocks for now, in app/test/providers/phone_call_provider_test.dart:
"reconnect attempts give up with failed status after max attempts reached" is off by one and should fail when actually run. _scheduleReconnect() checks _wsReconnectAttempts >= 10 before incrementing, so the failed transition only happens on the 11th reconnect-scheduling event. In the test only 10 events ever occur: after the first close, each connectTranscriptionSocketForTesting() re-listens to the already-closed broadcast incoming controller, so onDone fires immediately and produces one increment per cycle. The 9-iteration loop therefore lands the counter at 9, the final iteration of the loop brings it to 10 and schedules one more reconnect (status reconnecting), and the trailing socket.incoming.close() is a no-op on a closed broadcast controller — no 11th onDone ever fires, so the status stays reconnecting rather than failed. I simulated this exact sequence (broadcast controller + the provider's schedule/increment order) and it ends at attempts=10, status=reconnecting.
The minimal fix is to run the loop 10 times instead of 9 — then the 10th iteration's immediate onDone triggers the guard and flips the status to failed (also verified by simulation). Making the give-up check fire the moment the counter reaches the max (check after increment) would also work but changes scheduling behavior more broadly; the loop-count fix is the safer, smaller change.
Worth noting: the checks on this PR only include the AI code reviewer — the Flutter test suite doesn't appear to have run on this head, which is presumably why this wasn't caught. After adjusting the loop, running flutter test test/providers/phone_call_provider_test.dart locally would confirm.
The other new test ("immediate socket close does not reset ws reconnect counter; accepted message resets it") is correct — the counter arithmetic (0 → 1 on unaccepted close, stays 1 on reconnect before accept, 0 after accepted message) checks out against the implementation.
The provider-side fix itself looks good — once the test loop count is corrected (and ideally with the Flutter suite green on the head), this should be ready.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
|
@Git-on-my-level Thank you for the sharp catch and clear explanation on the off-by-one reconnect sequence! Updated the test in commit
|
Dismissing as resolved: the off-by-one in the give-up regression test (9-iteration loop + no-op trailing close never reaching failed status) is fixed at bf86f1e — the loop now runs 10 failed reconnect cycles and the test reaches the max-attempts guard as intended.
|
The follow-up commit resolves the off-by-one from my earlier review — thanks for the quick turnaround. Verified against the current head (bf86f1e):
One remaining gap: no checks are reported on this branch, so the Flutter test suite has not actually executed these new tests on this head. A by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
Summary
Resolves #21278
Fixes an issue where phone call live transcription gets stuck indefinitely on "Reconnecting transcription…" when the transcription WebSocket is closed immediately after connect by the server (e.g. server rejection, code 1008, auth token issue, etc.).
Root Cause
In
PhoneCallProvider._connectTranscriptionSocket(),_wsReconnectAttempts = 0was executed right after attaching the stream listener, before the server actually accepted the connection or delivered any message.When a connection was immediately closed or rejected by the server:
onDonetriggered_scheduleReconnect(), which incremented_wsReconnectAttemptsfrom 0 to 1 and scheduled a reconnect in 1s._connectTranscriptionSocket(),_wsReconnectAttempts = 0reset the counter back to 0._maxWsReconnectAttempts(10) was never reached, the UI stayed stuck on "Reconnecting transcription…", and the state never transitioned toTranscriptionStatus.failed.onDonedid not log the socket'scloseCodeorcloseReason.Key Changes
Reset on Accepted Connection Only (
app/lib/providers/phone_call_provider.dart):_wsReconnectAttempts = 0;to the first message handler instream.listenwhere_wsAccepted = trueis set._wsReconnectAttempts, applying proper exponential backoff (1s, 2s, 4s...) until_maxWsReconnectAttemptsis reached, transitioning_transcriptionStatustofailed.closeCodeandcloseReasoninonDonefor transparent diagnostics.@visibleForTesting.Automated Regression Coverage (
app/test/providers/phone_call_provider_test.dart):_FakeWebSocketChannelwithcloseCodeandcloseReasontest properties._wsReconnectAttempts, and that receiving the first message resets the counter to 0 withTranscriptionStatus.active.transcriptionStatustoTranscriptionStatus.failed.