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:
hailin 2026-02-25 03:41:13 -08:00
parent 3185438f36
commit 45eb6bc453
1 changed files with 5 additions and 6 deletions

View File

@ -30,16 +30,15 @@ class WebSocketClient {
_lastPath = path;
_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) {
_heartbeatTimer?.cancel();
try {
await _channel!.sink.close();
} catch (_) {
// Ignore errors when closing stale connections
}
final old = _channel!;
_channel = null;
_isConnected = false;
unawaited(old.sink.close().catchError((_) {}));
}
final uri = Uri.parse('$baseUrl$path');