Skip to content

Commit bf9965c

Browse files
authored
Fix a race when close() called before remote class event delivered (dart-archive/web_socket_channel#348)
1 parent d0940e6 commit bf9965c

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

pkgs/web_socket_channel/lib/adapter_web_socket_channel.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,13 @@ class AdapterWebSocketChannel extends StreamChannelMixin
7979
}
8080

8181
webSocketFuture.then((webSocket) {
82-
var remoteClosed = false;
8382
webSocket.events.listen((event) {
8483
switch (event) {
8584
case TextDataReceived(text: final text):
8685
_controller.local.sink.add(text);
8786
case BinaryDataReceived(data: final data):
8887
_controller.local.sink.add(data);
8988
case CloseReceived(code: final code, reason: final reason):
90-
remoteClosed = true;
9189
_closeCode = code;
9290
_closeReason = reason;
9391
_controller.local.sink.close();
@@ -105,13 +103,15 @@ class AdapterWebSocketChannel extends StreamChannelMixin
105103
default:
106104
throw UnsupportedError('Cannot send ${obj.runtimeType}');
107105
}
108-
} on WebSocketConnectionClosed catch (_) {
106+
} on WebSocketConnectionClosed {
109107
// There is nowhere to surface this error; `_controller.local.sink`
110108
// has already been closed.
111109
}
112-
}, onDone: () {
113-
if (!remoteClosed) {
114-
webSocket.close(_localCloseCode, _localCloseReason);
110+
}, onDone: () async {
111+
try {
112+
await webSocket.close(_localCloseCode, _localCloseReason);
113+
} on WebSocketConnectionClosed {
114+
// It is not an error to close an already-closed `WebSocketChannel`.
115115
}
116116
});
117117
_protocol = webSocket.protocol;

pkgs/web_socket_channel/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies:
1414
crypto: ^3.0.0
1515
stream_channel: ^2.1.0
1616
web: ^0.5.0
17-
web_socket: ^0.1.1
17+
web_socket: ^0.1.3
1818

1919
dev_dependencies:
2020
dart_flutter_team_lints: ^2.0.0

0 commit comments

Comments
 (0)