Skip to content

Add bounded Process.run effect - #1030

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
oxura:feat/process-run
Sep 25, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
oxura:feat/process-run

Conversation

@oxura

@oxura oxura commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds Process.run(program, args, input, max_output, timeout_ms) to Base. The executable is invoked directly with literal arguments, without a shell. UTF-8 stdin is supplied, and Done{(status, (stdout, stderr))} is returned even for a nonzero exit. Spawn errors, timeouts, and combined output beyond max_output return Fail; both limits must be positive. The child inherits the caller's current directory and environment. This effect is not a sandbox.

Native C uses posix_spawnp on an IO helper thread, polls stdin/stdout/stderr concurrently, reaps the child, and closes inherited descriptors (Linux: glibc 2.34+ closefrom; macOS: POSIX_SPAWN_CLOEXEC_DEFAULT). JS uses Bun.spawnSync, which blocks its event loop while the child runs. Both limitations are documented.

Verification: new IO fixtures cover literal arguments, Unicode input, both output streams, nonzero/signaled exits, exact and combined output bounds, timeout, missing executable, invalid limits, NUL arguments, and concurrent calls. The checker/interpreter, emitted JS, native C, and local --checkup produced the expected output. A 32 KiB bidirectional transfer succeeded on C and JS; a Linux smoke check found no inherited sentinel descriptor. bun gates/repo.ts reported PASS: 46 / 46. The cluster gate and a macOS build were unavailable; TypeScript checking reports four diagnostics in untouched bend2/bend.ts and bend2/docs/gen_*.ts (missing canvas types).

@oxura oxura changed the title Add bounded Process.run effect for CLI tools Add bounded Process.run effect Sep 24, 2026
@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, the design is what I'd want. I tested it on macOS (Apple M4) merged onto current main: process_run and process_run_parallel pass in the check, interpreted, JS and C lanes, 3 MB written to stdout and stderr at once completes without a deadlock on both lanes, a Listener the program holds does not show up in the child (lsof inside it), four forked calls overlap on C (four 0.5 s sleeps in 511 ms), and bun gates/repo.ts passes 46/46.

Two bugs, both places where the C lane and the JS lane disagree, and one housekeeping change.

1. The native child starts with SIGPIPE ignored. io_loop ignores SIGPIPE, and a signal ignored in the parent stays ignored across exec, so every child of the C lane inherits it:

Process.run("/usr/bin/perl", ["-e", "print $SIG{PIPE} // q(default)"], "", 4096, 2000)

prints IGNORE on C and default on JS (and from a normal shell). A child that runs a pipeline then behaves differently: in yes | head -1, yes gets EPIPE write errors instead of dying on SIGPIPE. Resetting it in the spawn attributes fixes it, which means creating attr on every platform, not only under __APPLE__:

sigset_t def;
sigemptyset(&def);
sigaddset(&def, SIGPIPE);
posix_spawnattr_setsigdefault(&attr, &def);
// and POSIX_SPAWN_SETSIGDEF in the flags, beside POSIX_SPAWN_CLOEXEC_DEFAULT on macOS

2. A background grandchild turns a finished run into a timeout on C. process_call only calls waitpid once all three pipes are closed, so a grandchild that inherited stdout keeps the loop polling until the deadline:

Process.run("/bin/sh", ["-c", "sleep 3 & echo hi"], "", 4096, 1500)

C answers Fail with ETIMEDOUT after 1501 ms and the hi is lost; JS answers Done{(0, ("hi\n", ""))} in 4 ms. Both lanes leave the sleep running. Please reap the child on every iteration and, once it has exited, drain what is readable and stop, so both lanes answer the same thing. Spawning it in its own process group (POSIX_SPAWN_SETPGROUP, then kill(-child, SIGKILL) on a timeout or an overflow) would also stop a timed-out run from leaving descendants behind.

3. Please drop the CHANGELOG.md entry. The changelog is written by the release commits (see 2.0.27, #1024); an ## Unreleased section isn't part of that flow and would conflict with the next release.

A test for each of 1 and 2 would keep the lanes from drifting again; the two calls above are enough, since both check exact output.

Note: this review was written by an AI after it tested the PR for me and I reviewed the findings. If anything here is wrong, reply and I will review it myself.

@oxura

oxura commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review on the updated branch: spawn attributes reset SIGPIPE on Linux and macOS; the C loop reaps the direct child while polling, drains readable output, then stops even if a descendant holds a pipe. Added both exact-output regressions, documented the descendant behavior, and removed the Unreleased changelog entry. Verified the regressions on interpreted JS, emitted JS, and native C; the repository gate passes 46/46. macOS has not been rerun locally.

@nicolas-abril
nicolas-abril merged commit 03da6fb into bendlang:main Sep 25, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you @oxura! Both fixes work: on macOS the child now sees SIGPIPE at its default (yes | head -1 behaves as it does from a shell), and sleep 3 & echo hi answers Done hi in about 55 ms on C instead of timing out. With the regression tests in place, both lanes stay in step. Merged.

Right after the merge we pushed 7ba8f9c29a9ec8ad09ab35ce10a4f0fa9ab8a6c5. This branch was started before #1042 changed how effect files register, so the merge had no text conflicts but broke on main: process_run.js never called io_eff, so the interpreter and JS lanes stopped with no effect registers Process.run, while C still built from the old CID_PROCESS_RUN macro. The commit changes three lines: the JS file now ends with io_eff(CID(Process.run), process_run) and walks the list by CID(Con), and the C file registers under CID(Process.run). process_run and process_run_parallel pass in the check, interpreter, JS and C lanes.

One small lane difference is left, in case you want to follow up: with sh -c "yes & echo hi", C answers Fail (output limit exceeded) and JS answers Done hi. After the direct child exits, the C loop keeps draining for as long as a pipe is readable, so a grandchild that never stops writing eventually hits the limit. Output is still capped, so it can't hang. A single drain pass after the child exits would make C match JS.

Thanks again for the careful work on this one 🙏

Note: this comment was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Opened #1051 for the remaining C/JS difference with a grandchild that keeps writing (sh -c "yes & echo hi"), with the cause and a possible fix.

@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
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.

2 participants