Skip to content

fix(executor): stream an unbounded external producer into an in-shell pipeline tail (#761) - #784

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/761-pipeline-unbounded-producer
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/761-pipeline-unbounded-producer

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes #761. An unbounded external producer feeding a pipeline whose rightmost stage is served in-shell (a builtin / inline arm such as true/:/echo/read, or a compound body) no longer hangs: yes | true (and yes | :, yes | read -r l, yes | cat | true, seq 1 100000000 | true, yes | while read; do break; done) return in well under a second with PIPESTATUS[0]=141.

This is very likely the reproducible root of the older #723 freeze (yes | head -5 | while read …) that could not be reproduced in 190+ attempts across 13 shapes — none of those shapes used an inline-arm / builtin last stage, which is the trigger. See the #723 link at the bottom.

Root cause (interleaving, with file:line)

The rightmost element was never started until the sequential stage loop had materialized every upstream element's whole stdout:

  • execute_simple_pipeline (src/executor/pipeline_exec.rs) walks stages one at a time; stage 0 of yes | true is external yes, dispatched to execute_external_pipeline_stage_inner (src/executor/pipeline_stages.rs), which ends in child.wait_with_output() — read_to_end on yes's stdout.
  • yes is unbounded, so that read never returns; true (the tail) is never reached; its read end is never closed; yes never observes a termination.
  • So the side that never observes the producer's termination is the shell's pump: it keeps draining instead of admitting that the consumer will never run. The producer's own footprint is flat; the consumer grew (~250 MB/s).

The #206 materializing prefix path has the identical defect one level up: execute_external_prefix_concurrently (pipeline_exec.rs:2023 at base) declines prefix_len < 2 (so a one-external prefix falls to the sequential drag) and when it does run it collects the last prefix member to completion via spawn_wait_concurrent_external_stages → results.last().cloned(). yes | cat | true therefore drains cat's output in a drain thread before true ever starts.

GNU anchor

execute_cmd.c:2620 execute_pipeline forks every element on real pipe(2)s. The rightmost element's exit closes its read end; the writer dies of SIGPIPE. yes | true → PIPESTATUS=(141 0), rc 0. (Same absence as #345, from the opposite end: #345 is a shell-side writer; this is an external producer with a shell-side reader.)

Fix

execute_external_prefix_streaming_tail (new, src/executor/pipeline_exec.rs, Windows) runs the plain-external prefix on real OS pipes and hands the last pipe's read end to the in-shell tail as fd 0, via a new pipeline_stdin_stream executor field that mirrors #345's pipeline_stdout_stream:

  • The tail's stage dispatcher leaves FUNCTION_STDIN absent for a streaming tail, so a present-but-empty mirror cannot short-circuit read back to EOF (read_io.rs:361-363); the tail's readers (read, a compound body's read/mapfile) pull the live pipe through the fd table (read_file_bytes blocks on the handle).
  • When the tail stops reading (it exits, reads one record, or breaks), restoring fd 0 drops the FileFd → close_handle → the producers' next write is a broken pipe.
  • Each lingering producer is reaped through wait_for_windows_pipeline_member with the SIGPIPE-shaped 141 (lingerer_sigpipe_status).
  • Admission is a whitelist (plain stages; the tail a literal true/:/false/echo/printf/read, or a compound body). Any shape this path cannot faithfully stream keeps the existing executor. Nested pipelines clear the flag (execute_simple_pipeline wrapper), so a body's inner pipelines are unaffected.

No sleeps, retries, timers, or CFG hints.

Before / after (this box, debug binary, timeout-bounded)

pipeline before after GNU 5.3.0
yes | true rc=124 @5.2 s, RSS ≥1 GB rc=0 @0.1 s, PS=141,0 rc=0 @0.2 s
yes | : rc=124 @5.2 s rc=0 @0.1 s PS=141,0
yes | read -r l rc=124 @5.2 s rc=0 @0.1 s PS=141,0
yes | cat | true rc=124 @5.2 s rc=0 @0.1 s PS=141,141,0*
seq 1 100000000 | true rc=124 @8.2 s rc=0 @0.2 s PS=141,0
yes | while read; do break; done rc=124 @5.2 s rc=0 @0.1 s PS=141,0
yes | head -1 (negative control) rc=0 @0.1 s rc=0 @0.1 s y

Peak-RSS sampling of the pre-fix shell for yes | true: 522 MB → 1034 MB in ~3 s while yes.exe stayed at 7.4 MB — confirming the consumer, not the producer, was growing.

* The intermediate cat status 0 vs GNU 141 is the pre-existing Windows intermediate-SIGPIPE gap (identical for the untouched all-external path: yes | cat | head -1 → PS=141,0,0). Not introduced here; PIPESTATUS[0] is 141 as required.

Tests

  • New tests/issue761_pipeline_unbounded_producer.rs: deadline-guarded (20 s), sandboxed CWD, resolves real externals — covers true/:/false/read/echo/printf tails, yes | cat | true, the compound while … break and brace-group tails, the PIPESTATUS=141,0 contract, the seq 1 1e8 | true long case, and the yes | head -1 negative control. Verified to fail (timeout) on pre-fix master and pass after.
  • Green: issue345_shell_writer_sigpipe, issue382_hardkilled_lingerer_sigpipe_status, issue158_pipeline_drain_deadlock, issue429_pipeline_stdin_redirect_regressions, --lib (689), plus a ~70-file pipeline/read/compound sweep.

The two sweep failures (issue680_mapfile_encoded_split_regressions::mapfile_nul_delim_stays_on_decode_path, issue647_function_redirect_stdin_regressions::function_call_file_redirect_survives_external_spawn) are pre-existing Windows path-quoting test bugs: both fail identically on clean origin/master (confirmed by stashing this change).

Relation to #723

#723 could not reproduce because its 13 shapes all used a bounded or external LAST stage. The missing shape is exactly this one — an unbounded external producer with an in-shell last stage (| true, | :, | read, | while … break). This PR reproduces it deterministically and fixes it at the pipe-ownership layer, which is the root cause #723 was hunting. /cc #723.

… pipeline tail (#761)

`yes | true` (and `yes | :`, `yes | read -r l`, `yes | cat | true`) never
returned: the shell drained the producer into memory (~250 MB/s) instead of
closing the read end and letting it die of SIGPIPE. Reproduces on HEAD; the
same absence is the untested root of #723's `producer | in-shell-tail` freeze
(its 190-run negative matrix never used an inline-arm / builtin LAST stage).

Root cause (interleaving): the rightmost element was never started until the
sequential stage loop had materialized every upstream element's whole stdout
(`execute_external_pipeline_stage_inner` -> `child.wait_with_output`,
pipeline_stages.rs). An unbounded producer never reaches EOF, so the tail's
read end was never closed and the producer never observed a termination — the
shell, not the producer, was the side that grew. The materializing
`execute_external_prefix_concurrently` path had the identical defect one level
up: `spawn_wait_concurrent_external_stages` drains the last prefix member to
completion before the tail starts, so `yes | cat | true` still never
terminated.

Fix: `execute_external_prefix_streaming_tail` (pipeline_exec.rs) runs the
plain-external prefix on real OS pipes and hands the LAST pipe's read end to
the in-shell tail as fd 0 (`pipeline_stdin_stream`, mirroring #345's
`pipeline_stdout_stream`). The tail's readers (`read`, a compound body's
`read`/`mapfile`) pull the live pipe through the fd table (read_io.rs
read_file_bytes blocks on the handle), because the stage dispatcher leaves
FUNCTION_STDIN absent for a streaming tail (pipeline_exec.rs /
pipeline_stages.rs, `execute_builtin_pipeline_stage` and
`execute_compound_pipeline_stage`). When the tail stops reading — it exits, or
reads one record, or `break`s — restoring fd 0 closes the read end to the
producers, and each lingering member is reaped through
`wait_for_windows_pipeline_member` with the SIGPIPE-shaped 141. Admission is a
whitelist (plain stages; the tail a literal `true`/`:`/`false`/`echo`/
`printf`/`read` or a compound body), so any shape this path cannot faithfully
stream keeps the existing executor.

GNU anchor: execute_cmd.c:2620 execute_pipeline forks every element on
pipe(2); the rightmost element's exit closes its read end and the writer dies
of SIGPIPE (`yes | true` -> PIPESTATUS 141 0).

Measured (debug, this box), bounded by `timeout`:
- `yes | true`      before rc=124 @5.2 s; after rc=0 @0.1 s, PIPESTATUS 141,0
- `yes | :`         before rc=124 @5.2 s; after rc=0 @0.1 s
- `yes | read -r l` before rc=124 @5.2 s; after rc=0 @0.1 s
- `yes | cat | true` before rc=124 @5.2 s; after rc=0 @0.1 s
- `seq 1 100000000 | true` before rc=124 @8.2 s; after rc=0 @0.2 s
- `yes | head -1`   unchanged rc=0 @0.1 s (negative control)

Tests: tests/issue761_pipeline_unbounded_producer.rs (deadline-guarded,
sandboxed CWD). Existing issue345_shell_writer_sigpipe,
issue382_hardkilled_lingerer_sigpipe_status, issue158_pipeline_drain_deadlock,
issue429_pipeline_stdin_redirect_regressions and `--lib` stay green.
@caomengxuan666
caomengxuan666 merged commit 3490b12 into master Oct 17, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/761-pipeline-unbounded-producer branch October 18, 2026 09:29
caomengxuan666 added a commit that referenced this pull request Oct 20, 2026
The four reproductions were committed #[ignore]d while the defect was live;
PR #784 fixed it and they now pass in ~0.7 s (they previously hit the 10 s
bound), so remove the attributes and let them enforce the behaviour.

Also promote lim-f5-fd-cap to must-match: the 2026-10-20 catalogue run
(arm 62a30ab) reported PROMOTE for it, i.e. the fd table no longer caps
at 1014.
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.

pipeline: unbounded external producer + shell-side tail never returns — yes | true drains into RAM at ~250 MB/s (hangs on HEAD too)

1 participant