-
Notifications
You must be signed in to change notification settings - Fork 363
Description
Issue
When a user logs out and then logs back in as a different user without restarting the app, opening a previously viewed group thread causes the app to crash with the error: "You can't use a channel after client.disconnect() was called"
This occurs because the channel reference from the previous user session is retained in the application state (Redux/Context). When the new user session tries to access this stale channel, it calls channel.getClient(), which throws an error in the Stream Chat SDK because the channel instance is marked as disconnected: true from the previous logout.
Steps to reproduce
- Launch the app and authenticate as User A
- Navigate to and open any group thread (e.g., direct message thread or channel conversation)
- Note the thread displays correctly with messages and ability to interact
- Log out User A
- Log in as User B (different user, same device, no app restart)
- Navigate back to the threads list
- Tap on the same thread that was viewed in step 2
- See error and app crash
Expected behavior
The app should load the thread for User B without crashing. The channel should use the new client instance from the current session, not the disconnected client from the previous session. Thread should display correctly with all operations available.
Project Related Information
Customization
Click To Expand
Affected Components:
- StreamContext.tsx - Manages chat client lifecycle and channel state
- ChatWrapper.tsx - Connection state gating
- Thread route files:
thread-detail.tsx,[cid]/index.tsx,groups/[groupId]/index.tsx - Channel operations:
getReplies(),sendReaction(),deleteReaction(),search()
Error Location:
- node_modules/stream-chat/src/channel.ts (lines 177-182)
- node_modules/stream-chat/dist/esm/index.mjs (lines 7587-7589)
Error Source Code:
/**
* getClient - Get the chat client for this channel. If client.disconnect() was called, this function will error
*
* @return {StreamChat}
*/
getClient(): StreamChat {
if (this.disconnected === true) {
throw Error(`You can't use a channel after client.disconnect() was called`);
}
return this._client;
}Error Stack Trace Example:
Error: You can't use a channel after client.disconnect() was called
at Channel.getClient (node_modules/stream-chat/src/channel.ts:178:13)
at async Channel.getReplies (node_modules/stream-chat/src/channel.ts:1856:22)
at async ThreadDetail (src/app/threads/thread-detail.tsx:87:15)
at async ThreadScreen (src/app/threads/[cid]/index.tsx:45:9)
at runCatchIOException(ExceptionsManager.js:178:11)
Root Cause Technical Sequence:
-
Phase 1 - Initial State:
- User A authenticates via JWT → StreamChat client created
- Channels queried and cached in Redux/Context state
channel.disconnected = falsefor all active channels- WebSocket connection established
-
Phase 2 - Disconnection:
- User A initiates logout
client.disconnect()called → all channel instances markeddisconnected = true- WebSocket closed
- Redux/Context state still contains old channel references with
disconnected: true
-
Phase 3 - New Session:
- User B authenticates via new JWT
- New StreamChat client instance created and set in context
- Old channel instances from User A still exist in Redux state
- App navigation does not trigger channel state cleanup
-
Phase 4 - Crash:
- Navigation to thread triggers component mount
- Thread component calls
channel.getReplies() getReplies()internally callsthis.getClient()- SDK checks:
if (this.disconnected === true) throw Error(...) - Error thrown but not caught → uncaught exception → app crash
Offline support
- I have enabled offline support.
- The feature I'm having does not occur when offline support is disabled.
Environment
Click To Expand
package.json:
{
"dependencies": {
"react": "^18.2.0",
"react-native": "0.81.5",
"expo": "^52.0.0",
"expo-router": "^4.0.0",
"@react-navigation/native": "^6.1.0",
"stream-chat": "^9.26.0",
"stream-chat-react-native-core": "^8.10.0",
"@reduxjs/toolkit": "^1.9.5",
"react-redux": "^8.1.1",
"@tanstack/react-query": "^5.0.0"
}
}- Platform that you're experiencing the issue on:
- Both (iOS and Android)
stream-chatversion you're using that has this issue:9.26.0
stream-chat-react-nativeversion:8.10.0
- Device/Emulator info:
- Tested on physical devices
- OS version:
iOS 17.x, Android 13+ - Device/Emulator:
iPhone 14+, Samsung Android phones
Issue Summary:
This is a critical bug affecting multi-user scenarios. The root cause is SDK marking channels as disconnected during logout, but the application-level state management not clearing these stale references during user transitions. When a new user accesses these disconnected channels, the SDK throws an uncaught error.
Severity: 🔴 Critical (Blocks core feature - threading in multi-user scenarios)
Frequency: 100% reproducible