Repository navigation
fix(io): stop Process.run draining descendant pipes - #1054
Conversation
nicolas-abril
left a comment
There was a problem hiding this comment.
Thanks for picking up #1051. Bounding the post-reap drain with FIONREAD is the right idea. A child can't exit while blocked on a write, so all of its own output is already in the pipe when it's reaped. But on macOS this doesn't fix the issue yet, and the new test fails on the C lane there. The test gate runs on Macs, so it would go red.
What I see on macOS (M4)
tests/io/process_run.bendfrom this branch: every JS line isTrue. The C lane printsdescendant output: unexpected failure.- The issue's call,
Process.run("/bin/sh", ["-c", "yes & echo hi"], "", 65536, 1500), run 10 times:- C:
Fail 27 File too large10/10. - JS:
Done, with stdout exactlyhi\n, every time.
- C:
- The same call with
max_outputat 1 MB: C answersDone, but with 57–74 KB of stdout.
Why
The loop only notices the child's exit at its next waitpid. Meanwhile poll can sleep up to 50 ms after sh has already exited:
yeswrites, which wakespoll.- The loop reads 8 KB of
yesoutput. - The next pass reaps
sh, but by thenyeshas refilled the pipe. - The
FIONREADdrain takes a full 64 KB pipe ofyesoutput.
That adds up to 3 + 8192 + 65536 = 73731 bytes, which is exactly the size seen, and it's over 65536.
Suggested fix: wake on the child's exit
Add one more descriptor to the existing poll set that becomes readable when the child exits:
- macOS: a kqueue watching the child's exit (
EVFILT_PROC/NOTE_EXIT).polldoes report a kqueue as readable on macOS. - Linux: a
pidfd_opendescriptor.
The loop then sleeps until a pipe is ready, stdin is writable, the child exits, or the deadline passes, and the 50 ms tick goes away. If the descriptor can't be created, the loop falls back to today's 50 ms tick. Your FIONREAD drain then runs at the moment of exit, which is what Bun.spawnSync effectively does.
I prototyped this on top of your branch (diff below). On the same M4:
- The issue's call:
Done 0with stdouthi\n, 10/10, the same as JS. - The full test file: every line
True. - Not tested: the Linux (pidfd) path.
Smaller points
drainflag: it is always equal tochild < 0, so it can go.- Dead code: once the loop breaks right after the drain,
int ms = 0(the post-reap case),if (child < 0 && count == 0)andif (ready == 0 && child < 0)can never run. - The test stays timing-dependent: under load (20 runs in parallel), even the prototype failed the issue's call once in 40 runs. Nothing can tell the child's bytes from the grandchild's in a shared pipe, so the result depends on whether
yesis scheduled beforeshexits. Please use a largermax_outputin the test, for example 1 MB.mainstill fails it deterministically, because it drainsyesforever, while a fixed version answersDone. - Linux JS lane: the max-buffer and timeout differences you saw for descendant cases are Bun's behaviour and a separate issue. Feel free to open one.
Prototype diff (on top of this branch)
diff --git a/bend2/effs/process_run.c b/bend2/effs/process_run.c
index e4ccb52a..add7688e 100644
--- a/bend2/effs/process_run.c
+++ b/bend2/effs/process_run.c
@@ -4,6 +4,11 @@
#include <spawn.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
+#ifdef __APPLE__
+#include <sys/event.h>
+#else
+#include <sys/syscall.h>
+#endif
extern char** environ;
@@ -94,6 +99,22 @@ static bool process_drain(ProcessCall* p, int fd, bool error) {
return true;
}
+// A descriptor that polls readable once the child exits.
+static int process_exitfd(pid_t child) {
+#ifdef __APPLE__
+ int kq = kqueue();
+ struct kevent ev;
+ EV_SET(&ev, child, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL);
+ if (kq >= 0 && kevent(kq, &ev, 1, NULL, 0, NULL) != 0) {
+ close(kq);
+ kq = -1;
+ }
+ return kq;
+#else
+ return (int)syscall(SYS_pidfd_open, child, 0);
+#endif
+}
+
static int process_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL);
return flags < 0 ? -1 : fcntl(fd, F_SETFL, flags | O_NONBLOCK);
@@ -124,7 +145,7 @@ static void process_call(IoWork* w) {
int pipes[3][2] = {{-1, -1}, {-1, -1}, {-1, -1}};
pid_t child = -1;
int status = 0;
- bool drain = false;
+ int exitfd = -1;
for (int i = 0; i < 3; i += 1) {
if (process_pipe(pipes[i]) != 0) {
p->code = errno;
@@ -191,6 +212,7 @@ static void process_call(IoWork* w) {
if (p->input_len == 0) {
close(pipes[0][1]); pipes[0][1] = -1;
}
+ exitfd = process_exitfd(child);
u64 deadline = io_tick() + (u64)p->timeout * 1000000ull;
u64 written = 0;
while (p->code == 0) {
@@ -198,7 +220,6 @@ static void process_call(IoWork* w) {
pid_t got = waitpid(child, &status, WNOHANG);
if (got == child) {
child = -1;
- drain = true;
if (pipes[0][1] >= 0) {
close(pipes[0][1]); pipes[0][1] = -1;
}
@@ -207,7 +228,7 @@ static void process_call(IoWork* w) {
break;
}
}
- if (drain) {
+ if (child < 0) {
for (int i = 1; i < 3; i += 1) {
if (pipes[i][0] >= 0 && !process_drain(p, pipes[i][0], i == 2)) {
break;
@@ -220,8 +241,8 @@ static void process_call(IoWork* w) {
p->code = ETIMEDOUT;
break;
}
- struct pollfd fds[3];
- int roles[3];
+ struct pollfd fds[4];
+ int roles[4];
nfds_t count = 0;
for (int i = 0; i < 3; i += 1) {
int fd = i == 0 ? pipes[0][1] : pipes[i][0];
@@ -230,15 +251,13 @@ static void process_call(IoWork* w) {
roles[count++] = i;
}
}
- if (child < 0 && count == 0) {
- break;
- }
- int ms = 0;
- if (child >= 0) {
- u64 left = (deadline - now + 999999ull) / 1000000ull;
- ms = (int)(left > 50 ? 50 : left);
+ if (exitfd >= 0) {
+ fds[count] = (struct pollfd){exitfd, POLLIN, 0};
+ roles[count++] = 3;
}
- int ready = poll(fds, count, ms);
+ u64 left = (deadline - now + 999999ull) / 1000000ull;
+ u64 most = exitfd >= 0 ? 1000000 : 50;
+ int ready = poll(fds, count, (int)(left > most ? most : left));
if (ready < 0) {
if (errno == EINTR) {
continue;
@@ -246,8 +265,8 @@ static void process_call(IoWork* w) {
p->code = errno;
break;
}
- if (ready == 0 && child < 0) {
- break;
+ if (exitfd >= 0 && fds[count - 1].revents != 0) {
+ continue;
}
for (nfds_t k = 0; k < count && p->code == 0; k += 1) {
if (fds[k].revents == 0) {
@@ -292,6 +311,9 @@ static void process_call(IoWork* w) {
: WIFSIGNALED(status) ? 128 + (u32)WTERMSIG(status) : 1;
}
done:
+ if (exitfd >= 0) {
+ close(exitfd);
+ }
for (int i = 0; i < 3; i += 1) {
for (int j = 0; j < 2; j += 1) {
if (pipes[i][j] >= 0) {This review was written with AI assistance (Claude Code) and checked by me.
The post-reap drain only ran once the loop noticed the child at its next waitpid, up to 50 ms after it exited, so a descendant that refilled the pipe in that window was drained whole. Poll an extra descriptor that becomes readable at exit -- a kqueue on NOTE_EXIT for macOS, a pidfd on Linux -- and fall back to the tick where neither exists. The drain flag was always equal to child < 0, and the count == 0 and ready == 0 branches below it could never run, so they go too. Role 3 is the exit descriptor, which carries no pipe. The descendant test takes 1 MB of headroom: nothing in a shared pipe tells the child's bytes from its descendants', so whether "hi" survives depends on whether the grandchild is scheduled first.
|
Pushed Your diagnosis matched what I measured. I pulled the real
73731 is your Two things I could not test, so you will have to:
On the test, I took your 1 MB. Measured sequentially, which is how
So the 1 MB buys the ~10x, not the descriptor. At 1 MB the remaining failures are pre-exit volume rather than post-exit drain, which is the part neither of us can fix from the runtime. Worth knowing: 7/300 is still ~2%, and every one I caught is One deviation from the diff you posted, and one guard:
Also checked: no fd leak over 200 runs, and the two other loop changes are covered, the timeout path ( Happy to put the bounded-grandchild test in instead of the 1 MB if you prefer the gate not to carry the flake. |
…le the child lives, process_drain reads what FIONREAD counts, and the exit descriptor is the poll set's last slot Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
left a comment
There was a problem hiding this comment.
Verified on macOS (M4), where the kqueue path runs: the issue's call answers Done 0 with stdout "hi\n" 20/20 on C (Fail 27 on main), the same as JS; process_run and process_run_parallel pass on the check, interpreted, C and JS lanes, 100/100 sequential. I pushed one simplification on top (7db24b2): the loop waits only while the child lives, process_drain reads what FIONREAD counts, and the exit descriptor is the poll set's last slot. Thanks!
…g its pipes cannot extend it As bendlang#1054 did on POSIX: once the child exits, the Windows loop reads only the bytes already in the pipes (PeekNamedPipe) and stops. The descendant case moves to process_run_posix, as Bun's spawnSync on Windows reads until every writer closes the pipes, so the JS lane cannot stop at the child's exit. process_run.c's cap takes bendlang#1054's growth. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Summary
Fixes #1051
Verification
BEND_NO_TELEMETRY=1 bun bend2/main.ts tests/io/process_run.bend --check-onlyTruegit diff --checkbun gates/repo.tscould not run becausettokis not installed in this environmentThe local Linux/Bun 1.4.2 JS lane reports max-buffer/timeout differences for descendant cases; the issue was reported on macOS/Bun, where the JS lane returns
Done.