Repository navigation
Race conditions between setTimeout() & setImmediate() #48163
Description
Activity
- addedflaky-testIssues and PRs involving tests that fail intermittently in CI.Issues and PRs involving tests that fail intermittently in CI.
on May 25, 2023 - addedlinuxIssues and PRs related to the Linux platform.Issues and PRs related to the Linux platform.
on May 25, 2023 Caused failure for #48159
The subject issue says
setTimeout() & setInterval()but looks like it's really aboutsetTimeout() & setImmediate()?Reacted by Raghu SaxenaCorrect, my bad. Let me fix it.
- changed the title
[-]Race conditions between setTimeout() & setInterval()[/-][+]Race conditions between setTimeout() & setImmediate()[/+]on May 25, 2023 The timer module only cares about relative time, so node chooses to use
gethrtime()as the time source, while theDate.now()is usinggettimeofday(). If the clock under thegethrtime()goes enough slower than the one undergettimeofday(), then 'IMMEDIATE' may print first.setTimeout(() => { console.log('TIMEOUT') }, 1); const now = Date.now(); while (Date.now() - now < 2); setImmediate(() => { console.log('IMMEDIATE'); })
Considering the test is testing timers explicitly, would it be better to change it to use
process.hrtime()in thenow/whilecondition instead?github-actions commented
on May 31, 2026 on May 31, 2026 – with GitHub ActionsContributorMore actionsThis issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on May 31, 2026 This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.
Test
test-timers-immediate-queue.js
Platform
Linux x64
Console output
Build links
Additional information
This seems to be some kinda potential race condition between
setTimeout()andsetImmediate()?For the following code, it seems
TIMEOUTorIMMEDIATEeither could be printed first, since over a few runs the order will flip.However on my machine, the following code will always result in
TIMEOUTbeing printed first - the while loop strategy is what the test usesI was wondering: is it possible due to some conditions on the host system, for
IMMEDIATEto print beforeTIMEOUTin the second case?I do not know much about the Event Loop internals, but from some rough reading,
setTimeout()will put calbacks in the timers queue,setImmediate()in the I/O queue, and the timers are always checked first (https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick)But if in the first case (
setTimeoutwithout while waiting loop) it is non-predictable, I wonder if there are other factors at play, which make the test flaky.