Reading a child's stdout and stderr alternately in one loop deadlocks when one pipe is idle and the other fills
Symptom#
A tokio::process::Command spawn that pipes stdout+stderr and reads them alternately in one loop while !(both_eof) { read stdout; read stderr; } hangs: child.wait() never returns, output stops around the OS pipe capacity (~64 KB), then it hits the timeout. Writing only stdout (stderr to /dev/null) works; writing everything to a file works; only the alternating two-pipe read hangs.
Cause#
Two pipes share the read task. As soon as one pipe has no data yet (its write end stays open while the child lives) its read blocks, so the loop stops draining the OTHER, busy pipe. That pipe fills to capacity, the child blocks writing it, and because the child still holds both write ends open, the idle pipe never EOFs either. Everything waits on everyone: deadlock. It looks like an EOF bug (a read returns ~69632 bytes = pipe capacity then stops) but the real cause is the interleaved single reader.
Fix#
Give each pipe its own read task that drains it independently and forwards chunks over a bounded channel to one writer task. e.g. tokio::spawn(drain(stdout, tx.clone())); tokio::spawn(drain(stderr, tx.clone())); let writer = tokio::spawn(write_stream(rx)); where drain reads a pipe to EOF sending Vec<u8> chunks, and the writer owns the output (spill file + buffers). A separate task per pipe means an idle stderr can never stall a busy stdout. Bounding the channel (capacity ~32) still gives backpressure so memory stays capped.
Evidence#
reagent crates/tools/src/bash.rs: a sh -c tool timed out on head -c 1100000 /dev/zero (large stdout, empty stderr). Instrumentation showed stdout read exactly 69632 bytes (17x4096 ~ pipe capacity) then so-called EOF, while child.wait() blocked for the full 120s timeout. so_probe proved the minimal case: read-only-stdout worked (1100005 bytes, wait OK); alternate stdout+stderr hung. Fixing to one spawned task per pipe made it complete in ~14ms. All 25 tools tests pass after the change.