Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/ignore-replaced-socket-callbacks
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" "A late close from a signal socket the SDK already replaced no longer starts another reconnect. In a browser it arrives up to seconds later, which made a room resume every 2 s after its first reconnect. A signal connect that only opens after disconnect() or a newer connect() is closed instead of taking over the connection"
5 changes: 2 additions & 3 deletions lib/src/connection_check/checks/reconnect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ class ReconnectCheck extends Checker {
});

try {
// Forcefully close the underlying signal WebSocket; the engine should
// notice and resume the session on its own.
await engine.signalClient.cleanUp();
// Simulate a signal loss and wait for the room to resume.
await engine.signalClient.simulateSignalDrop();

await reconnectCompleter.future.timeout(
const Duration(seconds: 5),
Expand Down
4 changes: 4 additions & 0 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
} catch (error) {
logger.fine('Connect Error $error');

// A superseded attempt reports nothing, the connection that replaced it
// is the one the room has.
if (error is SignalConnectSupersededException) rethrow;
Comment on lines +357 to +359

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Cancelled reconnect restarts the new room

When a full reconnect opens after the app leaves and rejoins, SignalConnectSupersededException still reaches reconnect recovery. attemptReconnect treats it as retryable and restarts the healthy session.

Learn more

A server-requested full reconnect opens a fresh signaling socket through restartConnection. The app can disconnect during that handshake and reuse the room; Engine.connect clears _isClosed and _connect replaces the URL and token. When the old handshake returns, SignalClient.connect discards it with a superseded exception. Without another region, restartConnection propagates that exception, and attemptReconnect sets fullReconnectOnNext and queues a retry. The engine is open again, so the retry tears down the new room's transports in restartConnection. With a regional fallback, restartConnection can start another handshake directly instead of propagating cancellation.

Example: Room A starts a full reconnect. The app leaves A and successfully joins B using the same Room. A's handshake returns late; the old retry starts a full reconnect of B even though B is healthy.

Recommended fix: Propagate supersession past regional failover and treat it as cancellation, not failure, in attemptReconnect. Scope the attempt's retry bookkeeping and finally cleanup to the session that started it. Test a full reconnect held at the handshake while the app disconnects and joins again, then verify that releasing the stale handshake opens no further sockets.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.


// during a reconnect this connect() runs inside restartConnection and
// attemptReconnect owns disconnect emission, emitting here as well
// would produce two events for one failure
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1353,12 +1353,12 @@ extension RoomDebugMethods on Room {
bool? leaveRequestFullReconnect,
}) async {
if (signalReconnect != null && signalReconnect) {
await engine.signalClient.cleanUp();
await engine.signalClient.simulateSignalDrop();
return;
}
if (fullReconnect != null && fullReconnect) {
engine.fullReconnectOnNext = true;
await engine.signalClient.cleanUp();
await engine.signalClient.simulateSignalDrop();
return;
}
engine.signalClient.sendSimulateScenario(
Expand All @@ -1375,7 +1375,7 @@ extension RoomDebugMethods on Room {
// The server only acts on the next resume, so start one now. Mirrors the
// post action in client-sdk-js.
if ((disconnectSignalOnResume ?? false) || (disconnectSignalOnResumeNoMessages ?? false)) {
await engine.signalClient.cleanUp();
await engine.signalClient.simulateSignalDrop();
}
}
}
Expand Down
83 changes: 74 additions & 9 deletions lib/src/core/signal_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
ConnectionState get connectionState => _connectionState;

final WebSocketConnector _wsConnector;

// Id of the socket whose callbacks are honored. connect() gives each new
// socket the next id, and cleanUp() advances it too so the socket it disposes
// stops being current. A real socket reports its close after dispose()
// returns, a few milliseconds later on a good path but seconds later in a
// browser when the server keeps TCP open after the close handshake. That
// report used to be attributed to whatever connection was current by then,
// clobbering its state and starting a reconnect against a healthy socket,
// over and over when every reconnect replaced the socket again.
int _socketId = 0;
LiveKitWebSocket? _ws;

final _queue = Queue<lk_rtc.SignalRequest>();
Expand Down Expand Up @@ -144,6 +154,8 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {

logger.fine('SignalClient connecting with url: ${rtcUri.redactingAccessToken}');

// Id of the socket this attempt opens, set once the previous one is gone.
int? socketId;
try {
if (reconnect == true) {
_connectionState = ConnectionState.reconnecting;
Expand All @@ -154,25 +166,38 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
}
// Clean up existing socket
await cleanUp();
// Attempt to connect
var future = _wsConnector(
// Attempt to connect. Callbacks carry the id of the socket they belong to
// so a late report from a replaced socket is ignored.
final id = socketId = ++_socketId;
final ws = await _wsConnector(
Comment on lines +169 to +172

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Stale resume rejects a newer join

If a resume finishes closing its old socket during a new join’s handshake, connect gives the resume the newer ID. The new join then rejects its own successful handshake as superseded.

Learn more

A server migration requesting RESUME reaches resumeConnection while the old signal socket is still open. cleanUp detaches that socket before awaiting its disposal, and the native WebSocket disposer awaits the close. The app can complete disconnect and call Room.connect while that detached socket is closing. If the close finishes while the new join is opening its socket, the old resume claims a later ID and the post-handshake check rejects the new join, regardless of whether the resume itself succeeds.

Example: A migration starts a resume and waits for socket A to close. The app leaves and starts joining another room on socket B. A finishes closing before B opens, so the stale resume claims the current ID. B opens successfully but the SDK disposes it and fails the join.

Recommended fix: Reserve ownership of the connection attempt before asynchronous preparation and teardown. Let connect detach the old socket without invalidating its own reservation, and recheck that reservation before invoking the connector as well as after the handshake. Add a gated test where the old close finishes while the new join is opening.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

rtcUri,
options: WebSocketEventHandlers(
onData: _onSocketData,
onDispose: _onSocketDispose,
onError: _onSocketError,
onData: (data) => _ifCurrentSocket(id, () => _onSocketData(data)),
onDispose: () => _ifCurrentSocket(id, _onSocketDispose),
onError: (error) => _ifCurrentSocket(id, () => _onSocketError(error)),
),
headers: {
'Authorization': 'Bearer $token',
},
networkOptions: roomOptions.networkOptions,
);
future = future.timeout(connectOptions.timeouts.connection);
_ws = await future;
).timeout(connectOptions.timeouts.connection);
if (id != _socketId) {
// disconnect() or a newer connect() took over while this socket was
// opening. Installing it would take the place of the current one.
await ws.dispose();
throw const SignalConnectSupersededException();
Comment on lines +184 to +188

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Stale join disables Android call routing

When a cancelled join opens after a successful rejoin, SignalConnectSupersededException reaches Room.connect. Its failure cleanup stops the shared Android audio session, releasing the active call’s audio focus and routing.

Learn more

Room.connect starts native audio before it opens the signal socket. Room._cleanUp stops audio when the app disconnects, and the next join starts it again. When the first socket finally opens, the superseded exception passes through Engine._connect, but the original Room.connect still runs its failure cleanup. In SDK-managed Android mode, NativeAudioManagement.stop stops the shared session, and LKAudioSwitchManager.stop tears down focus and routing rather than releasing an attempt-specific resource.

Example: An Android app cancels a slow join and then joins another room. The new call starts with Bluetooth routing. The first handshake completes late and its error cleanup stops the audio session still in use by the new call.

Recommended fix: Track which room connection attempt owns the native audio setup and make failure cleanup conditional on that ownership. A superseded attempt must not stop audio after a newer attempt has taken ownership. Extend the delayed-handshake regression test to assert that the stale completion causes no extra native audio stop.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

}
_ws = ws;
// Successful connection
_connectionState = ConnectionState.connected;
events.emit(const SignalConnectedEvent());
} catch (socketError) {
// An attempt that something else replaced leaves the state and the
// events to the connection that replaced it.
if (socketId != null && socketId != _socketId) {
if (socketError is SignalConnectSupersededException) rethrow;
throw const SignalConnectSupersededException();
}
if (socketError is CertificatePinningException) {
// In reconnect mode the engine owns state and event emission,
// emitting here would race its reconnect handling.
Expand Down Expand Up @@ -242,11 +267,15 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
@internal
Future<void> cleanUp() async {
logger.fine('[${objectId}] cleanUp()');
_socketId++;
_connectionState = ConnectionState.disconnected;
await _ws?.dispose();
// Detached before the await, so a connect() that runs while the old
// socket is still closing keeps its own socket, queue and pings.
final ws = _ws;
_ws = null;
_queue.clear();
_clearPingInterval();
await ws?.dispose();
}

void _sendRequest(
Expand Down Expand Up @@ -409,6 +438,31 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
}
}

void _ifCurrentSocket(int socketId, void Function() action) {
if (socketId != _socketId) {
logger.fine('[$objectId] ignoring a callback from a socket that was already replaced');
return;
}
action();
}

/// Drops the signal socket the way a server side close does: the socket goes
/// away and the engine sees a disconnect it has to recover from. For the
/// debug scenarios and the connection checker. [cleanUp] alone is a quiet
/// teardown, the disposed socket's own close is ignored.
@internal
Future<void> simulateSignalDrop() async {
if (_ws == null) {
logger.fine('[$objectId] simulateSignalDrop(): no signal socket to drop');
return;
}
// The loss is reported before the socket has finished closing, which can
// take a while, so it is charged to the socket that was dropped.
final dropped = cleanUp();
_onSocketDispose();
await dropped;
}

void _onSocketError(dynamic error) {
logger.warning('received websocket error $error');
}
Expand Down Expand Up @@ -629,3 +683,14 @@ extension SignalClientInternalMethods on SignalClient {
@internal
void clearQueue() => _queue.clear();
}

/// Thrown by a [SignalClient.connect] whose socket was no longer wanted by the
/// time it opened, because disconnect() or a newer connect() took over. The
/// connection that took over owns the state and the events.
@internal
class SignalConnectSupersededException implements Exception {
const SignalConnectSupersededException();

@override
String toString() => 'SignalConnectSupersededException: the signal connection was superseded while it was opening';
}
Loading
Loading