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
16 changes: 8 additions & 8 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ exports.enroll = function(item, msecs) {

// call this whenever the item is active (not idle)
// it will reset its timeout.
exports.active = function(item) {
const active = exports.active = function(item) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about it and I think it would be best to only do this for the globals, so only clearTimeout in this patch. (Plus we also don't test active/{un}enroll and there's not a terribly good reason to.

var msecs = item._idleTimeout;
if (msecs >= 0)
insert(item, msecs);
Expand Down Expand Up @@ -208,19 +208,19 @@ exports.setTimeout = function(callback, after) {

if (process.domain) timer.domain = process.domain;

exports.active(timer);
active(timer);

return timer;
};


exports.clearTimeout = function(timer) {
const clearTimeout = exports.clearTimeout = function(timer) {
if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
timer[kOnTimeout] = timer._onTimeout = null;
if (timer instanceof Timeout) {
timer.close(); // for after === 0
} else {
exports.unenroll(timer);
unenroll(timer);
}
}
};
Expand Down Expand Up @@ -262,7 +262,7 @@ exports.setInterval = function(callback, repeat) {
timer._repeat = ontimeout;

if (process.domain) timer.domain = process.domain;
exports.active(timer);
active(timer);

return timer;

Expand All @@ -278,7 +278,7 @@ exports.setInterval = function(callback, repeat) {
this._handle.start(repeat, 0);
} else {
timer._idleTimeout = repeat;
exports.active(timer);
active(timer);
}
}
};
Expand Down Expand Up @@ -318,7 +318,7 @@ Timeout.prototype.unref = function() {
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
if (delay < 0) delay = 0;
exports.unenroll(this);
unenroll(this);

// Prevent running cb again when unref() is called during the same cb
if (this._called && !this._repeat) return;
Expand All @@ -343,7 +343,7 @@ Timeout.prototype.close = function() {
this._handle[kOnTimeout] = null;
this._handle.close();
} else {
exports.unenroll(this);
unenroll(this);
}
};

Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-timers-api-refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('assert');

// don't verify the globals for this test
common.globalCheck = false;

// try overriding global APIs to make sure
// they're not relied on by the timers
global.clearTimeout = assert.fail;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably add everything here:

node/src/node.js

Lines 171 to 176 in ec6e5c7

global.clearImmediate = timers.clearImmediate;
global.clearInterval = timers.clearInterval;
global.clearTimeout = timers.clearTimeout;
global.setImmediate = timers.setImmediate;
global.setInterval = timers.setInterval;
global.setTimeout = timers.setTimeout;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearTimeout(..), unenroll(..) and active(..) are the only methods that are called from the code itself, which means they are the only ones that could break code if allowed to be overwritten without a safe internal reference. That's why I went the conservative route.

We could apply the fix to all API methods, but if they aren't being called, it seems like unnecessary extra code.


// run timeouts/intervals through the paces
const intv = setInterval(function() {}, 1);

setTimeout(function() {
clearInterval(intv);
}, 100);

setTimeout(function() {}, 2);