-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
clearTimeout with invalid handle cause program runs longer #7722
Comments
This is not a duplicate of #5426. Applying the fix in #3063 on top of current node's master still allows to reproduce the problem described in this issue. The problem that this issue describes is that, in the following code: var handle = setTimeout(function(){
clearTimeout(handle);
handle = setTimeout( function() {
}, 10000 );
setTimeout( function() {
clearTimeout( handle );
}, 500 );
}, 10000); when the callback of the first timer with a 10000 delay fires, this is (some of) what happens:
Here's a quick fix that I put together to demonstrate the problem. This is not a definite fix, it's mentioned here just to illustrate the problem. It is likely that a better fix can be written: diff --git a/lib/timers.js b/lib/timers.js
index 7379cfe..b7f2259 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -212,9 +212,11 @@ function listOnTimeout() {
assert(L.isEmpty(list));
this.close();
if (list._unrefed === true) {
- delete unrefedLists[msecs];
+ if (list === unrefedLists[msecs])
+ delete unrefedLists[msecs];
} else {
- delete refedLists[msecs];
+ if (list === refedLists[msecs])
+ delete refedLists[msecs];
}
} With that fix, the problem cannot be reproduced and all tests pass. I'll try to dig a bit deeper in the next few days to come up with a better fix and tests, but if anyone is willing to take a shot at it, I'd be glad to help them too. From what I described above, it seems this issue should be kept open, thus I'm reopening it. |
Oh nice catch, I think I see what you are saying. Yes, that would be a bug in my refactoring. The patch could probably be simplified a bit also. |
@misterdjules we don't really label the effected versions quite like that these days though...
|
That's quite possible, the current diff was added just to illustrate the problem, I'll clarify that in my comment.
This bug also affects some v0.x versions, at least latest v0.10.x and v0.12.x versions. I had tested it was the case. If you run the code mentioned by the reporter of this issue with node v0.10.x and v0.12.x, I believe you'll reach the same conclusions. That bug wasn't caused by the refactoring in #4007. For instance, in v0.10.x versions it is caused by the same problem described in my comment above, the fact that a list of timers created in a nested timer's callback can be incorrectly destroyed by the outer timer's callback.
I find it very useful to document which versions are affected by a given bug. In this case, anyone coming back to this later would be able to determine which versions are affected without reading through all the comments and without having to run tests themselves. Is it that we're using different labels for that now? If so, then let's set the new labels, otherwise I don't understand how adding valuable metadata would hurt. |
ergh, yes I see how it it also exists in 0.x, the fix is similar. |
@Fishrock123 The problem also affects the master branch, shouldn't we keep the |
For nested timers with the same timeout, we can get into a situation where we have recreated a timer list immediately before we need to clean up an old timer list with the same key. Fix: make sure the list to be deleted is the same instance as the list whose reference was used to determine that a cleanup is necessary. If it's not the same instance, a new list with the same key has been created, and it should not be deleted. Fixes: nodejs#7722
The fix in 7d75338 should be able to make its way to v6.x and v4.x releases soon. It's unlikely it will make it to any v0.12 and v0.10 release unless there's enough evidence that a significant number of users are impacted by it on these release lines. |
For nested timers with the same timeout, we can get into a situation where we have recreated a timer list immediately before we need to clean up an old timer list with the same key. Fix: make sure the list to be deleted is the same instance as the list whose reference was used to determine that a cleanup is necessary. If it's not the same instance, a new list with the same key has been created, and it should not be deleted. Fixes: #7722 PR-URL: #7827 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Julien Gilli <jgilli@fastmail.fm>
This program cost 20 seconds, but 10.5 seconds was expected:
But all below codes work as expected:
Does clearTimeout with a invalid handle causes this problem?
The text was updated successfully, but these errors were encountered: