Skip to content

Commit 9fa25c8

Browse files
getifyTrott
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 <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Fixes: #2493
1 parent a17200b commit 9fa25c8

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/timers.js

Lines changed: 8 additions & 8 deletions
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

@@ -351,19 +351,19 @@ exports.setTimeout = function(callback, after) {
351351

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

354-
exports.active(timer);
354+
active(timer);
355355

356356
return timer;
357357
};
358358

359359

360-
exports.clearTimeout = function(timer) {
360+
const clearTimeout = exports.clearTimeout = function(timer) {
361361
if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
362362
timer[kOnTimeout] = timer._onTimeout = null;
363363
if (timer instanceof Timeout) {
364364
timer.close(); // for after === 0
365365
} else {
366-
exports.unenroll(timer);
366+
unenroll(timer);
367367
}
368368
}
369369
};
@@ -409,7 +409,7 @@ exports.setInterval = function(callback, repeat) {
409409
timer._repeat = ontimeout;
410410

411411
if (process.domain) timer.domain = process.domain;
412-
exports.active(timer);
412+
active(timer);
413413

414414
return timer;
415415

@@ -425,7 +425,7 @@ exports.setInterval = function(callback, repeat) {
425425
this._handle.start(repeat, 0);
426426
} else {
427427
timer._idleTimeout = repeat;
428-
exports.active(timer);
428+
active(timer);
429429
}
430430
}
431431
};
@@ -468,7 +468,7 @@ Timeout.prototype.unref = function() {
468468

469469
// Prevent running cb again when unref() is called during the same cb
470470
if (this._called && !this._repeat) {
471-
exports.unenroll(this);
471+
unenroll(this);
472472
return;
473473
}
474474

@@ -496,7 +496,7 @@ Timeout.prototype.close = function() {
496496
this._handle[kOnTimeout] = null;
497497
this._handle.close();
498498
} else {
499-
exports.unenroll(this);
499+
unenroll(this);
500500
}
501501
return this;
502502
};

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

Lines changed: 20 additions & 0 deletions
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)