Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66155 +/- ##
==========================================
- Coverage 90.52% 90.50% -0.02%
==========================================
Files 803 805 +2
Lines 285308 285766 +458
Branches 55087 55163 +76
==========================================
+ Hits 258272 258642 +370
- Misses 17157 17206 +49
- Partials 9879 9918 +39
🚀 New features to boost your workflow:
|
Does this mean at least one event loop turn or within an event loop turn? |
jasnell
left a comment
There was a problem hiding this comment.
LGTM once CI is green and the "as soon as possible" is clarified.
A setTimeout() delay of 0 (or a positive sub-millisecond delay, which is truncated to 0 by insert()) is now scheduled as soon as possible instead of being clamped to 1 ms, matching browser behavior. Negative delays, NaN, and values above TIMEOUT_MAX are still clamped to 1 ms, and setInterval() keeps clamping delays below 1 ms to 1 ms to avoid firing as fast as the event loop allows. Refs: nodejs#46596 PR-URL: nodejs#66155 Assisted-by: pi Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
1ddf9b2 to
8d92515
Compare
| const order = []; | ||
|
|
||
| setTimeout(common.mustCall(() => order.push('one')), 1); | ||
| setTimeout(common.mustCall(() => order.push('zero')), 0); |
There was a problem hiding this comment.
In the CI test failure.. was worried about this. The "as soon as possible" means that it's going to race against non-zero but very short deadline timers in some cases. Think we need to put some exact timing semantics around it.
[
+ 'one',
'zero',
- 'one'
]
A setTimeout() delay of 0 (or a positive sub-millisecond delay, which is truncated to 0 by insert()) is now scheduled as soon as possible instead of being clamped to 1 ms, matching browser behavior. Negative delays, NaN, and values above TIMEOUT_MAX are still clamped to 1 ms, and setInterval() keeps clamping delays below 1 ms to 1 ms to avoid firing as fast as the event loop allows. Refs: nodejs#46596 PR-URL: nodejs#66155 Assisted-by: pi Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
8d92515 to
b008bbe
Compare
|
Is it ok that such a behavior change goes into a semver patch version? |
|
Marked as a major |
Failed to start CIFull Auto Start CI output |
A setTimeout() delay of 0 (or a positive sub-millisecond delay, which is truncated to 0 by insert()) is now scheduled as soon as possible instead of being clamped to 1 ms, matching browser behavior. Negative delays, NaN, and values above TIMEOUT_MAX are still clamped to 1 ms, and setInterval() keeps clamping delays below 1 ms to 1 ms to avoid firing as fast as the event loop allows. Refs: nodejs#46596 PR-URL: nodejs#66155 Assisted-by: pi Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
b008bbe to
8380a6b
Compare
ShogunPanda
left a comment
There was a problem hiding this comment.
I think a zero delay breaks the guarantee that a timer always yields to the event loop:
- In
listOnTimeout, a timer runs whennow - timer._idleStart >= msecs. AsetTimeout(f, 0)scheduled from inside a timer callback is appended to the same 0 ms list that is currently being processed, with_idleStart === now(millisecond resolution). Withmsecs === 0the check is always true, so it runs in the same timers phase. A function rescheduling itself withsetTimeout(f, 0)then spins without doing any I/O until the clock moves by 1 ms. The 1 ms clamp prevented exactly this, and it contradicts the new doc sentence ("It does not run within the current turn"). Browsers clamp nested timers to 4 ms for the same reason. I think the 0 ms list needs to defer timers created during its own processing (e.g. compare_idleStartagainst thenowof the current pass), with a test that runs a nestedsetTimeout(f, 0)against an I/O callback. test-timers-zero-delay-ordering.jsis inherently flaky: if the millisecond ticks between the two calls, the 1 ms and 0 ms lists end up with the same expiry and the older list (1 ms) wins the tie. That is the failure @jasnell saw in CI. Since the docs make no ordering guarantee, I'd rather not assert it, or assert it in a deterministic way.- Nit: the comment next to
after = 1still says "Schedule on next tick, follows browser behavior", which is now misleading.
Summary
setTimeout()now accepts a delay of0(and positive sub-millisecond delays, which are truncated to0byinsert()), scheduling the callback as soon as possible instead of silently clamping it to1 ms. This matches browser (Chrome) behavior and resolves the ordering surprise described in #46596.Negative delays,
NaN, and values aboveTIMEOUT_MAXare still clamped to1 mswith the existing warnings.setInterval()continues to clamp delays below1 msto1 msso it does not fire as fast as the event loop allows.Behavior change
Before:
After (matches browsers):
Test plan
test/parallel/test-timers-zero-delay-ordering.jsverifiessetTimeout(fn, 0)runs beforesetTimeout(fn, 1)and thattimers/promises.setTimeout(0)resolves.test-timers*/ mock-timers / promisified tests pass, plus a broader cross-subsystem subset (vm, worker, stream, http, net).Refs: #46596