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

timers: warn on overflowed timeout duration #71

Closed
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
21 changes: 19 additions & 2 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,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 Down Expand Up @@ -449,8 +452,15 @@ exports.setTimeout = setTimeout;

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

var timer = new Timeout(after, callback, args);
if (process.domain)
Expand Down Expand Up @@ -538,8 +548,15 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {

function createRepeatTimeout(callback, repeat, args) {
repeat *= 1; // coalesce to number or NaN
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX))
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it feasible to avoid duplicating code in createSingleTimeout() and createRepeatTimeout()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the emitWarning call the duplicate code you're referring to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The warning message differs slightly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that's what I was going to say. Doesn't seem worth abstracting, IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, sorry, didn't notice the difference in warning messages.

if (repeat > TIMEOUT_MAX) {
process.emitWarning(`${repeat} does not fit into` +
' a 32-bit signed integer.' +
'\nInterval duration was set to 1.',
'TimeoutOverflowWarning');
}
repeat = 1; // schedule on next tick, follows browser behavior
}

var timer = new Timeout(repeat, callback, args);
timer._repeat = repeat;
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-timers-max-duration-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'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);
}