felangel / web_socket_client

A simple WebSocket client for Dart which includes automatic reconnection logic.
https://pub.dev/packages/web_socket_client
MIT License
150 stars 32 forks source link

Does not handle errors emitted by web socket channel or thrown inside listen #56

Closed saibotma closed 2 months ago

saibotma commented 2 months ago

I did not test this or experience a bug, but after looking at the source code, I noticed that an error emitted by the web socket channel or thrown inside listen never gets handled.

Future<void> _connect() async {
  // ..

  void attemptToReconnect([Object? error, StackTrace? stackTrace]) {
    // ..
  }

  try {
    // ..
    _channel!.stream.listen(
          (message) {
        if (_messageController.isClosed) return;
        _messageController.add(message);
      },
      onDone: attemptToReconnect,
      cancelOnError: true,
    );
  } catch (error, stackTrace) {
    attemptToReconnect(error, stackTrace);
  }
}

Due to onDone not getting called, when the stream gets cancelled because of an error, attemptToReconnect will never be called in this case. In addition to that, an error thrown inside listen will put the web socket client in a kind of zombie state, because the error isn't propagated up and never handled inside listen.

So from my point of view the solutions would be:

I could be wrong, but wanted to point this out.

felangel commented 2 months ago

Thanks for filing an issue! I don’t think this is an issue since cancelOnError is true and we should catch the error and attempt to reconnect as is. Happy to look at a simple reproduction sample if you have one that illustrates incorrect behavior though, thanks!

felangel commented 2 months ago

Closing for now since there are no actionable next steps without a reproduction sample.