Repository navigation
WIP: spawn-time fd state for procsub consumers and coproc (Refs #2195, #2196, #2200) - #2232
Merged
Merged
Conversation
…sequence Refs #2195 Materialization of >(…) consumers is re-keyed from the parser's dedicated fd 0-2 mirror fields to the ordered redirect list: each materialization records its list position, served entries are rewritten positionally (identical body texts on different entries no longer cross-wire to one carrier), and a 2>&1 whose target the parser mirrored into redirect_err_append as 2>> <same target> no longer spawns a phantom second consumer — the mirror aliases the real carrier and the list's own dup binds fd 2 to it. Each redirect-derived consumer now carries a spawn plan computed from the redirect entries preceding its own: GNU forks the consumer mid-do_redirections (redir.c:237; subst.c:6362), so it inherits the fd table as those entries left it — file redirects (captured plain-path descriptions, truncate captures seeked to end at spawn to approximate the shared description), dup results (including closed targets), carriers of earlier substitutions, Closed — instead of the shell's ambient stdio. Fds >= 3 ride the existing __RUBASH_FD_* env transport; fds 1/2 become spawn stdio. Pins: tests/issue2195_procsub_spawn_fd_state_regressions.rs (8 pins, GNU 5.3.20 expectations, run outside the build slot).
Refs #2196 The parser's dedicated redirect mirrors carry only fds 0-2 (nodes.rs), so a numbered-fd substitution (cat 3> >(wc -c)) lives only in the ordered redirect list and no field arm ever materialized it — the consumer never spawned. A list pass after the field arms now materializes every unserved output entry (Output/Append/ClobberOutput with a recorded >(…) span) keyed by list position, with its own carrier and a #2195 spawn plan, so each entry gets exactly one consumer as GNU forks one per entry. Combined kinds are excluded (field-served by construction); exec-kept substitutions stay with the exec path. Pins: tests/issue2196_numbered_fd_procsub_regressions.rs (4 pins: spawn, payload on fd 3, payload on fd 7, plain-then- procsub rebind — all red on master, GNU 5.3.20 expectations).
Refs #2200 apply_coproc_redirects created the target file for a construct- level redirect and then dropped every fd outside {1,2}: output/ append arms matched only fds 1/2, input and read-write kinds fell to the catch-all, and dup arms skipped numbered targets — so the coproc body ran without its fd 3 (3: Bad file descriptor) while GNU applies the construct's full redirect list in the child (execute_cmd.c:2477 execute_coproc; do_redirections at :4244). Numbered bindings are now collected during the walk (file read/write/read-write, closed, dup — including pipe sources such as 3>&1 bound to the stdout pipe and the {word} ambiguous- redirect diagnostic GNU gives) and transported after the walk: inheritable dups ride __RUBASH_FD_HANDLE_/WHANDLE_ + PATH/WPATH env pairs into respawned children, and are pinned pre-exec on the direct route (pin_ambient_fds_in_child, now pub(in crate::executor)). Dups are created only after the walk, so a declined redirect leaks none; the caller closes them on every exit path past creation. Pins: tests/issue2200_coproc_numbered_fd_regressions.rs (4 pins: output file, input file, 3>&1 pipe dup, async control — fix pins red on master, GNU 5.3.20 expectations).
Refs #2196 The #2196 list pass admitted every unserved Output/Append/ ClobberOutput entry with a recorded >(…) span — including {var} entries. Those are not unserved: apply_dynamic_fd_var_redirects runs on the original node before materialization and binds the fd to a persistent process-substitution endpoint whose body runs at close (GNU redir_varassign, redir.c:1133-1166; the substitution itself is expansion-time, subst.c:6362 process_substitute). Materializing the entry again spawned a second consumer fed by an empty carrier; the two consumers raced on the body's output and the stream was lost whenever the empty one wrote last — issue_1646's varredir-close and default-mode drain pins lost the whole stream (goldens run 41070494271). Fix by category: the list pass now skips fd_var entries, leaving them to their owner. Numbered-fd entries (the #2196 class) are unaffected. Verification: issue_1646 module 4/4 (both pins red at branch head and at e31b529 in bisection; 9571736 green), the fd cluster's 16 pins still green, both pin scripts byte-identical to the GNU expectations under the fixed binary.
caomengxuan666
marked this pull request as ready for review
October 30, 2026 14:52
This was referenced Oct 30, 2026
Closed
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.
WIP: spawn-time fd-state family — one mechanism investigation, three commits.
Refs #2195
Refs #2196
Refs #2200
Mechanism
GNU decides a child's fd table where the child is forked:
do_redirections(redir.c:237) walks the ordered redirect list, expanding each entry's word at its own turn (redir.c:298); a>(...)consumer is forked byprocess_substitute(subst.c:6362) at that moment and inherits the table as the preceding entries left it; a coproc child applies its construct-level redirects of any fd in the child (execute_cmd.c:2477, :4244).rubash instead (a) materialized procsub consumers from the parser's dedicated fd 0–2 mirror fields — fabricating a phantom second consumer for the
2>&1-after-procsub mirror, never materializing list-only entries (numbered/{var} fds), and cross-wiring identical body texts; (b) spawned deferred consumers with ambient stdio; (c) dropped coproc construct redirects for fds ≥ 3 at spawn.Commits
Closed), consumed at spawn (fds 1/2 as stdio, fds ≥ 3 via the__RUBASH_FD_*transport).{word}ambiguity) and transport them into the coproc child on both the respawn and direct routes.Evidence
tests/issue2195_*.rs,tests/issue2196_*.rs,tests/issue2200_*.rs. Baseline on master: 13 red / 3 green (the 3 green are the two control pins + the reverse-order pin already correct on master). Fixed build: 16/16 green.3>&1pipe-source dup.Cluster REPORT with the full mechanism map, boundary list, and probe ledger lands in the workdir (not in this PR).