Repository navigation
Conversation
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.
|
Warning Review limit reached
Next review available in: 6 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
Comment |
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
|
Duplicate of #35805, which has the identical |
There was a problem hiding this comment.
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
closewhich clears the timer; synchronous throws (EPERM) never reach timer creation. - Noted execFile already forwards
timeoutto 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():
clearis registered on bothexitandclose. Deferred spawn errors (ENOENT/EACCES/…) emitcloseon nextTick, which clears it. Synchronous spawn throws (EPERM/EBADF) propagate before the timer is created. - execFile():
exitHandler(registered onclose) anderrorHandler(registered onerror, callsexitHandler) both null outtimeoutIdviaclearTimeout. execFile also forwardstimeouttospawn(), 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 >= 400and 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.allon stdout/stderr/exited,bunEnv, exitCode asserted last. - No prior review comments to address; no bugs from the bug-hunting pass.
Repro
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.tscreates thespawn()andexecFile()timeout kill timers with.unref()(L154, L342). Node'slib/child_process.jscreates both ref'd (no.unref()on eithersetTimeout, both inspawnWithSignalandexecFile), 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 existingexit/closehandlers alreadyclearTimeout()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 undertimeout: 30000must not hold the parent for 30 s.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