Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit fd2cb7c

Browse files
author
Julien Gilli
committed
timers: don't mutate unref list while iterating it
Commit 934bfe2 had introduced a regression where node would crash trying to access a null unref timer if a given unref timer's callback would remove other unref timers set to fire in the future. More generally, it makes the unrefTimeout function more solid by not mutating the unrefList while traversing it. Fixes #8897. Reviewed-By: Timothy J Fontaine <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 1425ccd commit fd2cb7c

4 files changed

+221
-35
lines changed

lib/timers.js

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,38 @@ exports.clearImmediate = function(immediate) {
404404

405405
var unrefList, unrefTimer;
406406

407+
function _makeTimerTimeout(timer) {
408+
var domain = timer.domain;
409+
var msecs = timer._idleTimeout;
410+
411+
// Timer has been unenrolled by another timer that fired at the same time,
412+
// so don't make it timeout.
413+
if (!msecs || msecs < 0)
414+
return;
415+
416+
if (!timer._onTimeout)
417+
return;
418+
419+
if (domain && domain._disposed)
420+
return;
421+
422+
try {
423+
var threw = true;
424+
425+
if (domain) domain.enter();
426+
427+
debug('unreftimer firing timeout');
428+
L.remove(timer);
429+
timer._onTimeout();
430+
431+
threw = false;
432+
433+
if (domain)
434+
domain.exit();
435+
} finally {
436+
if (threw) process.nextTick(unrefTimeout);
437+
}
438+
}
407439

408440
function unrefTimeout() {
409441
var now = Timer.now();
@@ -414,7 +446,7 @@ function unrefTimeout() {
414446
var nextTimeoutTime;
415447
var nextTimeoutDuration;
416448
var minNextTimeoutTime;
417-
var itemToDelete;
449+
var timersToTimeout = [];
418450

419451
// The actual timer fired and has not yet been rearmed,
420452
// let's consider its next firing time is invalid for now.
@@ -445,44 +477,21 @@ function unrefTimeout() {
445477
// we scanned through the whole list.
446478
minNextTimeoutTime = nextTimeoutTime;
447479
}
448-
449-
// This timer hasn't expired yet, skipping
450-
cur = cur._idlePrev;
451-
continue;
480+
} else {
481+
// We found a timer that expired. Do not call its _onTimeout callback
482+
// right now, as it could mutate any item of the list (including itself).
483+
// Instead, add it to another list that will be processed once the list
484+
// of current timers has been fully traversed.
485+
timersToTimeout.push(cur);
452486
}
453487

454-
// We found a timer that expired
455-
var domain = cur.domain;
456-
457-
if (!cur._onTimeout) continue;
458-
459-
if (domain && domain._disposed)
460-
continue;
461-
462-
try {
463-
var threw = true;
464-
465-
if (domain) domain.enter();
466-
467-
itemToDelete = cur;
468-
// Move to the previous item before calling the _onTimeout callback,
469-
// as it can mutate the list.
470-
cur = cur._idlePrev;
471-
472-
// Remove the timeout from the list because it expired.
473-
L.remove(itemToDelete);
474-
475-
debug('unreftimer firing timeout');
476-
itemToDelete._onTimeout();
488+
cur = cur._idlePrev;
489+
}
477490

478-
threw = false;
491+
var nbTimersToTimeout = timersToTimeout.length;
492+
for (var timerIdx = 0; timerIdx < nbTimersToTimeout; ++timerIdx)
493+
_makeTimerTimeout(timersToTimeout[timerIdx]);
479494

480-
if (domain)
481-
domain.exit();
482-
} finally {
483-
if (threw) process.nextTick(unrefTimeout);
484-
}
485-
}
486495

487496
// Rearm the actual timer with the timeout delay
488497
// of the earliest timeout found.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
/*
23+
* This test is a regression test for joyent/node#8897.
24+
*/
25+
26+
var common = require('../common');
27+
var assert = require('assert');
28+
var net = require('net');
29+
30+
var clients = [];
31+
32+
var server = net.createServer(function onClient(client) {
33+
clients.push(client);
34+
35+
if (clients.length === 2) {
36+
/*
37+
* Enroll two timers, and make the one supposed to fire first
38+
* unenroll the other one supposed to fire later. This mutates
39+
* the list of unref timers when traversing it, and exposes the
40+
* original issue in joyent/node#8897.
41+
*/
42+
clients[0].setTimeout(1, function onTimeout() {
43+
clients[1].setTimeout(0);
44+
clients[0].end();
45+
clients[1].end();
46+
});
47+
48+
// Use a delay that is higher than the lowest timer resolution accross all
49+
// supported platforms, so that the two timers don't fire at the same time.
50+
clients[1].setTimeout(50);
51+
}
52+
});
53+
54+
server.listen(common.PORT, '127.0.0.1', function() {
55+
var nbClientsEnded = 0;
56+
57+
function addEndedClient(client) {
58+
++nbClientsEnded;
59+
if (nbClientsEnded === 2) {
60+
server.close();
61+
}
62+
}
63+
64+
var client1 = net.connect({ port: common.PORT })
65+
client1.on('end', addEndedClient);
66+
67+
var client2 = net.connect({ port: common.PORT });
68+
client2.on('end', addEndedClient);
69+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
/*
23+
* The goal of this test is to make sure that, after the regression introduced
24+
* by 934bfe23a16556d05bfb1844ef4d53e8c9887c3d, the fix preserves the following
25+
* behavior of unref timers: if two timers are scheduled to fire at the same
26+
* time, if one unenrolls the other one in its _onTimeout callback, the other
27+
* one will *not* fire.
28+
*
29+
* This behavior is a private implementation detail and should not be
30+
* considered public interface.
31+
*/
32+
var timers = require('timers');
33+
var assert = require('assert');
34+
35+
var nbTimersFired = 0;
36+
37+
var foo = new function() {
38+
this._onTimeout = function() {
39+
++nbTimersFired;
40+
timers.unenroll(bar);
41+
};
42+
}();
43+
44+
var bar = new function() {
45+
this._onTimeout = function() {
46+
++nbTimersFired;
47+
timers.unenroll(foo);
48+
};
49+
}();
50+
51+
timers.enroll(bar, 1);
52+
timers._unrefActive(bar);
53+
54+
timers.enroll(foo, 1);
55+
timers._unrefActive(foo);
56+
57+
setTimeout(function() {
58+
assert.notEqual(nbTimersFired, 2);
59+
}, 20);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
/*
23+
* This test is a regression test for joyent/node#8897.
24+
*
25+
* It tests some private implementation details that should not be
26+
* considered public interface.
27+
*/
28+
var timers = require('timers');
29+
30+
var foo = new function() {
31+
this._onTimeout = function() {};
32+
}();
33+
34+
var bar = new function() {
35+
this._onTimeout = function() {
36+
timers.unenroll(foo);
37+
};
38+
}();
39+
40+
// We use timers with expiration times that are sufficiently apart to make
41+
// sure that they're not fired at the same time on platforms where the timer
42+
// resolution is a bit coarse (e.g Windows with a default resolution of ~15ms).
43+
timers.enroll(bar, 1);
44+
timers._unrefActive(bar);
45+
46+
timers.enroll(foo, 50);
47+
timers._unrefActive(foo);
48+
49+
setTimeout(function() {}, 100);

0 commit comments

Comments
 (0)