-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
fix: Check if missed heartbeat synchronously before sending message #5134
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Chrome (and maybe other browsers) will throttle `setTimeout` under certain conditions, causing them to slow down, and this breaks the heartbeat detection logic. This fix means if a message is emitted when the connection should be considered expired (but the `setTimeout` hasn't fired yet), we close that socket and then buffer the message for the new connection. So now a message should never be sent over an expired connection. This doesn't solve the problem of connections being detected as expired too late. I think that could be done separately.
tjenkinson
force-pushed
the
check-heartbeat
branch
from
August 9, 2024 14:09
fd9ae84
to
0799c83
Compare
tjenkinson
force-pushed
the
check-heartbeat
branch
from
August 9, 2024 14:10
0799c83
to
e0f490c
Compare
tjenkinson
changed the title
[WIP] fix: Check if missed heartbeat synchronously before sending message
fix: Check if missed heartbeat synchronously before sending message
Aug 9, 2024
Looking at CI maybe this needs to be 2 separate PR's? |
darrachequesne
pushed a commit
that referenced
this pull request
Sep 18, 2024
When a laptop is suspended or a phone is locked, the timer that is used to check the liveness of the connection is paused and is not able to detect that the heartbeat has failed. Previously, emitting a message after resuming the page would lose the message. The status of the timer will now be checked before sending the message, so that it gets buffered and sent upon reconnection. Note: we could also have used the Page Visibility API or a custom setTimeout() method based on setInterval(), but this would not be as reliable as the current solution. Reference: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API Related: #5135
Merged as 8adcfbf. I've made some minor edits and added a few tests. Could you please check if everything makes sense? |
Thanks! Just this one comment 8adcfbf#r146871962 |
This was referenced Sep 21, 2024
Closing given merged in 8adcfbf 🎉 |
This was referenced Oct 14, 2024
This was referenced Oct 14, 2024
This was referenced Oct 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Chrome (and maybe other browsers) will throttle
setTimeout
under certain conditions, causing them to slow down, and this breaks the heartbeat detection logic.This fix means if a message is emitted when the connection should be considered expired (but the
setTimeout
hasn't fired yet), we close that socket and then buffer the message for the new connection. So now a message should never be sent over an expired connection.This doesn't solve the problem of connections being detected as expired too late. I think that could be done separately.
The kind of change this PR does introduce
Current behavior
Check the repro on #5135
New behavior
Other information (e.g. related issues)
The reason that the heartbeat logic is not immediately closing the old connection when the page is woken up is due to chrome pausing the
setTimeout
that's used in the heartbeat logic. This PR doesn't fix that. I Think that could be handled separately.#3507 (comment) is another fix related to throttled timers
Let me know if you think this makes sense and I'm happy to look into adding a test for it