Skip to content

Process.run reads until stdout and stderr close, as Node's and Bun 1.4's spawnSync do - #1392

Merged
nicolas-abril merged 4 commits into
mainfrom
process-run-eof
Oct 9, 2026
Merged

nicolas-abril merged 4 commits into
mainfrom
process-run-eof

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1343. Supersedes #1391.

Bun 1.4 changed spawnSync to read its pipes until they close, as Node's does (oven-sh/bun#33832). Bun treated the old behavior as a bug: output a grandchild wrote after the direct child exited was silently lost. The PR adds no option to keep it.

Bend's contract ("waits for the direct child … a descendant holding an inherited pipe open does not extend the wait") was Bun 1.3's behavior, adopted for the C lane in #1054. This PR moves the contract to what Node, Bun 1.4 and, as far as I know, Python, Go and Rust do by default, and makes every lane follow it on any Bun version.

What changed:

  • process_run.c: reads until stdout and stderr both close, then reaps the child, all within the deadline. The FIONREAD drain from fix(io): stop Process.run draining descendant pipes #1054 goes. The exit descriptor (kqueue/pidfd) stays, but it's polled only after both pipes close: before that, the child may already have exited, and the descriptor would stay readable and make poll spin. Without kqueue or pidfd, it checks waitpid every 50ms, as before.
  • process_run.js: Bun 1.3's spawnSync returns when the direct child exits, so the JS lanes no longer use it. A worker runs the async Bun.spawn, which reads each pipe to its end on any Bun version. It enforces the timeout (clamped to 2³¹−1 ms, the most setTimeout takes) and the combined output limit; on either, it kills the child, cancels both readers and waits for the child to exit, as the C lane does. Each call passes the current process.env, which the worker would otherwise copy only when it starts. The main thread blocks on Atomics.wait (as long as spawnSync blocked it) and takes the answer with receiveMessageOnPort. The worker, its channel and the flag are made on the first call and reused; the worker is unref'd. If no answer comes within the timeout plus 1s (the worker failed to load or died), the call drops the worker and answers Fail EIO instead of hanging. No FFI and no comp.ts change, unlike JS Process.run parks on the child's pipes and exit, not Bun.spawnSync #1391.
  • GUIDE.md: the call returns once the child exits and stdout and stderr close, so a descendant holding a pipe open delays it, up to the timeout.
  • tests/io/process_run.bend:
    • inherited pipe (sleep 3 & echo hi, now with a 300ms timeout) expects a timeout failure.
    • descendant output is now printf A; (sleep 0.2; printf B) & exit 0 and expects "AB".
    • runaway descendant (yes & echo hi) now expects Fail 27 (output limit), the reverse of Process.run: a grandchild that keeps writing makes C answer Fail where JS answers Done #1051's fix. Its contains_answer helper is gone.
    • New closed stdio (exec >&- 2>&-; sleep 3, 300ms) expects a timeout: once the pipes close, the timeout still bounds the wait for the child to exit.
    • New largest timeout (/bin/echo hi with a timeout of 4294967295 ms) expects Done.

Trade-off: a sloppily backgrounded process holding stdout (cmd & in a shell one-liner) now runs Process.run into its timeout. That's the same footgun as in Node. Well-behaved daemons redirect their stdio.

Bun version: none required. The JS lanes give the same answers on Bun 1.3.14 and 1.4.2, so the release and the cluster can stay on 1.3.14 for this.

Size (ttok, from the merge base): process_run.c −196, process_run.js +480, process_run.bend +138, GUIDE.md +3; +425 in all.

Checked (macOS arm64):

  • process_run (19 lines) and process_run_parallel: interpreter, emitted JS and native all pass on Bun 1.3.14 and on 1.4.2.
  • process_run through a bun build --compile binary on 1.3.14 (the worker loads from a blob URL): passes.
  • No stray yes after runaway descendant: cancelling the readers closes the pipes.
  • A child that closes its stdio and runs 2s: 17 involuntary context switches, against about 1,600 with a 1ms waitpid loop.
  • In a standalone benchmark, about 0.85ms per call on either Bun version, plus about 5ms once to start the worker.
  • With process_run.js in a harness, on both Bun versions: a child sees an env var set after the worker started; a child killed on timeout is reaped before the call answers; a worker that throws on load answers Fail 5 after the timeout plus 1s.
  • bun gates/repo.ts: 50 / 50.

Checked (Arch Linux x86_64, glibc 2.44):

  • process_run and process_run_parallel: interpreter, emitted JS and native all pass on Bun 1.3.14 and on 1.4.2. No stray yes after.
  • The harness checks above (env, reaping, a broken worker) pass on both versions; a timeout answers 110.
  • A child that closes its stdio and runs 2s: 4 context switches in 1.8s across all threads, so the native lane blocks on the pidfd.

🤖 Generated with Claude Code

nicolas-abril and others added 4 commits October 7, 2026 20:28
…4's spawnSync do, so a descendant holding a pipe extends the wait up to the timeout

Bun 1.4 made spawnSync drain its pipes to EOF (oven-sh/bun#33832), which
the JS lane's Process.run inherits. The C lane now does the same: it reads
until both pipes close, then reaps the child, all within the deadline. The
exit descriptor (kqueue, pidfd) and the FIONREAD drain from #1054 go. A
grandchild's late output now arrives, and the descendant checks in
tests/io/process_run say so.

Fixes #1343.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…n the exit descriptor once both pipes close

Bun 1.3's spawnSync returns when the direct child exits, so the JS lane
matched the new contract only on Bun 1.4+. process_run.js now runs the
async Bun.spawn in a worker, which reads each pipe to its end on any
version, while the main thread waits on Atomics.wait for the answer.

The C lane polls the kqueue/pidfd exit descriptor again, but only after
both pipes close, instead of checking waitpid every 1ms. `open`, which
shadowed open(2), is now `reading`.

tests/io/process_run checks a runaway descendant (`yes & echo hi`, Fail
27) and a child that closes its stdio and keeps running (timeout).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…a killed child, and can't hang the caller

Bun's setTimeout turns a delay above 2^31-1 ms into 1 ms, so a U32
timeout near its max failed at once; the timer is clamped. Each call
sends process.env, which the worker otherwise copies only when it starts.
On timeout or EFBIG the worker awaits the killed child's exit before
answering, as the C lane does. The caller waits at most the timeout plus
1s, then drops the worker and answers EIO. One channel and one flag,
made with the worker, serve every call.

tests/io/process_run checks the largest timeout.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit 3f80b06 into main Oct 9, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the process-run-eof branch October 28, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Process.run JS lane times out after the direct child exits with inherited pipes

2 participants