· 4 min read
A 25-day timeout that fired after 1 millisecond
Node.js runs any timer longer than 2147483647 ms after 1 ms, with only a warning. In Fastify, that turned a "never time out" handlerTimeout into a route that timed out at once. The merged fix rejects the value at startup.
Here is a line of Node.js you can run right now:
const start = Date.now()
setTimeout(() => console.log(`fired after ${Date.now() - start} ms`), 2 ** 31)The delay is 2147483648 ms, a little under 25 days. On Node 22 it prints this almost at once:
(node) TimeoutOverflowWarning: 2147483648 does not fit into a 32-bit signed integer.
Timeout duration was set to 1.
fired after 4 msNo exception. One warning on stderr, and the callback runs a few milliseconds later. I fixed the place where this hurt Fastify users (fastify/fastify#7217, merged 24 November 2026). This post covers the limit, how it reached a web framework, and the general rule I took from it.
The limit: 2147483647 ms
Node stores a timer's delay as a 32-bit signed integer. The largest value that fits is 2147483647 (0x7FFFFFFF), which is about 24.8 days. The Node.js docs say it directly: when the delay is larger than 2147483647 or less than 1, it is set to 1.
So the timer does not fail. It turns into the shortest timer possible. I checked the related APIs on Node 22 and they all behave the same way:
setTimeout(fn, 2 ** 31)runs after about 1 ms.setTimeout(fn, Infinity)runs after about 1 ms.setInterval(fn, 2 ** 31)runs every millisecond or so.AbortSignal.timeout(2 ** 31)aborts after about 1 ms.
Each one prints a TimeoutOverflowWarning. In a service that ships logs somewhere and nobody reads stderr, that warning is easy to miss.
How it reached Fastify
Fastify has a handlerTimeout option. It sets the longest time a route handler may run. When it fires, request.signal is aborted and a 503 goes through the error handler. You can set it for the whole server or per route, and routes without their own value inherit the server one. Under the hood it is a timer.
Before the fix, the route-level check was:
if (!Number.isInteger(handlerTimeout) || handlerTimeout <= 0) {
throw new FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT(handlerTimeout)
}Any positive integer passed. The server-level option had a JSON schema of { type: 'integer' }, with no upper bound either.
Now picture someone who wants one slow route to "basically never" time out, and writes a very large number. The config is a positive integer, so Fastify accepts it. Node turns the timer into 1 ms. Every request on that route now times out with a 503 almost immediately. The exact opposite of what the config says, and the only clue is one warning line.
Another user reported it in fastify/fastify#7216, and I sent the fix. It is a clean example of a whole class of bug: a value that is valid at one layer and nonsense at the layer below.
The fix: say no at startup
The change was 40 lines added and 9 removed, and most of it is saying "no" earlier.
Route level. The check now has an upper bound, kept as a named constant with a comment (one of the maintainers asked for exactly this in review):
// Node.js runs a timer with a longer delay after 1 ms instead
const MAX_TIMEOUT = 0x7FFFFFFF
if (!Number.isInteger(handlerTimeout) || handlerTimeout <= 0 || handlerTimeout > MAX_TIMEOUT) {
throw new FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT(handlerTimeout)
}The error message now states the range: 'handlerTimeout' option must be an integer between 1 and 2147483647. An error that tells you the allowed range saves a trip to the docs.
Server level. The init-options schema got a maximum:
handlerTimeout: { type: 'integer', maximum: 2147483647, default: defaultInitOptions.handlerTimeout }Fastify compiles that schema into a standalone validator, so the generated file was rebuilt as well. Now Fastify({ handlerTimeout: 2147483648 }) throws FST_ERR_INIT_OPTS_INVALID and the server never starts.
Docs. Routes.md, Server.md and Errors.md state the range and why it exists: it is the longest delay a Node.js timer supports.
Testing a timer bug without waiting for a timer
The obvious test would start a server, send a request and check that it does not time out. That test is slow and still tells you little. The bug is in what the framework accepts, so the tests check acceptance at the two edges:
test('route-level handlerTimeout rejects values above the Node.js timer limit', async t => {
const fastify = Fastify()
t.assert.throws(() => {
fastify.get('/too-long', { handlerTimeout: 2147483648 }, async () => 'ok')
}, { code: 'FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT' })
fastify.get('/longest', { handlerTimeout: 2147483647 }, async () => 'ok')
await fastify.ready()
t.assert.strictEqual(fastify.hasRoute({ method: 'GET', url: '/longest' }), true)
})
test('server-level handlerTimeout rejects values above the Node.js timer limit', t => {
t.plan(2)
t.assert.throws(() => Fastify({ handlerTimeout: 2147483648 }), { code: 'FST_ERR_INIT_OPTS_INVALID' })
t.assert.strictEqual(Fastify({ handlerTimeout: 2147483647 }).initialConfig.handlerTimeout, 2147483647)
})2147483647 passes, 2147483648 fails, at both levels. No test waits for a timer, so they run in milliseconds and cannot be flaky.
Checking your own code
You do not need Fastify to have this bug. Anywhere a timeout comes from config, an environment variable or user input and ends up in setTimeout, setInterval or AbortSignal.timeout, the same thing can happen. A few habits that help:
- Validate at the edge. When you read a timeout from config, check
1 <= value <= 2147483647and fail startup otherwise. Zod, JSON schema or a plainifall work. - Watch the unit. A lot of these bugs start as seconds written where milliseconds were expected. 30 days in seconds is 2592000, which is fine. 30 days in milliseconds is 2592000000, which is over the limit and becomes 1 ms.
- Treat
TimeoutOverflowWarningas an error in tests. Node emits it as a process warning, so aprocess.on('warning')handler in the test setup can fail the run whenwarning.name === 'TimeoutOverflowWarning'. - Do not use a timer for "weeks from now". If something really has to happen in 30 days, store the due date and let a scheduler or a job queue pick it up. A process that restarts in the meantime loses an in-memory timer anyway.
What I took from it
Every config value ends up inside some lower layer with limits of its own: a 32-bit timer, a column type, a header size, a queue's message size. If your layer does not check those limits when it starts, the lower layer decides for you, quietly, at runtime.
The review itself was short. One maintainer asked for the limit as a named constant at the top of the file, I pushed that change, and it was merged a day later. Small, boring fixes like this one are some of the most useful you can send to a framework, because every user of it gets the safer default without changing a line.