Files
node/test/parallel/test-process-timeout-blocked.js
T
James M Snell 823d229adc src: add --process-timeout=N
Putting a time limit on a Node.js process is common in many
use cases, but each of the common approaches are generally
flawed.

OS and CI timeouts are inconsistent and blind. Every OS flavor
handles process timeouts in different ways. Stock macOS and
Windows do not ship coreutils `timeout`, etc. None of the options
can explain why the process was still running or what exactly
was interrupted. Using setTimeout only fires when the event loop
is free so it misses busy loops and thread blocks and cannot
see the stack. Diagnostics triggered by signal have the same
problem.

This introduces a `--process-timeout` that should work
consistently across all runtimes. A native watchdog thread
interrupts the main thread, even in the middle of running
code. It prints the JavaScript stack (if any) and resources
keeping the event loop alive (if any), and can optionally
print a full diagnostic report. The process exits with a
distinct code (124). If the main thread is stuck in native
code, the process still exits.

    $ node --process-timeout=10s -e "while(true) {}"
    (node:483970) Process timed out after 10s (--process-timeout). Exiting with code 124.
    Main thread was executing JavaScript:
        at [eval]:1:1
        at runScriptInThisContext (node:internal/vm:219:10)
        at node:internal/process/execution:485:12
        at [eval]-wrapper:6:24
        at runScriptInContext (node:internal/process/execution:483:60)
        at evalFunction (node:internal/process/execution:317:30)
        at evalTypeScript (node:internal/process/execution:329:3)
        at node:internal/main/eval_string:71:3
    No resources keeping the event loop alive were found.

Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opencode
PR-URL: https://github.com/nodejs/node/pull/66138
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2026-09-25 20:28:33 +00:00

72 lines
2.7 KiB
JavaScript

'use strict';
// Tests that --process-timeout makes the process exit even if the main thread
// cannot be interrupted.
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const { spawnSync } = require('child_process');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
// The deadline is measured from the start of the process, so on a slow machine
// it can expire before the fixture has reached the state under test, which the
// fixture signals by creating a file. If it did not get that far and did not
// print the expected message, run it again with a longer timeout.
function runUntilReady(fixture, expected) {
for (let timeout = common.platformTimeout(1000); ; timeout *= 2) {
// Child processes of a previous attempt may still be running, so use a
// different file for each attempt.
const marker = tmpdir.resolve(`${fixture}.${timeout}.ready`);
const start = process.hrtime.bigint();
const child = spawnSync(process.execPath, [
`--process-timeout=${timeout}ms`,
fixtures.path('process-timeout', fixture),
marker,
], { encoding: 'utf8' });
const elapsed = Number(process.hrtime.bigint() - start) / 1e6;
if (!expected.test(child.stderr) && !fs.existsSync(marker) &&
timeout < common.platformTimeout(16000)) {
continue;
}
assert.strictEqual(child.signal, null);
assert.strictEqual(child.status, 124, child.stderr);
assert.ok(elapsed >= timeout, `Exited after ${elapsed}ms, before ${timeout}ms`);
assert.match(child.stderr, new RegExp(
`^\\(node:\\d+\\) Process timed out after ${timeout}ms \\(--process-timeout\\)\\. ` +
'Exiting with code 124\\.$', 'm'));
assert.match(child.stderr, expected);
return child.stderr;
}
}
{
// The main thread is blocked in a synchronous native call.
const stderr = runUntilReady(
'blocked-main-thread.js',
/^The main thread did not respond within 2000ms\. It is likely blocked/m);
assert.doesNotMatch(stderr, /Main thread was/);
}
{
// The main thread responds, but exiting waits for a Worker thread that is
// blocked in a synchronous native call.
const stderr = runUntilReady(
'blocked-worker.js',
/^\(node:\d+\) The process did not finish exiting within 5000ms after --process-timeout expired\. Forcing exit\.$/m);
assert.match(stderr, /^ {4}Worker \(thread 1, name 'blocked'\)$/m);
}
{
// The event loop has stopped, but exiting waits for a Worker thread that is
// blocked in a synchronous native call.
runUntilReady(
'blocked-worker-at-exit.js',
/^The process did not finish exiting after the event loop had stopped\.$/m);
}