diff --git a/.changes/web-websocket-error-after-open b/.changes/web-websocket-error-after-open new file mode 100644 index 000000000..3f0ae2ef1 --- /dev/null +++ b/.changes/web-websocket-error-after-open @@ -0,0 +1 @@ +patch type="fixed" "Web: a signal connection that fails after it opened no longer throws an uncaught Future already completed error" diff --git a/lib/src/support/websocket/web.dart b/lib/src/support/websocket/web.dart index bb89f412b..3dad90f60 100644 --- a/lib/src/support/websocket/web.dart +++ b/lib/src/support/websocket/web.dart @@ -84,7 +84,10 @@ class LiveKitWebSocketWeb extends LiveKitWebSocket { final completer = Completer(); 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; } } diff --git a/test/support/websocket_web_error_server.dart b/test/support/websocket_web_error_server.dart new file mode 100644 index 000000000..17b06ee88 --- /dev/null +++ b/test/support/websocket_web_error_server.dart @@ -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 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.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)); +} diff --git a/test/support/websocket_web_test.dart b/test/support/websocket_web_test.dart new file mode 100644 index 000000000..14cd9e7c0 --- /dev/null +++ b/test/support/websocket_web_test.dart @@ -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 = []; + final disposed = Completer(); + + 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.delayed(const Duration(milliseconds: 100)); + }, (error, _) => errors.add(error)); + + expect(errors, isEmpty); + }); +}