diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 039f16321..298468ba2 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +🐞 Fixed + +- Fixed `WebSocket` race condition where reconnection could access null user during disconnect. + ## 9.14.0 🐞 Fixed diff --git a/packages/stream_chat/lib/src/ws/websocket.dart b/packages/stream_chat/lib/src/ws/websocket.dart index f4c4cd875..4a339a423 100644 --- a/packages/stream_chat/lib/src/ws/websocket.dart +++ b/packages/stream_chat/lib/src/ws/websocket.dart @@ -263,6 +263,12 @@ class WebSocket with TimerHelper { setTimer( Duration(milliseconds: delay), () async { + // If the user is null, it means either the connection was never + // established or it was disconnected manually. + // + // In either case, we should not attempt to reconnect. + if (_user == null) return; + final uri = await _buildUri( refreshToken: refreshToken, includeUserDetails: false, @@ -475,21 +481,18 @@ class WebSocket with TimerHelper { /// Disconnects the WS and releases eventual resources void disconnect() { if (connectionStatus == ConnectionStatus.disconnected) return; - - _resetRequestFlags(resetAttempts: true); - _connectionStatus = ConnectionStatus.disconnected; _logger?.info('Disconnecting web-socket connection'); + _manuallyClosed = true; + _resetRequestFlags(resetAttempts: true); + _stopMonitoringEvents(); + // resetting user _user = null; connectionCompleter = null; - _stopMonitoringEvents(); - - _manuallyClosed = true; - _closeWebSocketChannel(); } }