denoland/deno · error · ERR_OUT_OF_RANGE
ERR_OUT_OF_RANGE
ERR_OUT_OF_RANGE
Error message
The value of "${name}" is out of range. It must be a non-negative finite number. Received ${msecs} What it means
getTimerDuration validates timeout durations inside Deno's node compat layer and throws ERR_OUT_OF_RANGE for negative or non-finite numbers. Its active call sites are stream/net setTimeout (ext/node/polyfills/internal/stream_base_commons.ts:392, i.e. socket.setTimeout(msecs)), http.request({ timeout }) (ext/node/polyfills/_http_client.js:649), and ClientRequest.prototype.setTimeout (line 1626) — the global setTimeout/setInterval are NOT affected (they clamp instead). Values above 2147483647 do not throw; they are truncated with a TimeoutOverflowWarning.
Source
Thrown at ext/node/polyfills/internal/timers.mjs:366
Timeout.prototype.hasRef = function () {
return this[kRefed];
};
Timeout.prototype[SymbolToPrimitive] = function () {
return this[kTimerId];
};
/**
* @param {number} msecs
* @param {string} name
* @returns
*/
function getTimerDuration(msecs, name) {
validateNumber(msecs, name);
if (msecs < 0 || !NumberIsFinite(msecs)) {
throw new ERR_OUT_OF_RANGE(name, "a non-negative finite number", msecs);
}
// Ensure that msecs fits into signed int32
if (msecs > TIMEOUT_MAX) {
lazyProcess().default.emitWarning(
`${msecs} does not fit into a 32-bit signed integer.` +
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
"TimeoutOverflowWarning",
);
return TIMEOUT_MAX;
}
return msecs;
}
function setUnrefTimeout(callback, timeout, ...args) {
validateFunction(callback, "callback");View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Use 0 (or omit the call) to disable a socket timeout — never a negative number
- Sanitize: const t = Number.isFinite(ms) && ms > 0 ? Math.min(ms, 2147483647) : 0
- Clamp Infinity to TIMEOUT_MAX (2147483647) if you really want 'very long'
Example fix
// before const timeoutMs = cfg.disabled ? -1 : 5000; socket.setTimeout(timeoutMs); // after const timeoutMs = cfg.disabled ? 0 : 5000; if (timeoutMs > 0) socket.setTimeout(timeoutMs); else socket.setTimeout(0);
Defensive patterns
Strategy: validation
Validate before calling
const TIMEOUT_MAX = 2147483647;
function sanitizeTimeout(ms: unknown): number {
const n = Number(ms);
if (!Number.isFinite(n) || n < 0) return 0; // 0 disables the socket timeout
return Math.min(n, TIMEOUT_MAX);
}
socket.setTimeout(sanitizeTimeout(cfg.timeout)); Type guard
const isValidTimeout = (ms: unknown): ms is number => typeof ms === 'number' && Number.isFinite(ms) && ms >= 0;
Try / catch
try {
req.setTimeout(ms);
} catch (err) {
if (err?.code === 'ERR_OUT_OF_RANGE') req.setTimeout(0);
else throw err;
} Prevention
- Encode 'disabled' as 0, never -1 or Infinity, in timeout config
- Remember only stream/http setTimeout paths validate; values above 2^31-1 are silently truncated with a warning
When it happens
Trigger: socket.setTimeout(-1); req.setTimeout(Infinity); http.request(url, { timeout: -1 }); duration computed as a - b going negative, or 0/0 producing NaN and forwarded to setTimeout on the socket.
Common situations: Config-driven timeouts where 'disabled' is encoded as -1 or Infinity instead of 0/omission; idletimeout math with unsigned subtraction bugs; passing a value parsed from a header/query (NaN) straight into socket.setTimeout.
Related errors
- unknown override kind: ${override.kind}
- ERR_INVALID_ARG_TYPE
- ERR_INVALID_URL
- ERR_UNESCAPED_CHARACTERS
- ERR_INVALID_PROTOCOL
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/59589baee2b4766e.
Report an issue: GitHub.