From 76550a3915b2619ceba19e8e722ba1f18d33007c Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 25 Oct 2026 08:50:47 +0000 Subject: [PATCH 1/7] node:child_process: keep the timeout timers ref'd and arm one per call spawn() and execFile() arm options.timeout as a timer that calls child.kill(). Both timers were unref'd, so after child.unref() nothing kept the parent alive: it exited with the deadline still armed and the child was never signalled. - Keep both timers ref'd, as Node does. - execFile() no longer passes its timeout to spawn(). One call armed two timers and called kill() twice at the deadline. - Create and clear the timers with the native timer functions and not with the globals. A fake-timers library replaces the globals, and a replaced clearTimeout would leave a ref'd timer armed. Add test-child-process-spawn-timeout-clear-on-error.js from Node v26.12.1. Co-authored-by: Leo Li --- src/js/node/child_process.ts | 10 +- .../node/child_process/child_process.test.ts | 94 +++++++++++++ .../fixtures/child-process-timeout-fixture.js | 123 ++++++++++++++++++ ...ld-process-spawn-timeout-clear-on-error.js | 19 +++ 4 files changed, 243 insertions(+), 3 deletions(-) create mode 100644 test/js/node/child_process/fixtures/child-process-timeout-fixture.js create mode 100644 test/js/node/test/parallel/test-child-process-spawn-timeout-clear-on-error.js diff --git a/src/js/node/child_process.ts b/src/js/node/child_process.ts index 23ef6489fb5e..8e6bbab80361 100644 --- a/src/js/node/child_process.ts +++ b/src/js/node/child_process.ts @@ -43,6 +43,10 @@ var StringPrototypeIncludes = String.prototype.includes; var StringPrototypeStartsWith = String.prototype.startsWith; var Uint8ArrayPrototypeIncludes = Uint8Array.prototype.includes; +// Not the globals, which user code and fake timers replace: https://github.com/nodejs/node/blob/v26.12.1/lib/child_process.js#L76 +const setTimeout: typeof globalThis.setTimeout = $newCppFunction("node/NodeTimers.cpp", "functionSetTimeout", 1); +const clearTimeout: typeof globalThis.clearTimeout = $newCppFunction("node/NodeTimers.cpp", "functionClearTimeout", 1); + const MAX_BUFFER = 1024 * 1024; interface ExecException extends Error { @@ -179,7 +183,7 @@ function spawn(file, args, options) { child.emit("error", err); } } - }, timeout).unref(); + }, timeout); const clear = () => { if (timeoutId) { @@ -187,6 +191,7 @@ function spawn(file, args, options) { timeoutId = null; } }; + // Node clears on 'close' alone (https://github.com/nodejs/node/blob/v26.12.1/lib/child_process.js#L874-L880). Here 'exit' is not always followed by 'close'. child.once("exit", clear); child.once("close", clear); } @@ -262,7 +267,6 @@ function execFile(file, args, options?, callback?) { const child = spawn(file, args, { cwd: options.cwd, env: options.env, - timeout: options.timeout, killSignal: options.killSignal, uid: options.uid, gid: options.gid, @@ -368,7 +372,7 @@ function execFile(file, args, options?, callback?) { timeoutId = setTimeout(function delayedKill() { timeoutId = null; kill(); - }, optionsTimeout).unref(); + }, optionsTimeout); } function addOnDataListener(child_buffer, _buffer, kind) { diff --git a/test/js/node/child_process/child_process.test.ts b/test/js/node/child_process/child_process.test.ts index 7845b6d9f361..a2f3b2b8c681 100644 --- a/test/js/node/child_process/child_process.test.ts +++ b/test/js/node/child_process/child_process.test.ts @@ -1696,6 +1696,100 @@ describe("spawn/execFile({signal}) does not leak abort listeners on spawn failur }); }); +// `timeout` is one timer per call, and it calls child.kill(): spawn() arms it +// for spawn and fork, execFile() for execFile and exec. As in Node, it keeps the +// parent alive until it fires or the child is gone, child.unref() or not. +describe("options.timeout", () => { + const fixture = path.join(import.meta.dir, "fixtures", "child-process-timeout-fixture.js"); + + // What the parent in the fixture reports as it exits, and whether the child + // it started is still there afterwards. + type Expected = { killed?: boolean; kills: number; events?: string[]; callback?: unknown; alive?: boolean }; + const cases: Record = { + // Only the deadline keeps the parent alive. It waits, signals once, and exits. + "spawn": { killed: true, kills: 1, alive: false }, + "fork": { killed: true, kills: 1, alive: false }, + "execFile": { killed: true, kills: 1, alive: false }, + "exec": { killed: true, kills: 1, alive: false }, + "execFile, pipes open at the deadline": { + killed: true, + kills: 1, + events: ["exit", "close"], + callback: { killed: true }, + }, + // The callback has its error, so the deadline is over. The child ends by itself. + "execFile, 'error' before the deadline": { + killed: false, + kills: 0, + events: ["error", "exit", "close"], + callback: { killed: false }, + }, + // The child is gone before the deadline. The parent hears of it, and the timer does not keep it. + "spawn, child exits first": { kills: 0, events: ["exit", "close"] }, + "fork, child exits first": { kills: 0, events: ["exit", "close"] }, + "execFile, child exits first": { kills: 0, events: ["exit", "close"], callback: "no error" }, + // No 'close' comes after this 'exit' (Node emits one), so the events are not part of the case. + "spawn, 'exit' listener pauses stdout": { killed: false, kills: 0 }, + "spawn of a missing file": { kills: 0, events: ["error", "close"] }, + // The timers are not the globals, which a fake-timers library replaces. + "spawn, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"] }, + "execFile, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"], callback: "no error" }, + "timers replaced while the module loads, then restored": { killed: true, kills: 1, alive: false }, + // No timeout, no timer: the parent leaves at once and the child stays. + "no timeout": { killed: false, kills: 0, events: [], alive: true }, + // The graph ends its child and its timers. No 'close' comes to clear the deadline. + "spawn in a Bun.ModuleGraph that is disposed": { killed: false, kills: 0, alive: false }, + }; + + const isAlive = (pid: number) => { + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } + }; + + // One at a time: a case is two processes, and a debug build that starts + // them all together needs the whole test timeout for the slowest. + it.each(Object.entries(cases))("%s", async (mode, expected) => { + await using parent = Bun.spawn({ + cmd: [bunExe(), fixture, mode], + // The runner sets this flag on ASAN lanes. With it the parent kills its + // child as it exits, whether or not the deadline was reached. + env: { ...bunEnv, BUN_FEATURE_FLAG_NO_ORPHANS: undefined }, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([parent.stdout.text(), parent.stderr.text(), parent.exited]); + const report = stdout ? JSON.parse(stdout) : {}; + const pid: number | undefined = report.pid; + try { + let alive = pid !== undefined && isAlive(pid); + // The parent signals the child and exits. The child dies a moment later. + for (let i = 0; alive && expected.alive === false && i < 200; i++) { + await Bun.sleep(10); + alive = isAlive(pid!); + } + const reported = Object.fromEntries( + Object.keys(expected).map(key => [key, key === "alive" ? alive : report[key]]), + ); + expect({ stderr, ...reported, exitCode, signalCode: parent.signalCode }).toEqual({ + stderr: "", + ...expected, + exitCode: 0, + signalCode: null, + }); + } finally { + if (pid !== undefined) { + try { + process.kill(pid, "SIGKILL"); + } catch {} + } + } + }); +}); + // A 'data' handler runs inside the native read loop that delivered its chunk, // and node streams pull again from there, so that pull reads synchronously, // nested in the outer loop, and can run all the way to EOF. The reader used to diff --git a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js new file mode 100644 index 000000000000..b20892da876e --- /dev/null +++ b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js @@ -0,0 +1,123 @@ +// The parent process of the `timeout` tests in child_process.test.ts, and the +// child it starts. The tests are about when the parent exits and what it did +// first, so each case runs in a process of its own and reports from 'exit'. +const { writeSync } = require("node:fs"); + +const mode = process.argv[2]; + +// What a fake-timers library puts in place of the globals: timers that never +// fire and have numbers for ids, and a clear that knows only its own. +const replaceTimers = () => { + const { setTimeout, clearTimeout } = globalThis; + let id = 0; + globalThis.setTimeout = () => ++id; + globalThis.clearTimeout = () => {}; + return () => Object.assign(globalThis, { setTimeout, clearTimeout }); +}; +const restoreTimers = mode === "timers replaced while the module loads, then restored" && replaceTimers(); +const { spawn, fork, exec, execFile } = require("node:child_process"); +if (restoreTimers) restoreTimers(); + +// A deadline the parent must reach, and one it must clear long before. +const soon = 200; +const never = 60_000; + +if (mode === "idle") { + // Long enough for every case to end it, and not for ever when a run fails to. + setTimeout(() => {}, 30_000); +} else if (mode === "brief") { + setTimeout(() => {}, 3 * soon); +} else if (mode !== "exit") { + const idle = [__filename, "idle"]; + const brief = [__filename, "brief"]; + const exits = [__filename, "exit"]; + const events = []; + let kills = 0; + let callback; + const done = error => { + callback = error ? { killed: !!error.killed } : "no error"; + }; + const watch = child => { + const kill = child.kill; + child.kill = function (...args) { + kills++; + return kill.apply(this, args); + }; + for (const event of ["error", "exit", "close"]) child.on(event, () => events.push(event)); + process.on("exit", () => { + writeSync(1, JSON.stringify({ pid: child.pid, killed: child.killed, kills, events, callback })); + }); + return child; + }; + // Leaves the deadline as the only thing that can keep this process alive. + const forget = child => { + child.stdout?.destroy(); + child.stderr?.destroy(); + child.unref(); + }; + + switch (mode) { + case "spawn": + case "timers replaced while the module loads, then restored": + forget(watch(spawn(process.execPath, idle, { stdio: "ignore", detached: true, timeout: soon }))); + break; + case "fork": { + const child = watch(fork(__filename, ["idle"], { stdio: "ignore", detached: true, timeout: soon })); + forget(child); + child.disconnect(); + break; + } + case "execFile": + forget(watch(execFile(process.execPath, idle, { timeout: soon }, done))); + break; + case "exec": { + // `exec` makes the shell become the child, so the pid reported is the one the signal has to reach. + const command = `"${process.execPath}" "${__filename}" idle`; + forget(watch(exec(process.platform === "win32" ? command : `exec ${command}`, { timeout: soon }, done))); + break; + } + case "execFile, pipes open at the deadline": + watch(execFile(process.execPath, idle, { timeout: soon }, done)); + break; + case "execFile, 'error' before the deadline": + watch(execFile(process.execPath, brief, { timeout: soon }, done)).emit("error", new Error("early")); + break; + case "spawn, child exits first": + watch(spawn(process.execPath, exits, { timeout: never })).unref(); + break; + case "fork, child exits first": + watch(fork(__filename, ["exit"], { stdio: "ignore", timeout: never })).unref(); + break; + case "execFile, child exits first": + forget(watch(execFile(process.execPath, exits, { timeout: never }, done))); + break; + case "spawn, 'exit' listener pauses stdout": { + const child = watch(spawn(process.execPath, exits, { timeout: never })); + child.once("exit", () => child.stdout.pause()); + break; + } + case "spawn of a missing file": + watch(spawn(__filename + ".does-not-exist", [], { timeout: never })); + break; + case "spawn, timers replaced after the module loaded": + replaceTimers(); + watch(spawn(process.execPath, exits, { stdio: "ignore", timeout: never })).unref(); + break; + case "execFile, timers replaced after the module loaded": + replaceTimers(); + forget(watch(execFile(process.execPath, exits, { timeout: never }, done))); + break; + case "no timeout": + forget(watch(spawn(process.execPath, idle, { stdio: "ignore", detached: true }))); + break; + case "spawn in a Bun.ModuleGraph that is disposed": { + // A disposed graph hears no 'close'. Its timers end with it. + const graph = new Bun.ModuleGraph(); + watch(graph.run(() => spawn(process.execPath, idle, { stdio: "ignore", timeout: never }))); + graph.dispose(); + break; + } + default: + throw new Error(`unknown mode: ${mode}`); + } +} diff --git a/test/js/node/test/parallel/test-child-process-spawn-timeout-clear-on-error.js b/test/js/node/test/parallel/test-child-process-spawn-timeout-clear-on-error.js new file mode 100644 index 000000000000..f674e4ed55f0 --- /dev/null +++ b/test/js/node/test/parallel/test-child-process-spawn-timeout-clear-on-error.js @@ -0,0 +1,19 @@ +'use strict'; + +// Measures the child's actual exit time, not just its 'error' event. +// The outer spawnSync timeout catches a leaked inner timer. + +const common = require('../common'); +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); + +const bugStallMs = common.platformTimeout(10000); +const outerTimeoutMs = common.platformTimeout(2000); + +spawnSyncAndExitWithoutError(process.execPath, ['-e', ` + const { spawn } = require('child_process'); + const cp = spawn(process.execPath, ['--version'], { + cwd: '/nonexistent/path/that/should/never/exist', + timeout: ${bugStallMs}, + }); + cp.on('error', () => {}); +`], { timeout: outerTimeoutMs }); From 1ffffd69aefda4570bc000afc5f904c7b6db4a26 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:05:24 +0000 Subject: [PATCH 2/7] node:child_process: execFile's timeout stops holding the process at 'exit' After 'exit' the timer of execFile() has one job left: to bound the wait for 'close' while the pipes stay open. Bun can emit 'exit' with no 'close' after it, and the ref'd timer then kept the parent alive until the deadline. The timer is unref'd at 'exit'. It still fires while the pipes hold the event loop. --- src/js/node/child_process.ts | 2 ++ test/js/node/child_process/child_process.test.ts | 3 ++- .../fixtures/child-process-timeout-fixture.js | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/js/node/child_process.ts b/src/js/node/child_process.ts index 8e6bbab80361..a62ca34c9e8a 100644 --- a/src/js/node/child_process.ts +++ b/src/js/node/child_process.ts @@ -373,6 +373,8 @@ function execFile(file, args, options?, callback?) { timeoutId = null; kill(); }, optionsTimeout); + // Past 'exit' the deadline only bounds the wait for 'close', which Bun can lose, so it stops holding the process. + child.once("exit", () => timeoutId?.unref()); } function addOnDataListener(child_buffer, _buffer, kind) { diff --git a/test/js/node/child_process/child_process.test.ts b/test/js/node/child_process/child_process.test.ts index a2f3b2b8c681..0694ac291685 100644 --- a/test/js/node/child_process/child_process.test.ts +++ b/test/js/node/child_process/child_process.test.ts @@ -1728,8 +1728,9 @@ describe("options.timeout", () => { "spawn, child exits first": { kills: 0, events: ["exit", "close"] }, "fork, child exits first": { kills: 0, events: ["exit", "close"] }, "execFile, child exits first": { kills: 0, events: ["exit", "close"], callback: "no error" }, - // No 'close' comes after this 'exit' (Node emits one), so the events are not part of the case. + // No 'close' comes after these 'exit's (Node emits one), so the events are not part of the cases. "spawn, 'exit' listener pauses stdout": { killed: false, kills: 0 }, + "execFile, 'exit' listener pauses the pipes": { killed: false, kills: 0 }, "spawn of a missing file": { kills: 0, events: ["error", "close"] }, // The timers are not the globals, which a fake-timers library replaces. "spawn, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"] }, diff --git a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js index b20892da876e..748cf608aed3 100644 --- a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js +++ b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js @@ -27,6 +27,9 @@ if (mode === "idle") { setTimeout(() => {}, 30_000); } else if (mode === "brief") { setTimeout(() => {}, 3 * soon); +} else if (mode === "leaves its pipes open") { + // Exits at once. A process of its own holds stdout and stderr a little longer. + spawn(process.execPath, [__filename, "brief"], { stdio: "inherit", detached: true }).unref(); } else if (mode !== "exit") { const idle = [__filename, "idle"]; const brief = [__filename, "brief"]; @@ -96,6 +99,14 @@ if (mode === "idle") { child.once("exit", () => child.stdout.pause()); break; } + case "execFile, 'exit' listener pauses the pipes": { + const child = watch(execFile(process.execPath, [__filename, "leaves its pipes open"], { timeout: never }, done)); + child.once("exit", () => { + child.stdout.pause(); + child.stderr.pause(); + }); + break; + } case "spawn of a missing file": watch(spawn(__filename + ".does-not-exist", [], { timeout: never })); break; From 10b634dbdbcb0f46ff81343ec057efe0ed9fe7d3 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:41:29 +0000 Subject: [PATCH 3/7] node:child_process: the timeout cases reap the child they killed The cases that end with a kill looked at the child's pid after the parent had gone. A child that no process reaps stays a zombie, and a zombie still answers kill(pid, 0). The fixture parent now stays until the child's 'exit', so the child is reaped before the test looks. The comment on the timer bindings named fake timers. bun:test fake timers still reach these timers, so it now names user code only. --- src/js/node/child_process.ts | 2 +- .../node/child_process/child_process.test.ts | 25 ++++++++++--------- .../fixtures/child-process-timeout-fixture.js | 5 +++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/js/node/child_process.ts b/src/js/node/child_process.ts index a62ca34c9e8a..50bd0f0b0b5c 100644 --- a/src/js/node/child_process.ts +++ b/src/js/node/child_process.ts @@ -43,7 +43,7 @@ var StringPrototypeIncludes = String.prototype.includes; var StringPrototypeStartsWith = String.prototype.startsWith; var Uint8ArrayPrototypeIncludes = Uint8Array.prototype.includes; -// Not the globals, which user code and fake timers replace: https://github.com/nodejs/node/blob/v26.12.1/lib/child_process.js#L76 +// Not the globals, which user code can replace: https://github.com/nodejs/node/blob/v26.12.1/lib/child_process.js#L76 const setTimeout: typeof globalThis.setTimeout = $newCppFunction("node/NodeTimers.cpp", "functionSetTimeout", 1); const clearTimeout: typeof globalThis.clearTimeout = $newCppFunction("node/NodeTimers.cpp", "functionClearTimeout", 1); diff --git a/test/js/node/child_process/child_process.test.ts b/test/js/node/child_process/child_process.test.ts index 0694ac291685..0b736cfb34c5 100644 --- a/test/js/node/child_process/child_process.test.ts +++ b/test/js/node/child_process/child_process.test.ts @@ -1706,11 +1706,12 @@ describe("options.timeout", () => { // it started is still there afterwards. type Expected = { killed?: boolean; kills: number; events?: string[]; callback?: unknown; alive?: boolean }; const cases: Record = { - // Only the deadline keeps the parent alive. It waits, signals once, and exits. - "spawn": { killed: true, kills: 1, alive: false }, + // Only the deadline keeps the parent alive. It waits, signals once, and then reaps the child. + "spawn": { killed: true, kills: 1, events: ["exit", "close"], alive: false }, + // Node emits no 'close' once the parent has called disconnect(), so the events are not part of this case. "fork": { killed: true, kills: 1, alive: false }, - "execFile": { killed: true, kills: 1, alive: false }, - "exec": { killed: true, kills: 1, alive: false }, + "execFile": { killed: true, kills: 1, events: ["exit", "close"], callback: { killed: true }, alive: false }, + "exec": { killed: true, kills: 1, events: ["exit", "close"], callback: { killed: true }, alive: false }, "execFile, pipes open at the deadline": { killed: true, kills: 1, @@ -1735,11 +1736,16 @@ describe("options.timeout", () => { // The timers are not the globals, which a fake-timers library replaces. "spawn, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"] }, "execFile, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"], callback: "no error" }, - "timers replaced while the module loads, then restored": { killed: true, kills: 1, alive: false }, + "timers replaced while the module loads, then restored": { + killed: true, + kills: 1, + events: ["exit", "close"], + alive: false, + }, // No timeout, no timer: the parent leaves at once and the child stays. "no timeout": { killed: false, kills: 0, events: [], alive: true }, // The graph ends its child and its timers. No 'close' comes to clear the deadline. - "spawn in a Bun.ModuleGraph that is disposed": { killed: false, kills: 0, alive: false }, + "spawn in a Bun.ModuleGraph that is disposed": { killed: false, kills: 0 }, }; const isAlive = (pid: number) => { @@ -1766,12 +1772,7 @@ describe("options.timeout", () => { const report = stdout ? JSON.parse(stdout) : {}; const pid: number | undefined = report.pid; try { - let alive = pid !== undefined && isAlive(pid); - // The parent signals the child and exits. The child dies a moment later. - for (let i = 0; alive && expected.alive === false && i < 200; i++) { - await Bun.sleep(10); - alive = isAlive(pid!); - } + const alive = pid !== undefined && isAlive(pid); const reported = Object.fromEntries( Object.keys(expected).map(key => [key, key === "alive" ? alive : report[key]]), ); diff --git a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js index 748cf608aed3..05d1c32a1756 100644 --- a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js +++ b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js @@ -44,7 +44,10 @@ if (mode === "idle") { const kill = child.kill; child.kill = function (...args) { kills++; - return kill.apply(this, args); + const sent = kill.apply(this, args); + // The deadline is over. Staying to reap the child leaves no zombie for the test to take for alive. + this.ref(); + return sent; }; for (const event of ["error", "exit", "close"]) child.on(event, () => events.push(event)); process.on("exit", () => { From 04a9e04b09dd2c7369eff827041aaf50188246ae Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:53:09 +0000 Subject: [PATCH 4/7] node:child_process: the timeout cases do not signal a pid the parent reaped A child whose 'exit' the fixture parent saw is reaped. Its pid can belong to another process by the time the test cleans up, so the test now leaves that pid alone and takes the child as gone. --- test/js/node/child_process/child_process.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/js/node/child_process/child_process.test.ts b/test/js/node/child_process/child_process.test.ts index 0b736cfb34c5..f2496c5a4e4a 100644 --- a/test/js/node/child_process/child_process.test.ts +++ b/test/js/node/child_process/child_process.test.ts @@ -1770,7 +1770,8 @@ describe("options.timeout", () => { }); const [stdout, stderr, exitCode] = await Promise.all([parent.stdout.text(), parent.stderr.text(), parent.exited]); const report = stdout ? JSON.parse(stdout) : {}; - const pid: number | undefined = report.pid; + // A child whose 'exit' the parent saw is reaped, and its pid can be another process by now. + const pid: number | undefined = report.events?.includes("exit") ? undefined : report.pid; try { const alive = pid !== undefined && isAlive(pid); const reported = Object.fromEntries( From 286f4b54df551b53189b180e6d499e6206c57edd Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:53:36 +0000 Subject: [PATCH 5/7] node:child_process: the timeout cases catch a deadline that is not cleared, on every lane - `never` is 600 s, longer than the test timeout of every CI lane (90 s, and 270 s on ASAN). At 60 s, a parent that waited for the deadline still passed there: with the `clearTimeout` binding removed and a 90 s test timeout, "spawn, timers replaced after the module loaded" passed after 60.8 s. - A case for execFile()'s clear under replaced timers. It uses a missing file, so no 'exit' unrefs the timer before the clear. - On Windows the `exec` case runs a child that ends by itself. cmd.exe stays the child there, and the signal ends the shell only. --- test/js/node/child_process/child_process.test.ts | 10 +++++++++- .../fixtures/child-process-timeout-fixture.js | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/test/js/node/child_process/child_process.test.ts b/test/js/node/child_process/child_process.test.ts index f2496c5a4e4a..5ee0f9e5ac26 100644 --- a/test/js/node/child_process/child_process.test.ts +++ b/test/js/node/child_process/child_process.test.ts @@ -1733,9 +1733,17 @@ describe("options.timeout", () => { "spawn, 'exit' listener pauses stdout": { killed: false, kills: 0 }, "execFile, 'exit' listener pauses the pipes": { killed: false, kills: 0 }, "spawn of a missing file": { kills: 0, events: ["error", "close"] }, - // The timers are not the globals, which a fake-timers library replaces. + // The timers are not the globals, which a fake-timers library replaces. A fake setTimeout would + // arm nothing, and the parent would be gone before the 'exit'. A fake clearTimeout would clear + // nothing, and the parent would wait for the deadline. "spawn, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"] }, "execFile, timers replaced after the module loaded": { kills: 0, events: ["exit", "close"], callback: "no error" }, + // execFile()'s timer stops holding the parent at 'exit'. No 'exit' comes here, so only the clear releases it. + "execFile of a missing file, timers replaced after the module loaded": { + kills: 0, + events: ["error", "close"], + callback: { killed: false }, + }, "timers replaced while the module loads, then restored": { killed: true, kills: 1, diff --git a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js index 05d1c32a1756..d00c1c1f1c50 100644 --- a/test/js/node/child_process/fixtures/child-process-timeout-fixture.js +++ b/test/js/node/child_process/fixtures/child-process-timeout-fixture.js @@ -18,9 +18,10 @@ const restoreTimers = mode === "timers replaced while the module loads, then res const { spawn, fork, exec, execFile } = require("node:child_process"); if (restoreTimers) restoreTimers(); -// A deadline the parent must reach, and one it must clear long before. +// A deadline the parent must reach, and one it must clear long before. The second is longer than the +// test timeout of every CI lane (scripts/runner.node.ts), so a parent that waits for it fails its test. const soon = 200; -const never = 60_000; +const never = 600_000; if (mode === "idle") { // Long enough for every case to end it, and not for ever when a run fails to. @@ -78,8 +79,10 @@ if (mode === "idle") { break; case "exec": { // `exec` makes the shell become the child, so the pid reported is the one the signal has to reach. - const command = `"${process.execPath}" "${__filename}" idle`; - forget(watch(exec(process.platform === "win32" ? command : `exec ${command}`, { timeout: soon }, done))); + // cmd.exe has no `exec`. There the signal ends the shell only, so its child is one that ends by itself. + const posix = process.platform !== "win32"; + const command = `"${process.execPath}" "${__filename}" ${posix ? "idle" : "brief"}`; + forget(watch(exec(posix ? `exec ${command}` : command, { timeout: soon }, done))); break; } case "execFile, pipes open at the deadline": @@ -121,6 +124,10 @@ if (mode === "idle") { replaceTimers(); forget(watch(execFile(process.execPath, exits, { timeout: never }, done))); break; + case "execFile of a missing file, timers replaced after the module loaded": + replaceTimers(); + watch(execFile(__filename + ".does-not-exist", { timeout: never }, done)); + break; case "no timeout": forget(watch(spawn(process.execPath, idle, { stdio: "ignore", detached: true }))); break; From 710ede0a2813832d34777ffba128c7147c743c49 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 01:31:14 +0000 Subject: [PATCH 6/7] codegen: classify the timer functions that node:child_process binds The table of built-ins asks of each native function whether it can run code that the compiler has not seen. node:child_process now makes functionSetTimeout and functionClearTimeout with $newCppFunction, so both get an entry in host/unseen-code.builtins.ts. Neither runs such code: setTimeout takes a function and refuses everything else, and clearTimeout calls nothing. The globals of the same native functions are declared the same way. --- src/codegen/builtins/host/unseen-code.builtins.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/codegen/builtins/host/unseen-code.builtins.ts b/src/codegen/builtins/host/unseen-code.builtins.ts index 615f4c333e56..e6c3ff675791 100644 --- a/src/codegen/builtins/host/unseen-code.builtins.ts +++ b/src/codegen/builtins/host/unseen-code.builtins.ts @@ -63,6 +63,8 @@ const seenCodeEntries = [ "native:NewCppFunction:ModuleGraph.cpp:jsFunctionModuleGraphOfFrame", "native:NewCppFunction:NodeModuleModule.cpp:jsFunctionIsModuleResolveFilenameSlowPathEnabled", "native:NewCppFunction:Worker.cpp:jsFunctionPostMessage", + "native:NewCppFunction:node/NodeTimers.cpp:functionClearTimeout", + "native:NewCppFunction:node/NodeTimers.cpp:functionSetTimeout", "native:Bindgen:napi_body.bind.ts:threadsafeFunctionLiveCount", "process.binding", ]; From ccee22da9baef55ddb66077a819c424287763ea2 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 05:33:18 +0000 Subject: [PATCH 7/7] ci: retrigger