-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: ChannelInner can call methods on a disconnected channel, causing uncaught errors #2393
Comments
Hey, @wchargin, this is a known set of somewhat big longstanding issues that have yet to be resolved. As one of the fixes we've introduced Thank you for submitting and let me know if you encounter simillar issue with the suggested solution. |
Hey @arnautov-anton; thanks for your quick response. I took a look at const chatClient = useCreateChatClient();
const channel = useMemo(() => {
if (streamClient) {
return streamClient.channel("team", "some-channel-id");
}
}, [chatClient]);
useEffect(() => {
if (channel) {
channel.watch();
return () => {
channel.stopWatching();
};
}
}, [channel]); —then how could we ensure that the client does not get disconnected while the (We're not trying to switch between users quickly or at all; it's just one user per session, and we do not expect the user ID to ever change. This issue occurs when you simply unmount the component by navigating away to a different part of the client-side application.) |
Ah, I see - yes, this might be a bit problematic as our const [channel, setChannel] = useState<Channel | undefined>(undefined);
useEffect(() => {
if (!chatClient) return;
let interrupted = false;
const c = chatClient.channel('team', 'some-channel-id');
c.watch().then(() => {
if (!interrupted) setChannel(c);
});
return () => {
setChannel(undefined);
interrupted = true;
if (c.disconnected) return;
c.stopWatching();
};
}, [chatClient]); I did some local testing trying to rapidly mount/unmount the whole chat component tree and while the amount of thrown errors has decreased, it's still not 0 and even with this approach I still sometimes get the error you mentioned. It's better than passing uninitialized channel instance directly to This issue - or rather - set of issues is pretty well known to us and is on our list but it's currently not prioritized. We'll update this thread once there's any progress. |
Describe the bug
Repeatedly, quickly mount and unmount a component that instantiates a
<Chat />
and connects on mount, then disconnects on unmount. Most of the time, this works fine. About 1–2% of the time, an unhandled error "You can't use a channel after client.disconnect() was called" is thrown, and the chat window no longer loads properly.To Reproduce
See description above. I don't have a fully self-contained repro. I can share the structure of our code (below), but the main evidence that I have is the following stack trace:
I captured this stack trace with my debugger paused on the
channel.ts
error site, "You can't use a channel after client.disconnect() was called". The lineChannel.js:207
corresponds to somewhere in this Babel-compiled code……which, from the
getChannel
call, must be this line:https://github.com/GetStream/stream-chat-react/blob/v11.8.0/src/components/Channel/Channel.tsx#L566
I don't see a clean fix that my code could employ to avoid this, since we're always passing in the same client, and the point where we're disconnecting is on unmount so we don't have the ability to do much else. We work around it for now by delaying 5000ms between unmounting and actually disconnecting, but this is undesirable for lots of obvious reasons. I think that
stream-chat-react
should guard its calls to channel/client methods to ensure that it does not call any when the client might be disconnected.Sample code structure
Expected behavior
A rendered
<Channel channel={channel} />
should never throw an internal error due to using a closed client.Screenshots
Package version
Desktop (please complete the following information):
Smartphone (please complete the following information):
Additional context
N/A
The text was updated successfully, but these errors were encountered: