bug: whole web page occasionally crashes if the user is added to a new channel and immediately receives a message from that channel. #2474
Labels
bug
Something isn't working
status:confirmed
Described issue has been reproduced by the repo maintainer
Describe the bug
The whole web page occasionally crashes if the user is added to a new channel and immediately receives a message from that channel.
To Reproduce
Steps to reproduce the behavior:
Please read my "Additional Context" to understand what's going on and why this is only a 10% repro.
Expected behavior
The web page should not crash.
Package version
Additional context
I know that if I reported a bug that only reproduces 10% of the time, my bug report would probably get buried. I took it upon myself to debug the issue and found the root cause. I will explain my findings and suggest a pretty obvious fix. I hope this can speed up Stream prioritizing this bug.
The stack trace of the crash:
How the code is supposed to work:
getChannel
will call .watch() on the channel.stream-chat-react/src/components/ChannelList/hooks/useNotificationAddedToChannelListener.ts
Line 29 in afcd40f
stream-chat-react/src/components/ChannelList/hooks/useMessageNewListener.ts
Lines 34 to 35 in afcd40f
90% of the time the events happen in the order of 1, 2, 3, 4. There is no crash because in step 4, the channel is already watched (aka initialized).
But 10% of the time, the events happen in the order of 1, 2, 4, 3. In 4, an uninitialized channel is added to ChannelList, resulting in the stack trace of crash above.
Now, why would 4 happen before 3? Isn't "message.new" supposed to fire AFTER a channel is watched?
The race condition happens because .watch() is asynchronous. Somewhere in the middle of that call, the web socket is established and the "message.new" event comes down BEFORE the whole .watch() is resolved.
Obviously this is a race condition that doesn't happen all the time. It really depends on how soon the message is sent after the user is added to the channel. It also depends on how much time elapses between the establishment of web socket and finishing the rest of .watch() call.
In our product, every user goes through a flow in onboarding where they add themselves into a chat channel and we immediately send a message when you are added. About 10% of the times, a user's first impression of our product is a crashed web page ....
A simple fix would be to call getChannel in useMessageNewListener
The text was updated successfully, but these errors were encountered: