dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
418 stars 109 forks source link

Attempting to close a websocket from inside of Flutter for web throws a stack trace #192

Closed domesticmouse closed 2 years ago

domesticmouse commented 3 years ago

Code that causes the issue:

final channel = WebSocketChannel.connect(
  Uri.parse('ws://localhost:8080'),
);

channel.stream.listen((message) {
  channel.sink.close(status.goingAway);
});

Stack trace:

DOMException: Failed to execute 'close' on 'WebSocket': The code must be either 1000, or
between 3000 and 4999. 1001 is neither.
    at
    http://localhost:56871/packages/web_socket_channel/src/copy/web_socket_impl.dart.lib.js:129
    2:32
    at _RootZone.runGuarded (http://localhost:56871/dart_sdk.js:38432:11)
    at sendDone (http://localhost:56871/dart_sdk.js:32370:24)
    at _ControllerSubscription.new.[_sendDone] (http://localhost:56871/dart_sdk.js:32380:11)
    at _ControllerSubscription.new.[_close] (http://localhost:56871/dart_sdk.js:32295:26)
    at _SyncStreamController.new.[_sendDone] (http://localhost:56871/dart_sdk.js:35158:36)
    at _SyncStreamController.new.[_sendDone] (http://localhost:56871/dart_sdk.js:35262:32)
    at _SyncStreamController.new.[_closeUnchecked]
    (http://localhost:56871/dart_sdk.js:34958:26)
    at _SyncStreamController.new.close (http://localhost:56871/dart_sdk.js:34952:30)
    at
    http://localhost:56871/packages/stream_channel/src/stream_channel_controller.dart.lib.js:10
    59:39
    at _RootZone.runGuarded (http://localhost:56871/dart_sdk.js:38432:11)
    at sendDone (http://localhost:56871/dart_sdk.js:32370:24)
    at _ControllerSubscription.new.[_sendDone] (http://localhost:56871/dart_sdk.js:32380:11)
    at _ControllerSubscription.new.[_close] (http://localhost:56871/dart_sdk.js:32295:26)
    at _SyncStreamController.new.[_sendDone] (http://localhost:56871/dart_sdk.js:35158:36)
    at _SyncStreamController.new.[_sendDone] (http://localhost:56871/dart_sdk.js:35262:32)
    at _SyncStreamController.new.[_closeUnchecked]
    (http://localhost:56871/dart_sdk.js:34958:26)
    at _SyncStreamController.new.close (http://localhost:56871/dart_sdk.js:34952:30)
    at _StreamSinkWrapper.new.close (http://localhost:56871/dart_sdk.js:35296:31)
    at _GuaranteeSink.new.close
    (http://localhost:56871/packages/stream_channel/src/stream_channel_controller.dart.lib.js:1
    163:56)
    at html$._HtmlWebSocketSink.new.close
    (http://localhost:56871/packages/async/src/delegate/stream_sink.dart.lib.js:36:29)
    at html$._HtmlWebSocketSink.new.close
    (http://localhost:56871/packages/web_socket_channel/src/copy/web_socket_impl.dart.lib.js:13
    74:20)
    at http://localhost:56871/packages/sdui_websockets_client/main.dart.lib.js:106:20
    at _RootZone.runUnaryGuarded (http://localhost:56871/dart_sdk.js:38448:11)
    at _ControllerSubscription.new.[_sendData] (http://localhost:56871/dart_sdk.js:32328:22)
    at _ControllerSubscription.new.[_add] (http://localhost:56871/dart_sdk.js:32277:26)
    at _SyncStreamController.new.[_sendData] (http://localhost:56871/dart_sdk.js:35152:34)
    at _SyncStreamController.new.[_sendData] (http://localhost:56871/dart_sdk.js:35256:32)
    at _SyncStreamController.new.[_add] (http://localhost:56871/dart_sdk.js:34966:26)
    at _SyncStreamController.new.add (http://localhost:56871/dart_sdk.js:34932:19)
    at _RootZone.runUnaryGuarded (http://localhost:56871/dart_sdk.js:38448:11)
    at _ControllerSubscription.new.[_sendData] (http://localhost:56871/dart_sdk.js:32328:22)
    at _ControllerSubscription.new.[_add] (http://localhost:56871/dart_sdk.js:32277:26)
    at _SyncStreamController.new.[_sendData] (http://localhost:56871/dart_sdk.js:35152:34)
    at _SyncStreamController.new.[_sendData] (http://localhost:56871/dart_sdk.js:35256:32)
    at _SyncStreamController.new.[_add] (http://localhost:56871/dart_sdk.js:34966:26)
    at _SyncStreamController.new.add (http://localhost:56871/dart_sdk.js:34932:19)
    at _StreamSinkWrapper.new.add (http://localhost:56871/dart_sdk.js:35290:24)
    at _GuaranteeSink.new.add
    (http://localhost:56871/packages/stream_channel/src/stream_channel_controller.dart.lib.js:1
    120:24)
    at
    http://localhost:56871/packages/web_socket_channel/src/copy/web_socket_impl.dart.lib.js:133
    3:37
    at Object._checkAndCall (http://localhost:56871/dart_sdk.js:5268:16)
    at Object.dcall (http://localhost:56871/dart_sdk.js:5273:17)
    at WebSocket.<anonymous> (http://localhost:56871/dart_sdk.js:101824:100)
Gursewak-Uppal commented 2 years ago

Same error

Uncaught (in promise) DOMException: Failed to execute 'close' on 'WebSocket': The code must be either 1000, or between 3000 and 4999. 1001 is neither.

natebosch commented 2 years ago

This looks like it's coming from the browser. AFAICT the code 1001 is not intended to be used by your code - I think it's a code reserved for the browser to use?

Instead of status.goingAway choose status.normalClosure, or as the message indicates you can use some number between 3000 and 4999.

https://bugzilla.mozilla.org/show_bug.cgi?id=1467107