-
Notifications
You must be signed in to change notification settings - Fork 7.3k
timers: fix processing of nested same delay timers #25763
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,13 +87,35 @@ function listOnTimeout() { | |
|
||
var first; | ||
while (first = L.peek(list)) { | ||
// If the previous iteration caused a timer to be added, | ||
// update the value of "now" so that timing computations are | ||
// done correctly. See test/simple/test-timers-blocking-callback.js | ||
// for more information. | ||
if (now < first._monotonicStartTime) { | ||
now = Timer.now(); | ||
debug('now: %d', now); | ||
// This handles the case of a timer that was created within a timers | ||
// callback with the same timeout value. For instance, when processing the | ||
// timer that would call `bar` in such code: | ||
// | ||
// setTimeout(function foo() { setTimeout(function bar() {}, 0) }, 0); | ||
// | ||
// or | ||
// | ||
// setTimeout(function foo() { setTimeout(function bar() {}, 500) }, 500); | ||
// | ||
// We want to make sure that newly added timer fires in the next turn of the | ||
// event loop at the earliest. So even if it's already expired now, | ||
// reschedule it to fire later. | ||
// | ||
// At that point, it's not necessary to process any other timer in that | ||
// list, because any remaining timer has been added within a callback of a | ||
// timer that has already been processed, and thus needs to be processed at | ||
// the earliest not in the current tick, but when the rescheduled timer will | ||
// expire. | ||
// | ||
// See: https://github.com/joyent/node/issues/25607 | ||
if (now <= first._monotonicStartTime) { | ||
var timeRemaining = msecs - (Timer.now() - first._monotonicStartTime); | ||
if (timeRemaining < 0) { | ||
timeRemaining = 0; | ||
} | ||
debug(msecs + ' list wait because timer was added from another timer'); | ||
list.start(timeRemaining, 0); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to just track if we are in a timeout and do the fix in like In general, touching the actual timeout loop seems pretty hairy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me wrap my head around this. I'm not sure what extra state, etc. would be required for your approach. Do you think the edge case, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Fishrock123 What did you have in mind more specifically? Some actual code would help. |
||
} | ||
|
||
var diff = now - first._monotonicStartTime; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
// Make sure we test 0ms timers, since they would had always wanted to run on | ||
// the current tick, and greater than 0ms timers, for scenarios where the | ||
// outer timer takes longer to complete than the delay of the nested timer. | ||
// Since the process of recreating this is identical regardless of the timer | ||
// delay, these scenarios are in one test. | ||
var scenarios = [0, 100]; | ||
|
||
scenarios.forEach(function (delay) { | ||
var nestedCalled = false; | ||
|
||
setTimeout(function A() { | ||
// Create the nested timer with the same delay as the outer timer so that it | ||
// gets added to the current list of timers being processed by | ||
// listOnTimeout. | ||
setTimeout(function B() { | ||
nestedCalled = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I would add a
|
||
}, delay); | ||
|
||
// Busy loop for the same timeout used for the nested timer to ensure that | ||
// we are in fact expiring the nested timer. | ||
common.busyLoop(delay); | ||
|
||
// The purpose of running this assert in nextTick is to make sure it runs | ||
// after A but before the next iteration of the libuv event loop. | ||
process.nextTick(function() { | ||
assert.ok(!nestedCalled); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest adding a comment that this |
||
}); | ||
|
||
// Ensure that the nested callback is indeed called prior to process exit. | ||
process.on('exit', function onExit() { | ||
assert.ok(nestedCalled); | ||
}); | ||
}, delay); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to keep these comments because they are still relevant: we still need to update
now
or useTimer.now()
to make sure timing computations are done correctly. However we may want to move these comments to were we useTimer.now()
right below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am already using
Timer.now()
to do the computation and since we didn't need to updatenow
, it's not used anywhere else and we quick return, I didn't think the previous comments made sense sticking around since we were no longer updatingnow
. Am I misunderstanding?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, it's probably self-explanatory in the current state of this PR. Thank you for questioning my comment :) 👍