Repository navigation
WIP: Fix stage-0 fd-alias pipelines draining the aliased descriptor (#2003) - #2028
Merged
Merged
Conversation
…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.
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.
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 -lprinted0, andx=$(cat < <(seq 5) | wc -l)captured0(GNU bash 5.3.20:5for 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:4617execute_simple_command→ redir.cdo_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 (inlinecatre-resolution, compound fd-0 bind, function-stage clones, external spawner handle materialization) then read EOF.Fix (mechanism-level)
initial_pipeline_inputmaterializes 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.FUNCTION_STDINmirror for alias stages — arming both channels served the same bytes twice to a stage-0 read loop.command_with_process_substitution_filesfor 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)
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 knownownership_unary_operators_check_existing_file).cargo test --test regression: failing-name set identical to baseline (7 environment-class failures); the 10 new pins pass.-D warningsclean 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 <&3prints the full file afterwards where GNU prints lines 2–5 (pre-fix it printed nothing). Same fold-back family as the ambientFUNCTION_STDINmechanism; left for its own home.Draft — do not merge without review.