Skip to content

child_process: keep spawn/execFile timeout kill timers ref'd like Node - #35808

Closed
robobun wants to merge 1 commit into
mainfrom
farm/2bc5b5ae/child-process-timeout-ref
Closed

robobun wants to merge 1 commit into
mainfrom
farm/2bc5b5ae/child-process-timeout-ref

Conversation

@robobun

@robobun robobun commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Repro

const { spawn } = require("node:child_process");
const c = spawn(process.execPath, ["-e", "setTimeout(() => {}, 30000)"], {
  stdio: "ignore",
  timeout: 500,
  killSignal: "SIGKILL",
});
console.log(c.pid);
c.unref();

Node v26.3.0: parent lives ~505 ms, delivers the kill, grandchild dies, parent exits.
Bun before: parent exits in ~40 ms with nothing ref'd, grandchild orphaned for 30 s.

Adding an unrelated ref'd interval to the parent makes Bun deliver the SIGKILL at 500 ms, which isolates the cause: the kill timer exists and works, it just is not holding the loop.

Cause

src/js/node/child_process.ts creates the spawn() and execFile() timeout kill timers with .unref() (L154, L342). Node's lib/child_process.js creates both ref'd (no .unref() on either setTimeout, both in spawnWithSignal and execFile), so the timer itself keeps the loop alive until the deadline when the user has unref'd the child handle.

Fix

Drop the .unref() from both timers. The existing exit/close handlers already clearTimeout() when the child finishes before the deadline, so a ref'd timer does not extend the parent's life past the child's natural exit; the second test below pins that.

Verification

New tests in test/js/node/child_process/child_process.test.ts:

  • timeout kill timer keeps the event loop alive when the child is unref'd: spawns the repro above, asserts the grandchild is dead after the parent exits and that the parent lived past the deadline.
  • timeout kill timer is cleared when the child exits early, releasing the loop: a child that exits immediately under timeout: 30000 must not hold the parent for 30 s.
# fail-before (released bun):
$ USE_SYSTEM_BUN=1 bun test test/js/node/child_process/child_process.test.ts -t "timeout kill timer"
  { grandchildAlive: true, parentWaitedForTimeout: false }   vs expected { false, true }
 1 pass  1 fail

# pass-after (debug build):
$ bun bd test test/js/node/child_process/child_process.test.ts -t "timeout kill timer"
 2 pass  0 fail

Node's ported timeout tests still pass (test-child-process-exec-timeout-{expire,kill,not-expired}.js, test-child-process-{spawn,fork}-timeout-kill-signal.js).


no test proof · iteration 0 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/node/child_process/child_process.test.ts

Node's lib/child_process.js creates the spawn() and execFile() timeout
timers without .unref(), so a user who unref()s the returned child
still has the kill timer holding the loop open until the deadline; the
child is then killed and the parent exits. Bun unref'd both timers, so
the same sequence drained the loop immediately and orphaned the child.

The existing exit/close handlers already clearTimeout() when the child
finishes early, so a ref'd timer does not prolong the parent past the
child's natural exit.
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 6 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1620886e-0379-49be-ab5d-c04da633fc91

📥 Commits

Reviewing files that changed from the base of the PR and between 916492f and efa29cf.

📒 Files selected for processing (2)
  • src/js/node/child_process.ts
  • test/js/node/child_process/child_process.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 2:21 PM PT - Jul 25th, 2026

@robobun, your commit efa29cf is building: #81502

@github-actions

Copy link
Copy Markdown
Contributor

This PR may be a duplicate of:

  1. node:child_process: keep the timeout timer ref'd so spawn({timeout}) + child.unref() still delivers the kill #35805 - Also removes .unref() from the spawn/execFile timeout kill timers in child_process.ts to match Node.js behavior

🤖 Generated with Claude Code

@robobun

robobun commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator Author

Duplicate of #35805, which has the identical src/ change and was opened first. Closing in favor of that PR.

@robobun robobun closed this Jul 25, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — drops the .unref() on the spawn/execFile timeout kill timers to match Node, so an unref'd child still gets killed at the deadline.

What was reviewed:

  • Confirmed both timers are cleared on every exit path (spawn: exit/close; execFile: close/error → exitHandler), so a ref'd timer never outlives the child.
  • Checked spawn-failure paths: deferred errors (ENOENT) emit close which clears the timer; synchronous throws (EPERM) never reach timer creation.
  • Noted execFile already forwards timeout to spawn(), so two timers exist — pre-existing, both cleared correctly, not made worse here.
  • Tests: fail-before/pass-after verified per description; the first test's dual assertion (parentWaitedForTimeout + grandchildAlive) discriminates the regression independently.
Extended reasoning...

Overview

Two-line runtime change in src/js/node/child_process.ts: removes .unref() from the setTimeout kill timers in spawn() (L154) and execFile() (L342). Adds two subprocess-based regression tests in test/js/node/child_process/child_process.test.ts.

Security risks

None. This only changes whether an internal timer holds the event loop open; no new inputs are parsed, no privilege or trust boundary is touched.

Level of scrutiny

Low-to-medium. This is a Node.js compat parity fix in a built-in JS module, with the mechanism fully explained and cross-referenced against Node's lib/child_process.js (which does not unref either timer). The only real risk of removing .unref() is a ref'd timer keeping the parent alive past the child's natural exit — I traced every path that ends the child and confirmed the timer is always cleared:

  • spawn(): clear is registered on both exit and close. Deferred spawn errors (ENOENT/EACCES/…) emit close on nextTick, which clears it. Synchronous spawn throws (EPERM/EBADF) propagate before the timer is created.
  • execFile(): exitHandler (registered on close) and errorHandler (registered on error, calls exitHandler) both null out timeoutId via clearTimeout. execFile also forwards timeout to spawn(), so two timers exist — that redundancy is pre-existing and both are cleared on exit; the second new test pins this contract at 15 s vs a 30 s timeout.

Other factors

  • The PR description shows fail-before (USE_SYSTEM_BUN=1) / pass-after (bun bd) output and lists the ported Node timeout tests that still pass.
  • The first test asserts two independent facts (parentLivedMs >= 400 and grandchild-dead-after-poll), so even if a platform's process-tree cleanup happened to kill the orphan, the timing assertion would still catch a regression. The grandchild-alive poll uses a bounded 1 s window with cleanup SIGKILL on failure, so it won't leak processes.
  • Tests follow harness conventions: await using + Promise.all on stdout/stderr/exited, bunEnv, exitCode asserted last.
  • No prior review comments to address; no bugs from the bug-hunting pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants