Commit 3708eac
committed
timers: prevent setTimeout from firing callback early
libuv's uv_now() truncates sub-millisecond time to integer
milliseconds. When a timer is scheduled at a fractional millisecond
boundary (e.g., real time 100.7ms, uv_now() returns 100), the timer
can fire up to 1ms before the requested delay has actually elapsed
when measured with high-resolution clocks like process.hrtime() or
Date.now().
Add 1ms to the duration passed to uv_timer_start() in
Environment::ScheduleTimer to compensate for this truncation. This
ensures that setTimeout/setInterval callbacks never fire before the
requested delay, at the cost of up to 1ms additional latency.
The fix is applied at the C++ boundary (ScheduleTimer) rather than in
JavaScript because it covers both scheduling paths: the initial
schedule from JS (binding.scheduleTimer) and the rescheduling after
timer processing in RunTimers.
Fixes: #26578
Signed-off-by: V Govindarajan <vijay.govindarajan91@gmail.com>1 parent 7d1f1b4 commit 3708eac
2 files changed
Lines changed: 50 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1505 | 1505 | | |
1506 | 1506 | | |
1507 | 1507 | | |
1508 | | - | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
1509 | 1513 | | |
1510 | 1514 | | |
1511 | 1515 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
0 commit comments