Skip to content

fix: keep the pre-connect audio buffer through a region failover on connect - #1215

Merged
hiroshihorie merged 6 commits into
mainfrom
hiroshi/preconnect-audio-survives-failover
Nov 17, 2026
Merged

hiroshihorie merged 6 commits into
mainfrom
hiroshi/preconnect-audio-survives-failover

Conversation

@hiroshihorie

@hiroshihorie hiroshihorie commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Problem

When the first connect attempt fails, the engine reports a disconnect before Room.connect retries another Cloud region. The Room handled that disconnect like any other and reset the pre-connect audio buffer. The retry then connected with a buffer that was no longer recording, so the join response found no microphone track to publish. The participant ended up in the room, joined unmuted, with no microphone until they toggled it.

Since 2.13.0 a 403 rejection also fails over instead of failing the connect, so a first attempt that used to fail visibly now connects silently without the microphone.

Fix

  • Room.connect records that a connect attempt is running.
  • The engine disconnect handler passes that state to _cleanUp, which skips the pre-connect buffer reset when the disconnect belongs to a connect attempt that may still be retried.
  • A disconnect the app asked for always resets the buffer, also during a connect. Otherwise a disconnect() called while the connect is in flight would leave the buffer recording, and the microphone would be published if that connect completed.
  • When a failed attempt was handed to the failover path and no attempt connects (no region left, the region fetch fails, or the retry fails too), connect() stops the buffer with the error before it throws, the way withPreConnectAudio ends a failed operation, and then resets it. Stopping with the error is what also stops the native recorder.
  • A disconnect outside of a connect attempt still resets the buffer, as before.

Tests

test/core/preconnect_failover_test.dart covers each way connect() can end. Every test fails when the part of the fix it guards is removed.

Case Buffer afterwards
first socket connect fails, validate answers 403, the retry connects still recording (fails on main)
the socket is rejected while validate succeeds, the retry connects still recording (fails on main)
connect fails with no failover (not a Cloud url) reset
the failover has no region left stopped with the connect error, then reset
the failover attempt fails too stopped with the connect error, then reset
disconnect() is called while the socket handshake is in flight reset
a server initiated leave while connected reset

Test-only additions: Room.regionUrlProviderForTesting, PreConnectAudioBuffer.markRecordingForTesting, sdkHttpClientFactoryForTesting, and two switches on the websocket mock: connectErrorOnce, so a first attempt fails and the retry gets through, and connectGate, so a handshake can be held in flight.

flutter analyze, format and import order are clean, and the full unit suite is green (595 tests).

Verified on devices

The e2e-flutter scenario "Local mic transmits, published through pre-connect audio".

iPhone 16 Pro (iOS 27.2) against a region-pinned LiveKit Cloud project. There the first connect attempt is rejected with a 403 and fails over to another region on every run.

SDK Result
main (a40ca98) failed 5 of 5: the microphone track is never published, because the buffer is reset during the failover
main before #1280 (4dae650) failed, so this is not a #1280 regression
this PR at ee3fbc8 passed 6 of 6: failover happens, the pre-connect track is published, and microphone audio reaches the subscriber

The iPhone runs predate the last two commits (f9c394b, ec7b96d), which only change what happens when a connect is cancelled or its failover gives up.

macOS against a local server, at ec7b96d, with the failover forced locally (livekit/e2e-flutter#33):

  • the microphone set passes 10 of 10, including pre-connect audio with and without a failover
  • without the fix, the failover variant fails: the retry joins with no microphone

Changes since the first version

  • Rebased onto main (a40ca98). The only conflict was in test/mock/websocket_mock.dart: main added sentData next to this PR's connectErrorOnce, and both are kept.
  • Added the changeset .changes/preconnect-audio-survives-failover.
  • e399ee3: markRecordingForTesting had been inserted between startRecording and its doc comment; moved it so the documentation stays on startRecording.
  • f9c394b: the two reset rules above for a cancelled connect and a failover that gives up, with the five new test cases.
  • ec7b96d: when a failover gives up, stop the buffer with the error instead of a plain reset, which left the native recorder running (found by the automated review of f9c394b).

🤖 Generated with Claude Code

@github-actions

This comment has been minimized.

hiroshihorie and others added 3 commits November 17, 2026 00:50
…onnect

When the first connect attempt fails, the engine reports a disconnect
before Room.connect retries another region. The Room handled that
disconnect like any other and reset the pre-connect audio buffer, so the
retry connected without a recording buffer and the join response found
nothing to publish. The participant ended up in the room with no
microphone until they toggled it, even though they had joined unmuted.

Track whether Room.connect is running and leave the buffer alone when the
disconnect belongs to a connect attempt. A connect that fails for good
still ends with the caller's own cleanup, and a disconnect outside of a
connect attempt still resets the buffer.

Tests: a failover where the first socket connect fails and the validate
call answers 403 now keeps the buffer recording, and a server initiated
leave still resets it. Adds small test hooks for the region provider, the
buffer's recording state, the HTTP client and a fail-once websocket mock.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hiroshihorie
hiroshihorie force-pushed the hiroshi/preconnect-audio-survives-failover branch from f24ccda to ee3fbc8 Compare November 16, 2026 16:55
@hiroshihorie
hiroshihorie marked this pull request as ready for review November 16, 2026 17:17

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

hiroshihorie and others added 2 commits November 17, 2026 12:45
markRecordingForTesting had been inserted between startRecording and its
doc comment, so the test helper inherited the documentation and
startRecording lost it. Move the helper above the comment.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…failover gives up

Keeping the buffer for every disconnect during connect() also kept it in two
cases where nothing should be published:

- disconnect() called while the connect is in flight. The buffer stayed
  recording, and the microphone was published if that connect completed.
  A client initiated disconnect now resets the buffer as before.
- a failed attempt that is eligible for failover but ends with no retry
  that connects (no region left, the region fetch fails, or the retry fails
  too). connect() now resets the buffer itself before it throws.

Tests cover each way connect() can end: failover on a 403 from validate,
failover on a rejected socket, failure with no failover, no region left,
a failed retry, disconnect() during connect, and a server initiated leave.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
devin-ai-integration[bot]

This comment was marked as resolved.

…es up

The previous commit reset the buffer when a failover ended without a
connection. A plain reset does not stop the native recorder, and it ran
before withPreConnectAudio could stop the buffer with the error, so that
stop found nothing to do and the microphone kept recording.

connect() now stops the buffer with the error itself in that case, the
way withPreConnectAudio ends a failed operation, and then resets it. The
tests check that agentReadyFuture completes with the connect error, which
only the error-aware stop does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@hiroshihorie
hiroshihorie merged commit 298cc62 into main Nov 17, 2026
16 checks passed
@hiroshihorie
hiroshihorie deleted the hiroshi/preconnect-audio-survives-failover branch November 17, 2026 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant