use unsorted array in Timers._unrefActive() by Fishrock123 · Pull Request #2540 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 96 additions & 57 deletions lib/timers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

/*
* This test is a regression test for joyent/node#8897.
*/

const common = require('../common');
const assert = require('assert');
const net = require('net');

const clients = [];

const server = net.createServer(function onClient(client) {
clients.push(client);

if (clients.length === 2) {
/*
* Enroll two timers, and make the one supposed to fire first
* unenroll the other one supposed to fire later. This mutates
* the list of unref timers when traversing it, and exposes the
* original issue in joyent/node#8897.
*/
clients[0].setTimeout(1, function onTimeout() {
clients[1].setTimeout(0);
clients[0].end();
clients[1].end();
});

// Use a delay that is higher than the lowest timer resolution accross all
// supported platforms, so that the two timers don't fire at the same time.
clients[1].setTimeout(50);
}
});

server.listen(common.PORT, common.localhostIPv4, function() {
var nbClientsEnded = 0;

function addEndedClient(client) {
++nbClientsEnded;
if (nbClientsEnded === 2) {
server.close();
}
};

const client1 = net.connect({ port: common.PORT });
client1.on('end', addEndedClient);

const client2 = net.connect({ port: common.PORT });
client2.on('end', addEndedClient);
});
46 changes: 46 additions & 0 deletions test/parallel/test-timers-unref-active-unenrolled-disposed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

// https://github.com/nodejs/node/pull/2540/files#r38231197

const common = require('../common');
const timers = require('timers');
const assert = require('assert');
const domain = require('domain');

// Crazy stuff to keep the process open,
// then close it when we are actually done.
const TEST_DURATION = common.platformTimeout(100);
const keepOpen = setTimeout(function() {
throw new Error('Test timed out. keepOpen was not canceled.');
}, TEST_DURATION);

const endTest = makeTimer(2);

const someTimer = makeTimer(1);
someTimer.domain = domain.create();
someTimer.domain.dispose();
someTimer._onTimeout = function() {
throw new Error('someTimer was not supposed to fire!');
};

endTest._onTimeout = common.mustCall(function() {
assert.strictEqual(someTimer._idlePrev, null);
assert.strictEqual(someTimer._idleNext, null);
clearTimeout(keepOpen);
});

const cancelsTimer = makeTimer(1);
cancelsTimer._onTimeout = common.mustCall(function() {
someTimer._idleTimeout = 0;
});

timers._unrefActive(cancelsTimer);
timers._unrefActive(someTimer);
timers._unrefActive(endTest);

function makeTimer(msecs) {
const timer = {};
timers.unenroll(timer);
timers.enroll(timer, msecs);
return timer;
}
51 changes: 51 additions & 0 deletions test/parallel/test-timers-unref-active.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

/*
* This test is aimed at making sure that unref timers queued with
* timers._unrefActive work correctly.
*
* Basically, it queues one timer in the unref queue, and then queues
* it again each time its timeout callback is fired until the callback
* has been called ten times.
*
* At that point, it unenrolls the unref timer so that its timeout callback
* is not fired ever again.
*
* Finally, a ref timeout is used with a delay large enough to make sure that
* all 10 timeouts had the time to expire.
*/

const common = require('../common');
const timers = require('timers');
const assert = require('assert');

var someObject = {};
var nbTimeouts = 0;

/*
* libuv 0.10.x uses GetTickCount on Windows to implement timers, which uses
* system's timers whose resolution is between 10 and 16ms. See
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408.aspx
* for more information. That's the lowest resolution for timers across all
* supported platforms. We're using it as the lowest common denominator,
* and thus expect 5 timers to be able to fire in under 100 ms.
*/
const N = 5;
const TEST_DURATION = 100;

timers.unenroll(someObject);
timers.enroll(someObject, 1);

someObject._onTimeout = function _onTimeout() {
++nbTimeouts;

if (nbTimeouts === N) timers.unenroll(someObject);

timers._unrefActive(someObject);
};

timers._unrefActive(someObject);

setTimeout(function() {
assert.equal(nbTimeouts, N);
}, TEST_DURATION);
Loading