Skip to content

JS Process.run parks on the child's pipes and exit, not Bun.spawnSync - #1391

Closed
nicolas-abril wants to merge 3 commits into
mainfrom
js-process-park
Closed

nicolas-abril wants to merge 3 commits into
mainfrom
js-process-park

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Fixes #1343. Supersedes #1371.

Bun 1.4 changed spawnSync to wait for the child's pipes to close, as Node's spawnSync always has, instead of for the child to exit. A background grandchild holding stdout open (sh -c "sleep 3 & echo hi") now runs Process.run into its timeout. On Bun 1.3.14, main passes; on 1.4.2 it fails inherited pipe and descendant output. The 1.4 release notes don't mention the change.

No spawnSync call gives the documented behavior, and the JS runtime's loop is synchronous, so Bun's async spawn callbacks never run. The JS lane therefore does what process_run.c does, over bun:ffi, but parks on the loop instead of blocking it.

What changed:

  • process_run.js:
    • posix_spawnp on close-on-exec pipes, moved above fd 2, with SIGPIPE back at its default.
    • Parks on stdin, stdout, stderr and an exit descriptor: a pidfd on Linux, a kqueue on macOS, or a 50ms tick when neither is available.
    • Reads one chunk (64KB) per pipe per wake, so a descendant that keeps writing (yes &) can't keep the call from seeing the exit. Once the direct child is reaped, it reads at most each pipe's capacity more (what it could hold at the exit) and answers: 64KB on macOS, F_GETPIPE_SZ on Linux.
    • Like the C lane, it needs glibc 2.34+ on Linux (posix_spawn_file_actions_addclosefrom_np).
  • io_park_on (comp.ts, +22 tokens): in place of fd, it also takes a list of [fd, out] pairs and wakes on the first one ready. Waiting stays on the one select loop: no epoll, and no kqueue beyond the exit notification. Existing callers are unchanged.
  • Other computations now run while a child does, on the JS lane as on the C lane. Before, the JS lane blocked the whole loop.
  • Docs: GUIDE.md and EFFECTS.md are updated.

Compared to #1371:

  • It parks on the event loop instead of running its own poll loop.
  • It wakes on the child's exit fd instead of polling waitpid every 50ms.
  • One reused read buffer, with chunks copied out of it, so tiny reads don't each hold 8KB.
  • The argv/env memory stays referenced until the call ends.
  • No kill after ECHILD, so it can't hit a reused pid.
  • No spinning in the last millisecond before the deadline.
  • One read path and one kill-and-reap path.

It keeps #1371's closed-stdin regression test (its author is credited as co-author).

New test: process_run_beside checks that main prints while a forked Process.run child is still running. Main's code fails it: the order flips.

Checked (macOS arm64):

  • Bun 1.3.14 and 1.4.2: process_run, process_run_parallel, process_closed_stdin and process_run_beside pass in the interpreter, emitted JS and native lanes.
  • 55 IO tests on Bun 1.4.2, interpreter and emitted JS: output is identical to main, except the two Process.run JS lane times out after the direct child exits with inherited pipes #1343 checks (now True) and the two new tests.
  • Extra checks: 3MB through cat; a child that ignores 3MB of stdin (EPIPE); yes over the limit (Fail 27); a child that closes its pipes and exits 200ms later; 64KB on each of stdout and stderr written just before exit, and 500KB.
  • bun gates/repo.ts: 50 / 50.

Not checked:

  • Linux has not been run. The pidfd call (syscall 434), addclosefrom_np, F_GETPIPE_SZ (1032) and O_NONBLOCK 0x800 are only checked against headers.
  • comp.ts is at 63,997 of its 64,000-token cap.

🤖 Generated with Claude Code

…, so a descendant holding a pipe no longer runs it into the timeout on Bun 1.4, and other computations run meanwhile

Bun 1.4's spawnSync waits for the pipes' EOF, as Node's does, not for the
direct child. process_run.js now follows process_run.c over bun:ffi:
posix_spawnp on close-on-exec pipes, then io_park_on over stdin, stdout,
stderr and an exit descriptor (pidfd, or a kqueue on macOS), draining only
what is buffered once the child is reaped. io_park_on takes a list of
[fd, out] pairs in place of fd, and wakes on the first ready.

Fixes #1343. Supersedes #1371, whose closed-stdin regression this keeps.

Co-Authored-By: Mansurshakh Japarov <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril and others added 2 commits October 7, 2026 19:44
…f FIONREAD, and needs glibc 2.34 as the C lane does

Drops the variadic ioctl and its Apple arm64 padding, and the optional
addclosefrom_np binding.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ld that enlarged its pipe loses no output

A macOS pipe holds 64KB; a Linux one reports its size through
F_GETPIPE_SZ, which io_sys's fcntl reads.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Closing in favor of #1392, which makes every lane read until stdout and stderr close (Bun 1.4's spawnSync behavior) on any Bun version, without FFI or a comp.ts change.

@nicolas-abril
nicolas-abril deleted the js-process-park 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

1 participant