Repository navigation
Add bounded Process.run effect - #1030
Conversation
2230f3d to
72e8071
Compare
nicolas-abril
left a comment
There was a problem hiding this comment.
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 macOS2. 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.
|
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. |
72e8071 to
73c6f06
Compare
|
Thank you @oxura! Both fixes work: on macOS the child now sees SIGPIPE at its default ( 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 One small lane difference is left, in case you want to follow up: with 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. |
|
Opened #1051 for the remaining C/JS difference with a grandchild that keeps writing ( |
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, andDone{(status, (stdout, stderr))}is returned even for a nonzero exit. Spawn errors, timeouts, and combined output beyondmax_outputreturnFail; 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_spawnpon 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 usesBun.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
--checkupproduced 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.tsreported PASS: 46 / 46. The cluster gate and a macOS build were unavailable; TypeScript checking reports four diagnostics in untouchedbend2/bend.tsandbend2/docs/gen_*.ts(missing canvas types).