fix(broker): replace blocking stdout writer task with tokio::io#841
Merged
Conversation
Two writer tasks in the broker spawn-loop body used `std::io::stdout().lock().write_all()` from inside `tokio::spawn`: - `src/pty_worker.rs` (PTY worker → SDK frame stream) - `src/main.rs::run_headless_worker` (headless worker → SDK frame stream) Synchronous I/O from an async task blocks the OS worker thread when the parent reader stalls. On macOS the kernel pipe buffer is 64KB; once it fills, the `write_all()` syscall blocks, taking the tokio thread with it. On resumption, the `tokio::select!` wakeup that drives `pty_rx` is lost — the broker stays parked in `_pthread_cond_wait` and downstream PTY workers freeze. Reproduces in proactive-runtime-m1 (9 PTY worker fanout). All workers froze within 4 seconds of each other across three independent runs; broker-init's tokio threads were either parked in `write()` (pre-drain fix) or in `_pthread_cond_wait` (post-drain fix — same blocking-writer design, now lost-wakeup mode). Switch both writer tasks to `tokio::io::stdout()` + `AsyncWriteExt::write_all().await`. Pipe backpressure now surfaces as a normal `Pending` future; the OS thread never blocks; the parent closing the pipe becomes a clean shutdown signal (break out of the loop). Verification - cargo build --release — clean - cargo test --release --lib — 232/232 pass - cargo test --release --test '*' — 8/8 pass The ricky #98 loader-level unblocker (which monkeypatches `child_process.spawn` on the Node SDK side) was a backstop; this is the upstream root-cause fix on the Rust broker side. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two writer tasks in the broker spawn-loop body used
std::io::stdout().lock().write_all()from insidetokio::spawn:Synchronous I/O from an async task blocks the OS worker thread when the parent reader stalls. On macOS the kernel pipe buffer is 64KB; once it fills, the `write_all()` syscall blocks, taking the tokio thread with it. On resumption, the `tokio::select!` wakeup that drives `pty_rx` is lost — the broker stays parked in `_pthread_cond_wait` and downstream PTY workers freeze.
This is the upstream root-cause fix corresponding to relay#838 (SDK-side drain — necessary but not sufficient) and ricky#98 (Node-side spawn monkeypatch — backstop). After both of those landed, multi-agent workflows still wedged at fanout because the broker writer task itself remained the trap.
Reproducer
Any workflow that fans out to ≥9 PTY workers with sustained `worker_stream` traffic. We reproduced via `proactive-runtime-m1` four times across two days — every wedge had the same shape:
The 75e5064d shape is the same blocking-writer design in cancel-unsafe steady state: workers had drained their kernel pipes by the time we sampled, but the wakeup chain into `tokio::select!` had been lost when one of them temporarily stalled in `write_all` earlier. All 9 workers' logs froze within 4 seconds of each other.
What changed
Backpressure now surfaces as `Pending`. The OS thread never blocks. No wakeups are lost.
Verification
Suggested follow-on tests (next PR, not in scope here):
Related