zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.24k stars 612 forks source link

Websocket not reconnect when no internet #1321

Open PhilippS93 opened 1 year ago

PhilippS93 commented 1 year ago

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

vincenzopalazzo commented 1 year ago

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

A2asad commented 9 months ago

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

james-pellow commented 7 months ago

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?