graphql-flutter icon indicating copy to clipboard operation
graphql-flutter copied to clipboard

Websocket not reconnect when no internet

Open PhilippS93 opened this issue 1 year ago • 3 comments

I am using WebSocketLink for subscription support. The initial connect does work pretty well. When I turn off the internet (autoReconnect is turned on), the reconnect logic in websocket_client.dart uses the reconnect timer in void onConnectionLost([Object? e]) async to do the reconnect.

When the connection is lost, the following line is called in onConnectionLost

await _closeSocketChannel();

In this method,, the line await socketChannel?.sink.close(ws_status.normalClosure); is executed.

This line calls Future close([int? closeCode, String? closeReason]) in sink_completer.dart which returns the done future.

In the done getter, after the first autoReconnect timeout, the _destinationSink will be null and the lines

if (_destinationSink == null) {
      _doneCompleter = Completer.sync();
      return _doneCompleter!.future;
    }

will be executed. The resulting future will never complete, because Completer.sync().complete() is never called.

Thus, the client has hung up and will never reconnect.

I have adapted the method like

if (_destinationSink == null) {
      _doneCompleter = Completer.sync();
      _doneCompleter!.complete();
      return _doneCompleter!.future;
    }

and now, the reconnect will work again.

To Reproduce (MUST BE PROVIDED)

  1. Create a minimal app with websocket support and subscriptions. Set autoReconnect=true
  2. start the app, connect the websocket and turn off internet
  3. let the websocket reconnect timer run 2 times.
  4. the second run will hang up the reconnect, because the future does not complete

Expected behavior The future should complete and reconnect should work

device / execution context

  • Android device / Samsung S20
  • graphql_flutter: 5.1.2
  • flutter 2.19.6

PhilippS93 avatar May 02 '23 19:05 PhilippS93

Yeah there is this tricky stuff, we should rewrite our ws protocol client, so I will take this and put on my todo list!

vincenzopalazzo avatar May 03 '23 08:05 vincenzopalazzo

@vincenzopalazzo I would love to contribute on this issue if you allow me?

A2asad avatar Dec 12 '23 10:12 A2asad

I'm curious if there has been any progress on this? I'm hitting exactly this behavior as well. I've tried the suggested fix and it is still hanging up on the second retry. I'm digging into it here, but I'm curious if anyone has made any progress? It seems strange that something as fundamental as reconnect has been sitting in an non-working state for so long. I wonder what people are using?

james-pellow avatar Jan 22 '24 15:01 james-pellow