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/web-websocket-error-after-open
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" "Web: a signal connection that fails after it opened no longer throws an uncaught Future already completed error"
5 changes: 4 additions & 1 deletion lib/src/support/websocket/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ class LiveKitWebSocketWeb extends LiveKitWebSocket {
final completer = Completer<LiveKitWebSocketWeb>();
final ws = web.WebSocket(uri.toString());
ws.onOpen.listen((_) => completer.complete(LiveKitWebSocketWeb._(ws, options)));
ws.onError.listen((e) => completer.completeError(WebSocketException('Failed to connect', e)));
// A socket that fails after opening also fires `error`; the close listener handles that case.
ws.onError.listen((e) {
if (!completer.isCompleted) completer.completeError(WebSocketException('Failed to connect', e));
});
return completer.future;
}
}
41 changes: 41 additions & 0 deletions test/support/websocket_web_error_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2026 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Serves one WebSocket that opens and then breaks the protocol, so the browser fails the open connection.

import 'dart:convert';
import 'dart:io';

import 'package:crypto/crypto.dart';

// [channel] is the StreamChannel that spawnHybridUri passes in.
Future<void> hybridMain(dynamic channel) async {
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
channel.sink.add(server.port);
server.listen((request) async {
final key = request.headers.value('sec-websocket-key')!;
final accept = base64.encode(sha1.convert(utf8.encode('${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11')).bytes);
final socket = await request.response.detachSocket(writeHeaders: false);
socket.add(
utf8.encode(
'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n'
'Sec-WebSocket-Accept: $accept\r\n\r\n',
),
);
await Future<void>.delayed(const Duration(milliseconds: 200));
// A frame with the reserved bits set is a protocol error.
socket.add([0xF1, 0x00]);
});
channel.stream.listen(null, onDone: () => server.close(force: true));
}
44 changes: 44 additions & 0 deletions test/support/websocket_web_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2026 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@TestOn('browser')
@Timeout(Duration(seconds: 10))
library;

import 'dart:async';

import 'package:flutter_test/flutter_test.dart';

import 'package:livekit_client/src/support/websocket.dart';
import 'package:livekit_client/src/support/websocket/web.dart';

void main() {
test('an open connection that fails is disposed without an uncaught error', () async {
final server = spawnHybridUri('websocket_web_error_server.dart');
final port = await server.stream.first as int;
final errors = <Object>[];
final disposed = Completer<void>();

await runZonedGuarded(() async {
await LiveKitWebSocketWeb.connect(
Uri.parse('ws://127.0.0.1:$port/rtc'),
options: WebSocketEventHandlers(onDispose: disposed.complete),
);
await disposed.future;
await Future<void>.delayed(const Duration(milliseconds: 100));
}, (error, _) => errors.add(error));

expect(errors, isEmpty);
});
}
Loading