graphql-flutter
graphql-flutter copied to clipboard
Websocket not reconnect when no internet
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)
- Create a minimal app with websocket support and subscriptions. Set autoReconnect=true
- start the app, connect the websocket and turn off internet
- let the websocket reconnect timer run 2 times.
- 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
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 I would love to contribute on this issue if you allow me?
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?