Skip to content

WIP: Fix stage-0 fd-alias pipelines draining the aliased descriptor (#2003) - #2028

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2003-procsub-pipe-stage0
Oct 29, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2003-procsub-pipe-stage0

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #2003

Defect

A pipeline element 0 whose fd 0 is an fd alias (<&N, < /dev/fd/N) lost its entire payload whenever a stage funnel consumed stdin through the descriptor channel: exec 3<five.txt; cat <&3 | wc -l printed 0, and x=$(cat < <(seq 5) | wc -l) captured 0 (GNU bash 5.3.20: 5 for both). The reported procsub shape is one member of the family — a plain file fd reproduces it, so the defect is the stage-0 fd-alias resolution, not procsub streaming (#1952).

Root cause

GNU execute_pipeline (execute_cmd.c:2620) forks every element with the shell's own descriptors; the element applies its own redirections inside the fork (execute_cmd.c:4617 execute_simple_command → redir.c do_redirection_internal), so the element's reads ARE the aliased descriptor's reads, exactly once. Rubash's stage-0 payload resolution (stdin_string_for_command_mut) instead served the alias by draining the shared descriptor to EOF, and the descriptor-channel funnels (inline cat re-resolution, compound fd-0 bind, function-stage clones, external spawner handle materialization) then read EOF.

Fix (mechanism-level)

  • initial_pipeline_input materializes the aliased fd's endpoint after resolving the payload: buffered sources get their pre-resolution offset restored; handle-backed (File/coproc) sources are replaced by an in-memory endpoint holding the resolved bytes. Descriptor consumers read the payload through the fd; consumption stays shared.
  • Compound/function stage funnels bind the stage clone's fd 0 to the aliased descriptor (dup shares the open file description) and drop the FUNCTION_STDIN mirror for alias stages — arming both channels served the same bytes twice to a stage-0 read loop.
  • The function funnel undoes the external-spawn snapshot in command_with_process_substitution_files for the alias redirect (temp-path rewrite + source consumption): a function element is never spawned; its body resolves the alias itself.

Verification (Linux, vs GNU bash 5.3.20 oracle)

  • 34-shape matrix (procsub/file/procsub-pipe sources × cat/grep/head/wc/read-loop/function/group/subshell funnels, comsub and direct forms): 34/34 byte-identical to GNU.
  • 10 new regression pins (tests/regression.rs, mod issue_2003), expectations cross-checked against the oracle: all pass.
  • cargo test --lib: failing-name set identical to same-tree baseline (only the known ownership_unary_operators_check_existing_file).
  • cargo test --test regression: failing-name set identical to baseline (7 environment-class failures); the 10 new pins pass.
  • diffcorpus: 306/306 PASS.
  • -D warnings clean for the lib and regression targets; per-file rustfmt applied; CR=0.

Known boundary (not this issue)

Partial consumption by a payload-arm stage does not fold back into the materialized endpoint: exec 3<f; head -1 <&3 | wc -l; cat <&3 prints the full file afterwards where GNU prints lines 2–5 (pre-fix it printed nothing). Same fold-back family as the ambient FUNCTION_STDIN mechanism; left for its own home.

Draft — do not merge without review.

…h#2003)

GNU execute_pipeline (execute_cmd.c:2620) forks every element with
the shell's own descriptors, and the element applies its own
redirections inside the fork (execute_cmd.c:4617
execute_simple_command -> redir.c do_redirection_internal), so an
element's reads ARE the aliased descriptor's reads, exactly once.

Rubash's stage-0 payload resolution (stdin_string_for_command_mut)
served an fd-alias winner (<&N, < /dev/fd/N) by draining the shared
descriptor to EOF, while several stage funnels consume the element's
stdin through the descriptor channel itself - the inline cat arm
re-resolving the redirect, a compound stage binding fd 0 to the
endpoint, function stages on fd-table-sharing clones, the external
spawner materializing the endpoint by handle. Every one of them read
EOF from the descriptor the resolution had just drained:
exec 3<f; cat <&3 | wc -l printed 0, and x=$(cat < <(seq 5) | wc -l)
captured 0 (GNU: 5 for both). Plain file fds hit the same drain, so
the defect family is the stage-0 fd-alias resolution, not procsub.

Fix, by mechanism:
- initial_pipeline_input materializes the aliased fd's endpoint
  after resolving the payload: a buffered source's pre-resolution
  offset is restored, a handle-backed (File/coproc) source is
  replaced by an in-memory endpoint holding the resolved bytes, so
  descriptor-channel consumers read the payload through the fd and
  consumption stays shared.
- The compound and function stage funnels bind the stage clone's
  fd 0 to the aliased descriptor (dup shares the open file
  description) and drop the FUNCTION_STDIN mirror for alias stages,
  so the body sees exactly one stdin channel; arming both served the
  same bytes twice to a stage-0 read loop.
- The function funnel undoes the external-spawn snapshot inside
  command_with_process_substitution_files for the alias redirect
  (temp-path rewrite + source consumption): a function element is
  never spawned, its body resolves the alias itself.

Ten regression pins in tests/regression.rs (mod issue_2003),
expectations verified against GNU bash 5.3.20.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 29, 2026 02:19
@caomengxuan666
caomengxuan666 merged commit 5ec322c into master Oct 29, 2026
10 of 11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2003-procsub-pipe-stage0 branch October 30, 2026 01:41
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.

Process substitution output vanishes when the consumer is itself piped: cat < <(seq 5) | wc -l prints 0 (GNU: 5)

1 participant