Skip to content

Commit

Permalink
Fix a race when close() called before remote class event delivered (#348
Browse files Browse the repository at this point in the history
)
  • Loading branch information
brianquinlan authored Apr 26, 2024
1 parent b612fc2 commit b672220
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/adapter_web_socket_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ class AdapterWebSocketChannel extends StreamChannelMixin
}

webSocketFuture.then((webSocket) {
var remoteClosed = false;
webSocket.events.listen((event) {
switch (event) {
case TextDataReceived(text: final text):
_controller.local.sink.add(text);
case BinaryDataReceived(data: final data):
_controller.local.sink.add(data);
case CloseReceived(code: final code, reason: final reason):
remoteClosed = true;
_closeCode = code;
_closeReason = reason;
_controller.local.sink.close();
Expand All @@ -105,13 +103,15 @@ class AdapterWebSocketChannel extends StreamChannelMixin
default:
throw UnsupportedError('Cannot send ${obj.runtimeType}');
}
} on WebSocketConnectionClosed catch (_) {
} on WebSocketConnectionClosed {
// There is nowhere to surface this error; `_controller.local.sink`
// has already been closed.
}
}, onDone: () {
if (!remoteClosed) {
webSocket.close(_localCloseCode, _localCloseReason);
}, onDone: () async {
try {
await webSocket.close(_localCloseCode, _localCloseReason);
} on WebSocketConnectionClosed {
// It is not an error to close an already-closed `WebSocketChannel`.
}
});
_protocol = webSocket.protocol;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
crypto: ^3.0.0
stream_channel: ^2.1.0
web: ^0.5.0
web_socket: ^0.1.1
web_socket: ^0.1.3

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
Expand Down

0 comments on commit b672220

Please sign in to comment.