Skip to content

Keep JS Process.run bounded by the direct child - #1371

Closed
oxura wants to merge 1 commit into
bendlang:mainfrom
oxura:fix/process-direct-child
Closed

oxura wants to merge 1 commit into
bendlang:mainfrom
oxura:fix/process-direct-child

Conversation

@oxura

@oxura oxura commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Closes #1343.

Replace Bun.spawnSync's inherited-pipe lifecycle with synchronous POSIX spawn/poll/waitpid through Bun FFI, matching the existing native contract. Feed stdin and capture both streams concurrently, enforce the combined byte limit and a monotonic deadline, reap the direct child, then drain only already-buffered output. Descendants retaining the pipes do not extend the wait. Timeout/output failures kill and reap the child and close all parent descriptors.

Keep literal argv, spawn errno, signal exit codes and child SIGPIPE defaults. Relocate pipe fds below 3 before setting up child stdio. Handle Darwin arm64's stack-based variadic ABI for fcntl and ioctl. No expectation in the existing process fixture is relaxed; add a real closed-parent-stdin regression with JS/C twins.

Verification on Linux (Bun 1.3.13, Clang 22):

  • Existing 16-check process fixture: every check True in source IO CLI, emitted JS and native CPU binary.
  • Existing parallel process fixture: True.
  • Closed-parent-stdin regression: 0:payload: on JS and native with 1/4 threads.
  • Real-process stress: 100 timeouts under forced GC, exact 4,000-byte environment value preserved, stable open-fd count, no unreaped direct children.
  • Repository gate: PASS: 50 / 50 including staged fixtures.
  • Clang arm64-apple-darwin ABI probe verifies the padded FFI call places ioctl's pointer on the same stack slot as a real variadic call. This is code-generation evidence, not a macOS runtime test.

MacOS runtime verification remains for the project CI/maintainer; no Mac, GPU or mini-cluster run is claimed. No native runtime, gates/caps, GUIDE or release-changelog changes.

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Rechecked the combined fixes against upstream 237c7226 / Bend 2.0.36. The source IO CLI and emitted JS still pass every existing Process.run check (16/16 True), including inherited pipes and descendant output; the closed-parent-stdin regression prints 0:payload:. The unchanged repository gate passes 50/50. The macOS limitation remains as documented: arm64 variadic stack placement was checked with Clang code generation, not on macOS hardware.

@nicolas-abril

nicolas-abril commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the investigation and the closed-stdin regression test. Closing in favor of #1391, which takes the same bun:ffi approach but parks on the event loop through io_park_on instead of running its own poll loop, wakes on the child's exit fd (pidfd/kqueue) instead of polling waitpid every 50ms, and covers the review points (8KB retention per chunk, argv/env lifetime, ECHILD, poll timeout rounding). Your closed-stdin test is kept there, with you credited as co-author.

@nicolas-abril

nicolas-abril commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

To expand on why: both PRs take the same bun:ffi approach, since Bun 1.4's spawnSync waits for the pipes' EOF, like Node's, so no spawnSync call can keep the documented behavior. The differences:

  • The rest of the program keeps running. This PR's own poll loop blocks the whole JS event loop until the child exits or times out, so every socket, sleep and channel stalls meanwhile. JS Process.run parks on the child's pipes and exit, not Bun.spawnSync #1391 parks on the loop, as the native lane does. Its process_run_beside test checks this.
  • It notices the exit right away. Without an exit fd, the exit is only seen by polling waitpid every 50ms, which adds up to 50ms per call in exactly the descendant-holds-the-pipe case.
  • Bugs fixed:
    • each small read kept a whole 8KB buffer alive, so a child printing short lines could hold hundreds of MB while under maxOutput;
    • nothing kept the argv/env buffers alive while the child started;
    • after ECHILD it could SIGKILL a reused pid;
    • the truncated poll timeout spun the CPU in the last millisecond before the deadline.

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review and for preserving the closed-stdin regression with co-author credit in #1391. Understood the event-loop parking and exit-fd differences; I will leave this superseded implementation closed.

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