Skip to content

Commit 7ec5397

Browse files
getifyevanlucas
authored andcommitted
timers: fixing API refs to use safe internal refs
Added safe internal references for 'clearTimeout(..)', 'active(..)', and 'unenroll(..)'. Changed various API refs from 'export.*' to use these safe internal references. Now, overwriting the global API identifiers does not create potential breakage and/or race conditions. See Issue #2493. PR-URL: #5882 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Fixes: #2493
1 parent cb676cf commit 7ec5397

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/timers.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const unrefedLists = {};
100100

101101
// Schedule or re-schedule a timer.
102102
// The item must have been enroll()'d first.
103-
exports.active = function(item) {
103+
const active = exports.active = function(item) {
104104
insert(item, false);
105105
};
106106

@@ -346,19 +346,19 @@ exports.setTimeout = function(callback, after) {
346346

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

349-
exports.active(timer);
349+
active(timer);
350350

351351
return timer;
352352
};
353353

354354

355-
exports.clearTimeout = function(timer) {
355+
const clearTimeout = exports.clearTimeout = function(timer) {
356356
if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
357357
timer[kOnTimeout] = timer._onTimeout = null;
358358
if (timer instanceof Timeout) {
359359
timer.close(); // for after === 0
360360
} else {
361-
exports.unenroll(timer);
361+
unenroll(timer);
362362
}
363363
}
364364
};
@@ -400,7 +400,7 @@ exports.setInterval = function(callback, repeat) {
400400
timer._repeat = ontimeout;
401401

402402
if (process.domain) timer.domain = process.domain;
403-
exports.active(timer);
403+
active(timer);
404404

405405
return timer;
406406

@@ -416,7 +416,7 @@ exports.setInterval = function(callback, repeat) {
416416
this._handle.start(repeat, 0);
417417
} else {
418418
timer._idleTimeout = repeat;
419-
exports.active(timer);
419+
active(timer);
420420
}
421421
}
422422
};
@@ -459,7 +459,7 @@ Timeout.prototype.unref = function() {
459459

460460
// Prevent running cb again when unref() is called during the same cb
461461
if (this._called && !this._repeat) {
462-
exports.unenroll(this);
462+
unenroll(this);
463463
return;
464464
}
465465

@@ -487,7 +487,7 @@ Timeout.prototype.close = function() {
487487
this._handle[kOnTimeout] = null;
488488
this._handle.close();
489489
} else {
490-
exports.unenroll(this);
490+
unenroll(this);
491491
}
492492
return this;
493493
};

test/parallel/test-timers-api-refs.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
// don't verify the globals for this test
6+
common.globalCheck = false;
7+
8+
// try overriding global APIs to make sure
9+
// they're not relied on by the timers
10+
global.clearTimeout = assert.fail;
11+
12+
// run timeouts/intervals through the paces
13+
const intv = setInterval(function() {}, 1);
14+
15+
setTimeout(function() {
16+
clearInterval(intv);
17+
}, 100);
18+
19+
setTimeout(function() {}, 2);
20+

0 commit comments

Comments
 (0)