Skip to content

Commit 381aef8

Browse files
erinishimotichacjihrig
authored andcommitted
timers: fix cleanup of nested same-timeout timers
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Julien Gilli <[email protected]>
1 parent 8d8d70d commit 381aef8

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

‎lib/timers.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ function listOnTimeout(){
211211
debug('%d list empty',msecs);
212212
assert(L.isEmpty(list));
213213
this.close();
214-
if(list._unrefed===true){
214+
215+
// Either refedLists[msecs] or unrefedLists[msecs] may have been removed and
216+
// recreated since the reference to `list` was created. Make sure they're
217+
// the same instance of the list before destroying.
218+
if(list._unrefed===true&&list===unrefedLists[msecs]){
215219
deleteunrefedLists[msecs];
216-
}else{
220+
}elseif(list===refedLists[msecs]){
217221
deleterefedLists[msecs];
218222
}
219223
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
/*
4+
* This is a regression test for https://github.com/nodejs/node/issues/7722.
5+
*
6+
* When nested timers have the same timeout, calling clearTimeout on the
7+
* older timer after it has fired causes the list the newer timer is in
8+
* to be deleted. Since the newer timer was not cleared, it still blocks
9+
* the event loop completing for the duration of its timeout, however, since
10+
* no reference exists to it in its list, it cannot be canceled and its
11+
* callback is not called when the timeout elapses.
12+
*/
13+
14+
constcommon=require('../common');
15+
constassert=require('assert');
16+
constTimer=process.binding('timer_wrap').Timer;
17+
18+
constTIMEOUT=common.platformTimeout(100);
19+
conststart=Timer.now();
20+
21+
// This bug also prevents the erroneously dereferenced timer's callback
22+
// from being called, so we can't use it's execution or lack thereof
23+
// to assert that the bug is fixed.
24+
process.on('exit',function(){
25+
constend=Timer.now();
26+
assert.equal(end-start<TIMEOUT*2,true,
27+
'Elapsed time does not include second timer\'s timeout.');
28+
});
29+
30+
consthandle1=setTimeout(common.mustCall(function(){
31+
// Cause the old TIMEOUT list to be deleted
32+
clearTimeout(handle1);
33+
34+
// Cause a new list with the same key (TIMEOUT) to be created for this timer
35+
consthandle2=setTimeout(function(){
36+
common.fail('Inner callback is not called');
37+
},TIMEOUT);
38+
39+
setTimeout(common.mustCall(function(){
40+
// Attempt to cancel the second timer. Fix for this bug will keep the
41+
// newer timer from being dereferenced by keeping its list from being
42+
// erroneously deleted. If we are able to cancel the timer successfully,
43+
// the bug is fixed.
44+
clearTimeout(handle2);
45+
setImmediate(common.mustCall(function(){
46+
setImmediate(common.mustCall(function(){
47+
constactiveHandles=process._getActiveHandles();
48+
constactiveTimers=activeHandles.filter(function(handle){
49+
returnhandleinstanceofTimer;
50+
});
51+
52+
// Make sure our clearTimeout succeeded. One timer finished and
53+
// the other was canceled, so none should be active.
54+
assert.equal(activeTimers.length,0,'No Timers remain.');
55+
}));
56+
}));
57+
}),10);
58+
59+
// Make sure our timers got added to the list.
60+
constactiveHandles=process._getActiveHandles();
61+
constactiveTimers=activeHandles.filter(function(handle){
62+
returnhandleinstanceofTimer;
63+
});
64+
constshortTimer=activeTimers.find(function(handle){
65+
returnhandle._list.msecs===10;
66+
});
67+
constlongTimers=activeTimers.filter(function(handle){
68+
returnhandle._list.msecs===TIMEOUT;
69+
});
70+
71+
// Make sure our clearTimeout succeeded. One timer finished and
72+
// the other was canceled, so none should be active.
73+
assert.equal(activeTimers.length,3,'There are 3 timers in the list.');
74+
assert(shortTimerinstanceofTimer,'The shorter timer is in the list.');
75+
assert.equal(longTimers.length,2,'Both longer timers are in the list.');
76+
77+
// When this callback completes, `listOnTimeout` should now look at the
78+
// correct list and refrain from removing the new TIMEOUT list which
79+
// contains the reference to the newer timer.
80+
}),TIMEOUT);

0 commit comments

Comments
(0)