You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
help wantedIssues that need assistance from volunteers or PRs that need help to proceed.timersIssues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().
Platform: Darwin Charlies-MacBook-Pro.local 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64
I'm trying to test this code with the following test:
it("Should wait for given time before resolving",async()=>{constMS=100;conststart=process.hrtime();awaittimeout(MS);constdiff=process.hrtime(start);expect(((diff[0]*NS_PER_SEC)+diff[1])/1000000).to.at.least(MS);});
The problem is sometimes (rarely), this test fails:
Should wait for given time before resolving:
AssertionError: expected 99.595337 to be at least 100
+ expected - actual
-99.595337
+100
Obviously this is some type of timing issue with Node.js or something. If anything I expect await timeout(MS); to take slightly longer than MS. In no case do I expect it to take less time.
What is it about the internals of JavaScript/Node.js that causes this to happen?
This occurred on macOS 10.14.3 running Node.js version 8.15.1.
Reacted by Leonardo, Niklas Wenzel, Lars Weber, Bryan Hoang, Juraj Zovinec, Bailey Pearson and Min Nyan Lin
I'm able to replicate using Node.js 8.15.1, 10.15.3, and 11.11.0. Here's the code I used. Start it and let it run for maybe 20 seconds or so and it will produce the error.
I'm reasonably sure this is caused by the interaction between setImmediate() and setTimeout() because just setTimeout() doesn't reproduce it. To wit, this fails after some time:
functiontest(){conststart=process.hrtime.bigint()setTimeout(next,100)functionnext(){constnow=process.hrtime.bigint()constdelta=now-startconsole.log(Number(delta/BigInt(1e6)),Number(delta%BigInt(1e6)))if(delta<BigInt(100*1e6))throw'fail'test()// no setImmediate()}}test()
My guess is that something somewhere does or doesn't call uv_update_time(), causing a slight discrepancy in the time base used for different timers.
Reacted by Luigi Pinca, Rich Trott, Weijia Wang, ZYSzys and Alex Yang
As @Fishrock123 pointed out, this is kind of expected due to the rounding hence it usually being off by sub 1ms. It also makes sense that it only occurs when there's other process activity.
Think about it like this:
You schedule a timer for 50ms at 100.2ms which is equivalent to 100ms.
If you don't have any other code running, libuv will let the system poll for 100ms which will be exact.
You run the callback roughly at 150.2ms which is exactly 50ms later.
Now imagine you introduce other code in there:
You schedule a timer for 50ms at 100.2ms which is equivalent to 100ms.
You schedule another timer for 48ms at 101ms.
You poll for for 48ms.
You come back to your second timer at 149ms and run its callback which, say, takes exactly 1ms.
At 150ms, both libuv and node are under the impression that your timer is ready to fire even though it's still technically 0.2ms off.
There's no real solution here other than making timers use more precise units under the hood.
It is possible that this bug was fixed and was reintroduced again.
Timers have always run with millisecond precision, so no, that's not really plausible.
What is plausible, though, is that the underlying operation of Node.js was sufficiently altered by that commit that some test-for-timer-precision code that used to fail will pass and/or some that used to pass will fail. But that's all about underlying and often-unpredictable (and sometimes OS-dependent) quirks, not about the millisecond precision not being there. For example, a code change that makes timers less efficient will, ironically, reduce the likelihood that the timer runs "early", thus making timers appear to be less buggy. But the issue is still there.
I appreciate that you took the time to do a git bisect on this issue, though. I imagine that took a long time to run. But, IIUC, timers have always run with millisecond precision in Node.js so there's always been a rounding-from-999.9-to-1000 issue in certain code. (/ping @nodejs/timers for correction/verification)
120-second timeout triggered after 110.096 seconds
1800-second timeout triggered after 1660 seconds
It appears the longer the timeout, the worse precision I get and the error accumulates. I noticed, however, that in prod (docker container on AWS EC2), the issue did not happen.
For anyone running into these problems with WSL2 as in the above comment, or wondering how things could get so bad: it looks like WSL2 is a good test case that demonstrates the difference between Date.now() and performance.now(). Running the following simple loop:
in WSL2 shows a few seconds(!) adjustment every 30s, but this is not always happening. The huge adjustment also make look like it's not just some NTP syncs (even 70s casio watches were far from being so bad).
My guess is that this is some timer syncing that responds to hypervisor priorities for the WSL2 VM. Running the same natively (on a linux, though I think that linux-vs-windows is a red herring) doesn't show any hiccups.
Most people consider them roughly the same with different precision, but the huge difference in behavior in WSL2's VM vs a native run show that they're very different. I'd even go as far as saying that their respective results should be considered different types.
This 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.
This issue has been marked as stale due to 90 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.
help wantedIssues that need assistance from volunteers or PRs that need help to proceed.timersIssues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().
Activity
devsnek commentedon Mar 11, 2019
@bnoordhuis iirc there was another issue about timeouts firing early but i can't find it
ZYSzys commentedon Mar 11, 2019
@devsnek I think you were saying #26155 if I'm not wrong.
Trott commentedon Mar 11, 2019
/ping @nodejs/timers
I'm able to replicate using Node.js 8.15.1, 10.15.3, and 11.11.0. Here's the code I used. Start it and let it run for maybe 20 seconds or so and it will produce the error.
OS info:
lpinca commentedon Mar 11, 2019
Can reproduce with the above test case.
Trott commentedon Mar 11, 2019
I'd be interested to know if non-macOS users are seeing this or not.
targos commentedon Mar 11, 2019
I can reproduce on Windows:
Fishrock123 commentedon Mar 11, 2019
fwiw:
libuv loop times only run with millisecond precision:
uv_nowhttp://docs.libuv.org/en/v1.x/loop.html?highlight=uv_now#c.uv_nowuv_timer_thttp://docs.libuv.org/en/v1.x/timer.htmlThe comes from the fact that libuv only tacks loop times to millisecond precision: https://github.com/libuv/libuv/blob/2d2af382ce84b91d6ee7a185af32fca7f0acd84b/src/unix/internal.h#L289-L293
I do think that should round correctly though?
Measuring this "correctly" shows no issue though:
lpinca commentedon Mar 11, 2019
This is reproducible even with
Date.now()so I guessDate.now()anduv_now()work differently.Fishrock123 commentedon Mar 11, 2019
Correct,
uv_now()is, last I recall, more 'stable' thanDate.now().bnoordhuis commentedon Mar 12, 2019
I'm reasonably sure this is caused by the interaction between setImmediate() and setTimeout() because just setTimeout() doesn't reproduce it. To wit, this fails after some time:
Whereas this keeps on running:
My guess is that something somewhere does or doesn't call
uv_update_time(), causing a slight discrepancy in the time base used for different timers.apapirovski commentedon Mar 17, 2019
As @Fishrock123 pointed out, this is kind of expected due to the rounding hence it usually being off by sub 1ms. It also makes sense that it only occurs when there's other process activity.
Think about it like this:
Now imagine you introduce other code in there:
There's no real solution here other than making timers use more precise units under the hood.
32 remaining items
Trott commentedon Oct 21, 2023
What is plausible, though, is that the underlying operation of Node.js was sufficiently altered by that commit that some test-for-timer-precision code that used to fail will pass and/or some that used to pass will fail. But that's all about underlying and often-unpredictable (and sometimes OS-dependent) quirks, not about the millisecond precision not being there. For example, a code change that makes timers less efficient will, ironically, reduce the likelihood that the timer runs "early", thus making timers appear to be less buggy. But the issue is still there.
I appreciate that you took the time to do a
git bisecton this issue, though. I imagine that took a long time to run. But, IIUC, timers have always run with millisecond precision in Node.js so there's always been a rounding-from-999.9-to-1000 issue in certain code. (/ping @nodejs/timers for correction/verification)panfiva commentedon Aug 7, 2025
In WSL:
It appears the longer the timeout, the worse precision I get and the error accumulates. I noticed, however, that in prod (docker container on AWS EC2), the issue did not happen.
ebarzilay-godaddy commentedon Aug 7, 2025
For anyone running into these problems with WSL2 as in the above comment, or wondering how things could get so bad: it looks like WSL2 is a good test case that demonstrates the difference between
Date.now()andperformance.now(). Running the following simple loop:in WSL2 shows a few seconds(!) adjustment every 30s, but this is not always happening. The huge adjustment also make look like it's not just some NTP syncs (even 70s casio watches were far from being so bad).
My guess is that this is some timer syncing that responds to hypervisor priorities for the WSL2 VM. Running the same natively (on a linux, though I think that linux-vs-windows is a red herring) doesn't show any hiccups.
Most people consider them roughly the same with different precision, but the huge difference in behavior in WSL2's VM vs a native run show that they're very different. I'd even go as far as saying that their respective results should be considered different types.
feat: remove use of nanotimer
timers: prevent setTimeout from firing callback early
doc: clarify setTimeout millisecond precision behavior
[responseOps][task manager] fix timing issue in event loop delay test (…
github-actions commentedon Jun 27, 2026
This 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.
SystemParadox commentedon Jun 29, 2026
This is still the case with node 24.18.0 and 26.4.0.
github-actions commentedon Sep 29, 2026
This issue has been marked as stale due to 90 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.
SystemParadox commentedon Sep 29, 2026
This is still relevant.