Skip to content

Keep the heartbeat going when its timer fires early - #8141

Open
b84330808 wants to merge 1 commit into
coder:mainfrom
b84330808:fix-heartbeat-early-timer
Open

b84330808 wants to merge 1 commit into
coder:mainfrom
b84330808:fix-heartbeat-early-timer

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Fixes #5969

Every minute the heartbeat timer checks for open connections and calls beat() again if there are any. beat() returns early while alive() is true, and alive() compares Date.now() with the last beat. Timers don't run on the Date.now() clock, so the timer can fire when Date.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 happens beat() 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 to expired, so --idle-timeout-seconds doesn'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 through beat(), 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 (isActive is called once instead of twice) and passes with it.

I also ran the real Heart class 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.

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.
@b84330808
b84330808 requested a review from a team as a code owner November 18, 2026 14:12
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Heartbeat not being updated while user edits files or has terminal activity.

1 participant