Skip to content

Commit

Permalink
timers: warn on overflowed timeout duration
Browse files Browse the repository at this point in the history
Perviously 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: #6956
  • Loading branch information
Fishrock123 committed Jun 29, 2016
1 parent fba271b commit 0367986
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ exports.enroll = function(item, msecs) {

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

Expand All @@ -316,6 +319,11 @@ exports.setTimeout = function(callback, after) {

after *= 1; // coalesce to number or NaN

if (after > TIMEOUT_MAX) {
process.emitWarning(`${after} does not fit into a 32-bit signed integer.` +
'\nTimeout duration was set to 1.',
'TimeoutOverflowWarning');
}
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
after = 1; // schedule on next tick, follows browser behaviour
}
Expand Down Expand Up @@ -376,6 +384,11 @@ exports.setInterval = function(callback, repeat) {

repeat *= 1; // coalesce to number or NaN

if (repeat > TIMEOUT_MAX) {
process.emitWarning(`${repeat} does not fit into a 32-bit signed integer.` +
'\nInterval duration was set to 1.',
'TimeoutOverflowWarning');
}
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
repeat = 1; // schedule on next tick, follows browser behaviour
}
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-timers-max-duration-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const timers = require('timers');

const OVERFLOW = Math.pow(2, 31); // TIMEOUT_MAX is 2^31-1

function TimerNotCanceled() {
common.fail('Timer should be canceled');
}

process.on('warning', common.mustCall((warning) => {
const lines = warning.message.split('\n');

assert.strictEqual(warning.name, 'TimeoutOverflowWarning');
assert.strictEqual(lines[0], `${OVERFLOW} does not fit into a 32-bit signed` +
' integer.');
assert.strictEqual(lines.length, 2);
}, 3));


{
const timeout = setTimeout(TimerNotCanceled, OVERFLOW);
clearTimeout(timeout);
}

{
const interval = setInterval(TimerNotCanceled, OVERFLOW);
clearInterval(interval);
}

{
const timer = {
_onTimeout: TimerNotCanceled
};
timers.enroll(timer, OVERFLOW);
timers.active(timer);
timers.unenroll(timer);
}

0 comments on commit 0367986

Please sign in to comment.