Repository navigation
Conversation
The heartbeat timer re-checks for active connections every minute and calls beat() again when there are some. beat() returns early while alive() is true, and alive() compares Date.now() with the last beat. Timers do not run on the Date.now() clock, so the timer can fire when Date.now() is still a millisecond or more short of the full minute. In that case beat() returns without writing the file or scheduling the next check, and the heartbeat stays stopped until the next HTTP request, even though the browser is still connected over the web socket. Have the timer write the heartbeat and reschedule directly instead of going through the alive() check.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or 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
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.
Fixes #5969
Every minute the heartbeat timer checks for open connections and calls
beat()again if there are any.beat()returns early whilealive()is true, andalive()comparesDate.now()with the last beat. Timers don't run on theDate.now()clock, so the timer can fire whenDate.now()still shows a millisecond or more less than the full minute (more if the system clock is being slewed or stepped back). When that happensbeat()returns without writing the file or scheduling the next check, and nothing restarts it until the next plain HTTP request. Terminal and editor traffic goes over the web socket that's already open, so it doesn't count. I think this is what #5969 runs into: the file stops updating after a while, and only opening a new file or reloading the tab starts it again. It also means the state never goes toexpired, so--idle-timeout-secondsdoesn't shut down after the last client leaves.The timer now writes the heartbeat and schedules the next check directly instead of going through
alive(). Requests still go throughbeat(), so they still write at most once per interval.Added a test that moves the fake clock back 1 ms before the timer fires. It fails without the change (
isActiveis called once instead of twice) and passes with it.I also ran the real
Heartclass with real timers and the interval set to 20 ms, on Windows 11 with Node 24.15: 4 runs before and 4 after the change, alternating, up to 1500 checks each. Before the change the heartbeat stopped in 2 of 4 runs (after 176 and 464 checks), each time right after the timer fired early. After the change it never stopped, including one run where the timer fired early.