Skip to content

fix: decide the reconnect mode after the connectivity wait, not before it - #1320

Open
xianshijing-lk wants to merge 1 commit into
mainfrom
sxian/fix-reconnect-mode-after-connectivity-wait
Open

xianshijing-lk wants to merge 1 commit into
mainfrom
sxian/fix-reconnect-mode-after-connectivity-wait

Conversation

@xianshijing-lk

@xianshijing-lk xianshijing-lk commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

attemptReconnect captured fullReconnectOnNext up front, and only then parked in the connectivity wait:

final fullReconnect = fullReconnectOnNext;   // decided here
fullReconnectOnNext = false;
...
if (await signalClient.networkIsAvailable() == false) {
  await signalClient.events.waitFor<SignalConnectivityChangedEvent>(
    duration: connectOptions.timeouts.connection * 10,   // ...but acted on up to 10 timeouts later
    ...
  );
}
if (fullReconnect) { await restartConnection(); } else { await resumeConnection(...); }

The mode it acts on is a snapshot of state taken before an unbounded wait on that very state.

The case that matters: ICE giving up partway through an outage calls handleReconnect(peerConnectionFailed), which sets fullReconnectOnNext = true. The attempt coming out of the wait has already consumed the flag, so it sends a resume anyway and honors the escalation only on the attempt after it — one doomed round trip, and the server's cant_migrate rejection, before recovery.

Fix

Move the mode decision below the wait, so a request raised during the wait takes effect on the attempt that follows it.

Nothing here infers a lost session from the transport's state, deliberately. resumeConnection is built for a peer that is not connected — it sends createAndSendOffer(iceRestart: true), sets subscriber.restartingIce, and waits for the peer to come back. A transport that went away with the network is therefore still resumable, and escalating on it would trade a recoverable session for a certain loss: restartConnection resets _reliableDataSequence and clears _reliableMessageBuffer, discarding exactly the packets a resume replays via _resendReliableMessagesForResume(lastMessageSeq).

A terminally gone transport already reports itself through peerConnectionFailed, which handleReconnect escalates — and which now applies to the current attempt rather than the next one.

An earlier revision of this PR added a transport-state gate (escalate if the primary peer connection was not connected after the wait). Devin Review flagged it and was right — it escalated in precisely the case resumeConnection exists to handle, and paid for it with the reliable buffer. That gate is gone, and the third test below now pins the behaviour it would have broken.

Scope

No change to retry/backoff policy, and no elapsed-time rule. Whether a resume budget should replace Flutter's current escalation conditions is a separate question that needs production measurement.

This SDK does not have the Swift bug fixed in livekit/client-sdk-swift#1244 — it consumes a single flag per attempt rather than seeding one per episode, so a RECONNECT leave is already honored.

Test seam

SignalClient.debugNetworkIsAvailable (@visibleForTesting) is new. networkIsAvailable() returns true unconditionally under lkPlatformIsTest(), which left the entire offline branch unreachable from the unit tests — there was no way to exercise this code path at all.

Tests

test/core/reconnect_after_connectivity_wait_test.dart, verified against origin/main:

Case Expected On main
ICE fails while the attempt is parked in the wait full reconnect fails — sends reconnect=1
Transport merely disconnected across the wait resume passes — guards the regression the dropped gate would have caused
Transport still connected (signalling-only failure) resume passes

Full suite: 683 tests pass. dart analyze lib test clean, dart format --line-length 120 clean.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

This PR bumps

  • livekit_client: patch

devin-ai-integration[bot]

This comment was marked as resolved.

…e it

`attemptReconnect` captured `fullReconnectOnNext` up front and only then parked
in the connectivity wait, which runs for up to ten connection timeouts. The mode
it acted on was therefore a snapshot of state taken before an unbounded wait on
that very state.

The case that matters: ICE giving up partway through an outage calls
`handleReconnect` with `peerConnectionFailed`, which sets `fullReconnectOnNext`.
The attempt coming out of the wait had already consumed the flag, so it sent a
resume anyway and honored the escalation only on the attempt after it.

Move the mode decision below the wait so that request takes effect on the
attempt that follows it.

Nothing here infers a lost session from the transport's state. `resumeConnection`
is built for a peer that is not connected — it sends an ICE restart and waits for
the peer to come back — so a transport that went away with the network is still
resumable. Escalating on that would trade a recoverable session for a certain
loss, because `restartConnection` resets `_reliableDataSequence` and clears
`_reliableMessageBuffer`, discarding exactly the packets a resume replays from
`lastMessageSeq`. A terminally gone transport already reports itself through
`peerConnectionFailed`, which is escalated and now applies to this attempt.

`SignalClient.debugNetworkIsAvailable` is a test seam: `networkIsAvailable()`
returns true unconditionally under `lkPlatformIsTest()`, which left the whole
offline branch unreachable from the unit tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@xianshijing-lk
xianshijing-lk force-pushed the sxian/fix-reconnect-mode-after-connectivity-wait branch from 1dd0d78 to 730da13 Compare November 26, 2026 01:30

This branch has not been deployed

No deployments
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