-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port from joyent/node: timers: fix timeout when added in timer's callback
#2232
Conversation
When a timer is added in another timer's callback, its underlying timer handle will be started with a timeout that is actually incorrect. The reason is that the value that represents the current time is not updated between the time the original callback is called and the time the added timer is processed by timers.listOnTimeout. That leads the logic in timers.listOnTimeout to do an incorrect computation that makes the added timer fire with a timeout of scheduledTimeout + timeSpentInCallback. This change fixes that and make timers scheduled within other timers' callbacks fire as expected. Fixes: nodejs/node-v0.x-archive#9333 Fixes: nodejs/node-v0.x-archive#15447 PR: nodejs/node-v0.x-archive#17203 PR-URL: nodejs/node-v0.x-archive#17203 Reviewed-By: Fedor Indutny <[email protected]> Conflicts: lib/timers.js test/common.js
timers: fix timeout when added in timer's callback
@@ -451,5 +451,9 @@ exports.fileExists = function(pathname) { | |||
return true; | |||
} catch (err) { | |||
return false; | |||
} |
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.
This catch block and the function is not finished, I think
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.
Oops, that was a merge conflict mis-resolve.
I wouldn't mind helping port nodejs/node-v0.x-archive#25763 if it's not already been done. |
@whitlockjc I'll be yeah, I figured I'd probably let Julien review it over there first. |
Updated, PTAL @misterdjules / @bnoordhuis |
exports.busyLoop = function busyLoop(time) { | ||
var startTime = new Date().getTime(); | ||
var stopTime = startTime + time; | ||
while (new Date().getTime() < stopTime); |
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.
Date.now() here and two lines up?
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.
Hmm, in the new patch joyent/node will be using Timer.now()
-- what's the difference between it and Date.now()
?
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.
Timer.now()
has a fighting chance of fitting in an SMI (a tagged integer), the return value of Date.now()
is always a heap-allocated double.
LGTM with suggestions. The 100 ms timeout may end up being flaky on some of the CI machines. |
Hmm, getting this with the patch locally:
I'm guessing that test exposes something wrong with this patch, since the test is not present in joyent/node. Perhaps the same bug nodejs/node-v0.x-archive#25763 is attempting to rectify? Test origin commit: ebf9f29 |
@@ -453,3 +453,9 @@ exports.fileExists = function(pathname) { | |||
return false; | |||
} | |||
}; | |||
|
|||
exports.busyLoop = function busyLoop(time) { | |||
var startTime = Date.now(); |
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.
const
? Also do we really need this? I mean we can directly use Date.now()
in the following statement no?
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.
Ah nice, true.
@Fishrock123 ... what's the status on this one? |
@jasnell was waiting on nodejs/node-v0.x-archive#25763 |
@Fishrock123 The failure in Since nodejs/node-v0.x-archive#25763 fixes this problem. |
closing in favor of #3063 |
#3063 has been updated to include this change and the test/parallel/test-timers-blocking-callback.js test per @misterdjules' request. |
We'll probably also want to port nodejs/node-v0.x-archive#25763
cc @misterdjules / @bnoordhuis