Skip to content
This repository was archived by the owner on Aug 31, 2018. It is now read-only.

Commit 607316c

Browse files
Fishrock123addaleax
authored andcommitted
timers: warn on overflowed timeout duration
Previously there wasn't any clear indicator when you hit the overflow other than possibly unexpected behavior, and I think emitting a warning may be appropriate. PR-URL: #71 Reviewed-By: Scott Trinh <scott@scotttrinh.com> Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 8e2de77 commit 607316c

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

lib/timers.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ exports.enroll = function(item, msecs) {
396396

397397
// Ensure that msecs fits into signed int32
398398
if (msecs > TIMEOUT_MAX) {
399+
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` +
400+
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
401+
'TimeoutOverflowWarning');
399402
msecs = TIMEOUT_MAX;
400403
}
401404

@@ -440,8 +443,15 @@ exports.setTimeout = setTimeout;
440443

441444
function createSingleTimeout(callback, after, args) {
442445
after *= 1; // coalesce to number or NaN
443-
if (!(after >= 1 && after <= TIMEOUT_MAX))
446+
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
447+
if (after > TIMEOUT_MAX) {
448+
process.emitWarning(`${after} does not fit into` +
449+
' a 32-bit signed integer.' +
450+
'\nTimeout duration was set to 1.',
451+
'TimeoutOverflowWarning');
452+
}
444453
after = 1; // schedule on next tick, follows browser behavior
454+
}
445455

446456
var timer = new Timeout(after, callback, args);
447457
if (process.domain)
@@ -529,8 +539,15 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
529539

530540
function createRepeatTimeout(callback, repeat, args) {
531541
repeat *= 1; // coalesce to number or NaN
532-
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX))
542+
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
543+
if (repeat > TIMEOUT_MAX) {
544+
process.emitWarning(`${repeat} does not fit into` +
545+
' a 32-bit signed integer.' +
546+
'\nInterval duration was set to 1.',
547+
'TimeoutOverflowWarning');
548+
}
533549
repeat = 1; // schedule on next tick, follows browser behavior
550+
}
534551

535552
var timer = new Timeout(repeat, callback, args);
536553
timer._repeat = repeat;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const timers = require('timers');
6+
7+
const OVERFLOW = Math.pow(2, 31); // TIMEOUT_MAX is 2^31-1
8+
9+
function timerNotCanceled() {
10+
common.fail('Timer should be canceled');
11+
}
12+
13+
process.on('warning', common.mustCall((warning) => {
14+
const lines = warning.message.split('\n');
15+
16+
assert.strictEqual(warning.name, 'TimeoutOverflowWarning');
17+
assert.strictEqual(lines[0], `${OVERFLOW} does not fit into a 32-bit signed` +
18+
' integer.');
19+
assert.strictEqual(lines.length, 2);
20+
}, 3));
21+
22+
23+
{
24+
const timeout = setTimeout(timerNotCanceled, OVERFLOW);
25+
clearTimeout(timeout);
26+
}
27+
28+
{
29+
const interval = setInterval(timerNotCanceled, OVERFLOW);
30+
clearInterval(interval);
31+
}
32+
33+
{
34+
const timer = {
35+
_onTimeout: timerNotCanceled
36+
};
37+
timers.enroll(timer, OVERFLOW);
38+
timers.active(timer);
39+
timers.unenroll(timer);
40+
}

0 commit comments

Comments
 (0)