Skip to content

Commit

Permalink
Fix a StateError in IOWebSocket (#1177)
Browse files Browse the repository at this point in the history
This occurs when data is received from the peer after the connection has been closed locally.
  • Loading branch information
brianquinlan authored Apr 16, 2024
1 parent b7477b1 commit 34d7087
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
5 changes: 5 additions & 0 deletions pkgs/web_socket/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.2

- Fix a `StateError` in `IOWebSocket` when data is received from the peer
after the connection has been closed locally.

## 0.1.1

- Add the ability to create a `package:web_socket` `WebSocket` given a
Expand Down
13 changes: 7 additions & 6 deletions pkgs/web_socket/lib/src/io_web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class IOWebSocket implements WebSocket {
IOWebSocket._(this._webSocket) {
_webSocket.listen(
(event) {
if (_events.isClosed) return;
switch (event) {
case String e:
_events.add(TextDataReceived(e));
Expand All @@ -64,6 +65,7 @@ class IOWebSocket implements WebSocket {
}
},
onError: (Object e, StackTrace st) {
if (_events.isClosed) return;
final wse = switch (e) {
io.WebSocketException(message: final message) =>
WebSocketException(message),
Expand All @@ -72,12 +74,11 @@ class IOWebSocket implements WebSocket {
_events.addError(wse, st);
},
onDone: () {
if (!_events.isClosed) {
_events
..add(CloseReceived(
_webSocket.closeCode, _webSocket.closeReason ?? ''))
..close();
}
if (_events.isClosed) return;
_events
..add(
CloseReceived(_webSocket.closeCode, _webSocket.closeReason ?? ''))
..close();
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/web_socket/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
Any easy-to-use library for communicating with WebSockets
that has multiple implementations.
repository: https://github.com/dart-lang/http/tree/master/pkgs/web_socket
version: 0.1.1
version: 0.1.2

environment:
sdk: ^3.3.0
Expand Down
25 changes: 25 additions & 0 deletions pkgs/web_socket_conformance_tests/lib/src/close_local_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,35 @@ import 'package:web_socket/web_socket.dart';
import 'close_local_server_vm.dart'
if (dart.library.html) 'close_local_server_web.dart';

import 'continuously_writing_server_vm.dart'
if (dart.library.html) 'continuously_writing_server_web.dart'
as writing_server;

/// Tests that the [WebSocket] can correctly close the connection to the peer.
void testCloseLocal(
Future<WebSocket> Function(Uri uri, {Iterable<String>? protocols})
channelFactory) {
group('remote writing', () {
late Uri uri;
late StreamChannel<Object?> httpServerChannel;
late StreamQueue<Object?> httpServerQueue;

setUp(() async {
httpServerChannel = await writing_server.startServer();
httpServerQueue = StreamQueue(httpServerChannel.stream);
uri = Uri.parse('ws://localhost:${await httpServerQueue.next}');
});
tearDown(() async {
httpServerChannel.sink.add(null);
});

test('peer writes after close are ignored', () async {
final channel = await channelFactory(uri);
await channel.close();
expect(await channel.events.isEmpty, true);
});
});

group('local close', () {
late Uri uri;
late StreamChannel<Object?> httpServerChannel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:stream_channel/stream_channel.dart';

/// Starts an WebSocket server that sends a lot of data to the peer.
void hybridMain(StreamChannel<Object?> channel) async {
late HttpServer server;

server = (await HttpServer.bind('localhost', 0))
..transform(WebSocketTransformer()).listen((WebSocket webSocket) {
for (var i = 0; i < 10000; ++i) {
webSocket.add('Hello World!');
}
});

channel.sink.add(server.port);
await channel
.stream.first; // Any writes indicates that the server should exit.
unawaited(server.close());
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34d7087

Please sign in to comment.