fix: use unawaited close to prevent WebSocket reconnect hang
The await on sink.close() blocks indefinitely when the server doesn't respond to the close handshake. Use fire-and-forget with unawaited() so the new connection can proceed immediately. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
3185438f36
commit
45eb6bc453
|
|
@ -30,16 +30,15 @@ class WebSocketClient {
|
||||||
_lastPath = path;
|
_lastPath = path;
|
||||||
_lastToken = token;
|
_lastToken = token;
|
||||||
|
|
||||||
// Close previous connection to prevent duplicate event delivery
|
// Close previous connection to prevent duplicate event delivery.
|
||||||
|
// Fire-and-forget: don't await close() as it can hang waiting for
|
||||||
|
// the server to acknowledge the close handshake.
|
||||||
if (_channel != null) {
|
if (_channel != null) {
|
||||||
_heartbeatTimer?.cancel();
|
_heartbeatTimer?.cancel();
|
||||||
try {
|
final old = _channel!;
|
||||||
await _channel!.sink.close();
|
|
||||||
} catch (_) {
|
|
||||||
// Ignore errors when closing stale connections
|
|
||||||
}
|
|
||||||
_channel = null;
|
_channel = null;
|
||||||
_isConnected = false;
|
_isConnected = false;
|
||||||
|
unawaited(old.sink.close().catchError((_) {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
final uri = Uri.parse('$baseUrl$path');
|
final uri = Uri.parse('$baseUrl$path');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue