Skip to content

fix(phone-calls): prevent infinite reconnect loop and log WebSocket closure (#21278) - #21305

Merged
kodjima33 merged 3 commits into
BasedHardware:mainfrom
Dragonire9:fix/phone-call-transcription-reconnect-counter
Oct 13, 2026
Merged

kodjima33 merged 3 commits into
BasedHardware:mainfrom
Dragonire9:fix/phone-call-transcription-reconnect-counter

Conversation

@Dragonire9

@Dragonire9 Dragonire9 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

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 = 0 was 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:

  1. onDone triggered _scheduleReconnect(), which incremented _wsReconnectAttempts from 0 to 1 and scheduled a reconnect in 1s.
  2. When the reconnect timer fired and invoked _connectTranscriptionSocket(), _wsReconnectAttempts = 0 reset the counter back to 0.
  3. This created an infinite loop (0 -> 1 -> 0 -> 1...) where the backoff remained at 1s, _maxWsReconnectAttempts (10) was never reached, the UI stayed stuck on "Reconnecting transcription…", and the state never transitioned to TranscriptionStatus.failed.
  4. Additionally, onDone did not log the socket's closeCode or closeReason.

Key Changes

  1. Reset on Accepted Connection Only (app/lib/providers/phone_call_provider.dart):

    • Moved _wsReconnectAttempts = 0; to the first message handler in stream.listen where _wsAccepted = true is set.
    • Removed the premature reset right after creating the socket listener.
    • Now, consecutive failed connection attempts monotonically increment _wsReconnectAttempts, applying proper exponential backoff (1s, 2s, 4s...) until _maxWsReconnectAttempts is reached, transitioning _transcriptionStatus to failed.
    • Logged closeCode and closeReason in onDone for transparent diagnostics.
    • Added test inspection getters @visibleForTesting.
  2. Automated Regression Coverage (app/test/providers/phone_call_provider_test.dart):

    • Enhanced _FakeWebSocketChannel with closeCode and closeReason test properties.
    • Added unit test verifying that unaccepted socket closures do not reset _wsReconnectAttempts, and that receiving the first message resets the counter to 0 with TranscriptionStatus.active.
    • Added unit test verifying that exhausting max reconnect attempts (10) transitions transcriptionStatus to TranscriptionStatus.failed.

View guided diff

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = 0 into 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 after stream.listen is 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/closeReason in onDone before nulling _transcriptionSocket is a nice diagnostic addition for exactly the server-rejection case in the issue.
  • The three new @visibleForTesting hooks (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 Git-on-my-level added the flutter flutter work label Oct 12, 2026
@Dragonire9

Copy link
Copy Markdown
Contributor Author

@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 bf86f1e:

  • Adjusted the simulation loop to run 10 cycles (for (var i = 0; i < 10; i++)).
  • The 10th reconnect cycle's immediate onDone triggers _scheduleReconnect() when _wsReconnectAttempts == 10, cleanly entering the guard and transitioning to TranscriptionStatus.failed.

@Git-on-my-level
Git-on-my-level dismissed their stale review October 13, 2026 14:07

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.

@Git-on-my-level

Copy link
Copy Markdown
Member

The follow-up commit resolves the off-by-one from my earlier review — thanks for the quick turnaround.

Verified against the current head (bf86f1e):

  • app/test/providers/phone_call_provider_test.dart — the give-up test now runs 10 failed reconnect cycles and drops the no-op trailing close. Since each re-listen on the closed broadcast controller fires onDone immediately, the 10th cycle's onDone finds _wsReconnectAttempts == 10 and hits the >= _maxWsReconnectAttempts guard in _scheduleReconnect(), landing on TranscriptionStatus.failed as intended. The counter arithmetic (10 increments, guard checked pre-increment) checks out against the implementation.
  • The accept/reset test also checks out: 0 -> 1 on the unaccepted 1008 close, stays 1 through the next connect (no premature reset), and returns to 0 only after the accepted 'ping' — which is safely short-circuited in _handleTranscriptionMessage, so nothing downstream of it can throw.
  • app/lib/providers/phone_call_provider.dart — production change is unchanged from the earlier verified state and remains correct: the reset lives next to _wsAccepted = true, and closeCode/closeReason are logged in onDone before _transcriptionSocket is nulled.

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 flutter test test/providers/phone_call_provider_test.dart run (locally or via CI) would close that loop — leaving the merge decision to a maintainer with that in mind.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scoped, well-tested fix for #21278 (phone-call transcription stuck on reconnect after immediate WS close); not already on main; confidence 5/5.

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

Labels

flutter flutter work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Phone Call: live transcription stuck on "Reconnecting transcription…" for entire call; never shows failure

3 participants