Repository navigation
fix: keep the pre-connect audio buffer through a region failover on connect - #1215
Merged
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
…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
force-pushed
the
hiroshi/preconnect-audio-survives-failover
branch
from
November 16, 2026 16:55
f24ccda to
ee3fbc8
Compare
hiroshihorie
marked this pull request as ready for review
November 16, 2026 17:17
hiroshihorie
requested review from
changt,
cloudwebrtc and
xianshijing-lk
as code owners
November 16, 2026 17:17
There was a problem hiding this comment.
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)
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>
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the first connect attempt fails, the engine reports a disconnect before
Room.connectretries 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.connectrecords that a connect attempt is running._cleanUp, which skips the pre-connect buffer reset when the disconnect belongs to a connect attempt that may still be retried.disconnect()called while the connect is in flight would leave the buffer recording, and the microphone would be published if that connect completed.connect()stops the buffer with the error before it throws, the waywithPreConnectAudioends a failed operation, and then resets it. Stopping with the error is what also stops the native recorder.Tests
test/core/preconnect_failover_test.dartcovers each wayconnect()can end. Every test fails when the part of the fix it guards is removed.main)main)disconnect()is called while the socket handshake is in flightTest-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, andconnectGate, 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.
main(a40ca98)mainbefore #1280 (4dae650)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):
Changes since the first version
main(a40ca98). The only conflict was intest/mock/websocket_mock.dart:mainaddedsentDatanext to this PR'sconnectErrorOnce, and both are kept..changes/preconnect-audio-survives-failover.markRecordingForTestinghad been inserted betweenstartRecordingand its doc comment; moved it so the documentation stays onstartRecording.🤖 Generated with Claude Code