Skip to content

Commit 186d12e

Browse files
authored
fix(llc): adjust WebSocket disconnect order to prevent race conditions (#2313)
1 parent 30eb3a8 commit 186d12e

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/stream_chat/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Upcoming
2+
3+
🐞 Fixed
4+
5+
- Fixed `WebSocket` race condition where reconnection could access null user during disconnect.
6+
17
## 9.14.0
28

39
🐞 Fixed

packages/stream_chat/lib/src/ws/websocket.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ class WebSocket with TimerHelper {
263263
setTimer(
264264
Duration(milliseconds: delay),
265265
() async {
266+
// If the user is null, it means either the connection was never
267+
// established or it was disconnected manually.
268+
//
269+
// In either case, we should not attempt to reconnect.
270+
if (_user == null) return;
271+
266272
final uri = await _buildUri(
267273
refreshToken: refreshToken,
268274
includeUserDetails: false,
@@ -475,21 +481,18 @@ class WebSocket with TimerHelper {
475481
/// Disconnects the WS and releases eventual resources
476482
void disconnect() {
477483
if (connectionStatus == ConnectionStatus.disconnected) return;
478-
479-
_resetRequestFlags(resetAttempts: true);
480-
481484
_connectionStatus = ConnectionStatus.disconnected;
482485

483486
_logger?.info('Disconnecting web-socket connection');
484487

488+
_manuallyClosed = true;
489+
_resetRequestFlags(resetAttempts: true);
490+
_stopMonitoringEvents();
491+
485492
// resetting user
486493
_user = null;
487494
connectionCompleter = null;
488495

489-
_stopMonitoringEvents();
490-
491-
_manuallyClosed = true;
492-
493496
_closeWebSocketChannel();
494497
}
495498
}

0 commit comments

Comments
 (0)