pusher / pusher-js

Pusher Javascript library
http://pusher.com
MIT License
2.11k stars 374 forks source link

Race condition when leaving and rejoining a channel #714

Closed hubertnnn closed 1 year ago

hubertnnn commented 1 year ago

Do you want to request a feature or report a bug? Bug

What is the current behavior? I have a use case where I use pusher to track time. Users sometimes enter a page and immediately are redirected back (eg. because of lack of permissions). As an effect I am leaving a channel and then immediately rejoining it.

Doing this seem to be causing a race condition where:

  1. I am getting an error Existing subscription to channel xxx
  2. User leave the channel and never returns causing a gap in my time tracking.

I assume the issue is due to pusher internally handling both events in random order. So it joins the channel first (causing Existing subscription error) and then leaves the channel (casing a gap in tracking).

As a workaround I have added throttling of messages going to pusher limiting subscribe/unsubscribe to run only once per second, but that did not solve the issue, just reduced it.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar.

pusher = ... // Create pusher instance
pusher.subscribe('test-channel');
await sleep(60000); // Wait a minute simulating staying on the channel
pusher.unsubscribe('test-channel'); // Leave the channel
await sleep(100); // 100ms sleep simulating a single request, or fast action
pusher.subscribe('test-channel'); // I am back

What is the expected behavior?

User should rejoin the channel and stay in it.

Which versions of Pusher, and which browsers / OS are affected by this issue? Did this work in previous versions of Pusher? If so, which?

Pusher 5.1.0

benjamin-tang-pusher commented 1 year ago

I will close this we spoke separately about this elsewhere.