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/republish-all-tracks-after-failure
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" "A track that fails to republish after a full reconnect no longer leaves the tracks after it unpublished or the room without a `RoomReconnectedEvent`. The failure is logged and the remaining tracks are republished"
2 changes: 2 additions & 0 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
..on<EngineRestartedEvent>((event) async {
// re-publish all tracks
await localParticipant?.rePublishAllTracks();
// The room may have closed or started another reconnect while the tracks were republished.
if (engine.isClosed || connectionState != ConnectionState.connected) return;

for (var participant in _remoteParticipants.toList()) {
for (var pub in participant.trackPublications.values.toList()) {
Expand Down
21 changes: 13 additions & 8 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,19 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
final tracks = trackPublications.values.toList();
trackPublications.clear();
for (LocalTrackPublication track in tracks) {
if (track.track is LocalAudioTrack) {
await publishAudioTrack(track.track as LocalAudioTrack);
} else if (track.track is LocalVideoTrack) {
final videoTrack = track.track as LocalVideoTrack;
// a full reconnect replaced the peer connection, so any simulcast
// codec senders the track still holds belong to the old one
videoTrack.clearSimulcastState();
await publishVideoTrack(videoTrack);
// one track failing to republish must not leave the others unpublished
try {
if (track.track is LocalAudioTrack) {
await publishAudioTrack(track.track as LocalAudioTrack);
} else if (track.track is LocalVideoTrack) {
final videoTrack = track.track as LocalVideoTrack;
// a full reconnect replaced the peer connection, so any simulcast
// codec senders the track still holds belong to the old one
videoTrack.clearSimulcastState();
await publishVideoTrack(videoTrack);
}
} catch (error, stackTrace) {
logger.warning('Failed to re-publish ${track.source} track ${track.sid}: $error', error, stackTrace);
Comment on lines +652 to +653

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Camera restarts after Unpublish all

While microphone republish waits, Unpublish all misses the camera in rePublishAllTracks’s private snapshot. After the microphone times out, the loop publishes the camera despite the user’s stop request.

Learn more

The example app’s Unpublish all control calls unpublishAllTracks() after confirmation and remains available on the room page during reconnect. unpublishAllTracks only sees entries in trackPublications. rePublishAllTracks clears that map before waiting for the first publish, so an unpublish request during that wait never reaches the queued camera. Before this change, a timeout on the first track stopped the loop, leaving the queued camera unpublished. Now the loop attempts the camera after the timeout, without honoring the completed Unpublish all request.

Example: A participant reconnects with microphone and camera enabled. While the microphone waits for its publish response, they confirm Unpublish all. The microphone times out, but the server answers the camera’s publish request, so video resumes.

Recommended fix: Make unpublishAllTracks() cancel pending reconnect publications as well as registered publications. Check that cancellation before attempting each queued track and reconcile any publish already in flight. Test Unpublish all during a pending microphone republish followed by a timeout, and assert that the camera never publishes.

Devin Review


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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The race is real, but it isn't new. On main the same thing happens whenever the microphone republish succeeds: rePublishAllTracks() has already cleared trackPublications, so unpublishAllTracks() during the wait can't see the queued camera. This PR only extends it to the case where the first republish fails. Cancelling queued republishes needs a cancellation hook in rePublishAllTracks(), which seems better as its own change.

Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
}
}
}
Expand Down
80 changes: 80 additions & 0 deletions test/core/room_e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@Timeout(Duration(seconds: 5))
library;

import 'dart:async';
import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';
Expand All @@ -23,6 +24,8 @@ import 'package:logging/logging.dart';

import 'package:livekit_client/livekit_client.dart';
import 'package:livekit_client/src/internal/events.dart';
import 'package:livekit_client/src/proto/livekit_models.pb.dart' as lk_models;
import 'package:livekit_client/src/proto/livekit_rtc.pb.dart' as lk_rtc;
import '../mock/e2e_container.dart';
import '../mock/test_data.dart';
import '../mock/websocket_mock.dart';
Expand Down Expand Up @@ -59,6 +62,62 @@ void main() {
}, skip: 'todo');
});

group('full reconnect', () {
test('republish failures do not stop other tracks or the reconnection', () async {
final participant = room.localParticipant!;
final tracks = [
for (final id in ['local_audio_1', 'local_audio_2']) _FailingAudioTrack(id, StateError('republish failed')),
];
for (final track in tracks) {
participant.addTrackPublication(
LocalTrackPublication<LocalAudioTrack>(
participant: participant,
info: remoteAudioTrack.clone()..sid = 'sid_${track.mediaStreamTrack.id}',
track: track,
),
);
}

final reconnected = room.events.waitFor<RoomReconnectedEvent>(duration: const Duration(seconds: 1));
container.engine.events.emit(const EngineRestartedEvent());
await reconnected;

expect(tracks.map((track) => track.startAttempts), [1, 1]);
});

test('no reconnected event when the server closes the room while a track is republished', () async {
final participant = room.localParticipant!;
final gate = Completer<void>();
final track = _FailingAudioTrack('local_audio_1', StateError('republish failed'), gate: gate);
participant.addTrackPublication(
LocalTrackPublication<LocalAudioTrack>(
participant: participant,
info: remoteAudioTrack.clone()..sid = 'sid_local_audio_1',
track: track,
),
);

var reconnected = false;
final listener = room.createListener()..on<RoomReconnectedEvent>((_) => reconnected = true);
addTearDown(listener.dispose);
container.engine.events.emit(const EngineRestartedEvent());
ws.onData(
lk_rtc.SignalResponse(
leave: lk_rtc.LeaveRequest(
action: lk_rtc.LeaveRequest_Action.DISCONNECT,
reason: lk_models.DisconnectReason.ROOM_DELETED,
),
).writeToBuffer(),
);
await room.events.waitFor<RoomDisconnectedEvent>(duration: const Duration(seconds: 1));
gate.complete();
await Future<void>.delayed(const Duration(milliseconds: 100));

expect(track.startAttempts, 1);
expect(reconnected, isFalse);
});
});

group('room updates', () {
test('participant join', () async {
final publicEventsStream = room.events.streamCtrl.stream.where((event) => event is! InternalEvent);
Expand Down Expand Up @@ -362,3 +421,24 @@ class _FakeMediaStreamTrack implements rtc.MediaStreamTrack {
@override
String toString() => 'FakeMediaStreamTrack(id: $id, kind: $kind, enabled: $enabled)';
}

class _FailingAudioTrack extends LocalAudioTrack {
final Object error;
final Completer<void>? gate;
int startAttempts = 0;

_FailingAudioTrack(String id, this.error, {this.gate})
: super(
TrackSource.microphone,
_FakeMediaStream('local_stream'),
_FakeMediaStreamTrack(id: id, kind: 'audio'),
const AudioCaptureOptions(),
);

@override
Future<void> startCapture() async {
startAttempts++;
await gate?.future;
throw error;
}
}
Loading