Skip to content

fix setTimeout inconsistencies#6034

Merged
anonrig merged 1 commit intomainfrom
yagiz/fix-settimeout-inconsistencies
Feb 6, 2026
Merged

fix setTimeout inconsistencies#6034
anonrig merged 1 commit intomainfrom
yagiz/fix-settimeout-inconsistencies

Conversation

@anonrig
Copy link
Copy Markdown
Member

@anonrig anonrig commented Feb 6, 2026

Fixes #6019

WorkerService::atTime() was scheduling timers using kj::Timer::afterDelay(delay), which internally computes the target time as timer.now() + delay. However, kj::Timer::now() only updates when the event loop polls for I/O — it stays frozen while JavaScript is executing. If significant CPU time elapsed since the last poll (e.g. compiling and running a script before the first setTimeout), timer.now() would be stale by that amount, causing afterDelay() to schedule the timer at a target time that was effectively in the past relative to the real monotonic clock. This made the first setTimeout(fn, 100) resolve in as little as 1ms of wall-clock time, while subsequent calls (which ran after the event loop had polled and refreshed the timer) worked correctly.

@anonrig anonrig requested a review from kentonv February 6, 2026 17:14
@anonrig anonrig requested review from a team as code owners February 6, 2026 17:14
@anonrig anonrig requested a review from jasnell February 6, 2026 17:14
@anonrig anonrig force-pushed the yagiz/fix-settimeout-inconsistencies branch 3 times, most recently from 86bc0c6 to 65bb6b0 Compare February 6, 2026 18:18
@anonrig anonrig force-pushed the yagiz/fix-settimeout-inconsistencies branch from 65bb6b0 to afa233f Compare February 6, 2026 19:00
@anonrig anonrig enabled auto-merge February 6, 2026 19:00
@anonrig anonrig merged commit 8d2733c into main Feb 6, 2026
21 checks passed
@anonrig anonrig deleted the yagiz/fix-settimeout-inconsistencies branch February 6, 2026 19:12
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.

setTimeout is too inaccurate

2 participants