Skip to content

WIP: fix pipeline-stage output procsub delivery (rubash#2184) - #2194

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2184-pipestage-procsub-output
Oct 30, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2184-pipestage-procsub-output

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2184

Defect

echo hi | cat > >(wc -c) printed 0 deterministically (GNU bash 5.3.20: 3). An output process substitution in redirect position on ANY pipeline stage delivered zero bytes at any payload size, while the same redirect on a simple command worked.

Localization

  • Materialization (command_with_process_substitution_files) did run for stages: the empty carrier was created, the target rewritten, and finish_process_substitutions did spawn the consumer — which read the still-empty carrier (side-effect probe: the consumer body ran).
  • Stage runners capture the stage's streams as strings; placement is deferred to the driver loop's routing walk — which re-resolves the original stage node. The raw target text >(wc -c) resolved to a literal path: the payload was written into a file literally named >(wc -c) in the cwd (found on disk after the probe run), or the open failed ENOENT when the body contained /, producing a spurious diagnostic.
  • The consumer also spawns (inside execute_pipeline_stage) before the driver routes, so no driver-side fix could be in time.
  • All concurrent/streaming fast paths decline procsub-carrying members, so the sequential wrapper is the only surface.

GNU shape: subst.c:6362 process_substitute() creates the pipe at expansion; execute_cmd.c:2620 execute_pipeline() runs each element with its own redirections applied — no capture-and-relocate step exists.

Fix (src/executor/pipeline_exec.rs only)

In execute_pipeline_stage's materialization branch: if a redirect target was rewritten into a carrier (new helper stage_redirect_rewritten_to_carrier compares the original vs materialized node), route the captured streams through the materialized node's redirect state with the driver's own ordered walk (route_pipeline_stage_streams) before the consumer spawns, and mark the stage pipeline_stage_fds_pre_wired so the driver skips its original-node walk. The routing population is exactly the driver's: stages that place their own streams (function stages, compound stages, the external shared-file arm) are skipped — routing them would truncate their carrier with their empty captured strings.

Input procsub on stages was probed as the boundary: it already worked (sources complete at materialization) and is pinned as a guard.

Verification

  • New pins tests/issue2184_pipeline_stage_procsub_regressions.rs: 16/16 (last/middle/first stage, append, builtin/function/compound stages, extra redirects, word+redirect on one stage, 2>&1-before, stderr face, 108890-byte payload, input boundary, no-literal-file, consumer-file, external stderr).
  • [bug] procsub 消费者输出在退出期竞态丢失:钉 output_process_substitution_preserves_printf_raw_bytes 间歇空输出(与 #2174 flush 无关) #2179 surface: its test file 5/5, its two victim pins in cli_tests green, 30-rep spot run of both victim shapes 0/30 failures each.
  • Neighborhood: issue1184_f4 7/7, issue1220 2/2, issue1221 3/3, issue1336 5/5, issue1460 1/1, issue1523 6/6, issue1659 5/5, issue1690 1/1, issue1691 3/3, issue2053 4/4, issue2057 2/2, cli_tests procsub subset 11/11, full lib suite (see REPORT).
  • 22-case probe matrix vs GNU 5.3.20: every in-scope shape matches; no literal >(wc -c) file is created anymore.

Recorded, deliberately NOT fixed here (pre-existing, reproduce identically on simple commands — details in the cluster REPORT)

  • > >(X) 2>&1 (dup ONTO a carrier) loses the payload on every path; reverse order works and is pinned.
  • Same-fd multi-redirect (> >(X) > file, > file > >(X)): GNU's consumer inherits the fd state as of its position in the redirect sequence; rubash's consumer spawn does not model that on any path.
  • Numbered-fd output procsub (3> >(wc -c)): consumer output missing on every path.
  • Word-position cat >(wc -c) as a stage argument errors on the /dev/fd word (different mechanism, fd-word lifetime).

No changes to src/executor/dev_fd_operands.rs or tests/regression.rs; no fifo protocol bytes touched.

…node so redirect-position procsub carriers receive the payload (rubash#2184)

A `>(...)` redirect on a pipeline stage delivered zero bytes
deterministically: materialization rewrote the target into an empty
temp carrier and the consumer spawned at stage end read it, but the
stage runners capture the stage's streams as strings and the driver
loop's routing walk re-resolved the ORIGINAL node's redirects —
writing the payload into a literal file named after the raw
`>(...)` text while the consumer read the empty carrier.

GNU has no capture-and-relocate step: subst.c:6362
process_substitute() creates the substitution pipe at expansion
time and execute_cmd.c:2620 execute_pipeline() runs each element
with its own redirections applied, so the element's redirected fd
is the pipe.

Fix: in execute_pipeline_stage, when materialization rewrote a
redirect target into a carrier, route the captured streams through
the MATERIALIZED node's redirect state (the driver's own ordered
walk) before finish_process_substitutions spawns the consumer, and
mark the stage pre-wired so the driver skips its original-node
walk. The routing population is exactly the driver's: function and
compound stages and the external shared-file arm place their own
streams and are left untouched.

Pins: tests/issue2184_pipeline_stage_procsub_regressions.rs (16) —
last/middle/first stage, append form, builtin/function/compound
stages, extra redirects, word+redirect procsubs on one stage,
dup-before, stderr face, >64KiB payload, input-procsub boundary
guards, no literal `>(...)` file, consumer-file delivery,
external-stage stderr delivery. Expectations verified against
GNU bash 5.3.20.

Refs #2184
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.

1 participant