Skip to content

WIP: spawn-time fd state for procsub consumers and coproc (Refs #2195, #2196, #2200) - #2232

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/fd-family-2195-2196-2200
Oct 30, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/fd-family-2195-2196-2200

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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 by process_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

  1. [bug] output procsub 消费者 spawn 的 fd 状态未按重定向序列建模:2>&1 压 carrier 丢载荷且多生消费者、同 fd 多重定向继承位置错 #2195 — materialization re-keyed to the ordered list (positional rewrite, dup-mirror aliasing), per-consumer spawn plan computed from the redirect prefix (file captures, dup results incl. closed targets, earlier carriers, Closed), consumed at spawn (fds 1/2 as stdio, fds ≥ 3 via the __RUBASH_FD_* transport).
  2. [bug] 带号 fd 的 output procsub 消费者输出全路径缺失:cat 3> >(wc -c) 只剩命令自身输出 #2196 — list pass materializes unserved output entries (numbered/{var} fds) with their own carriers and plans.
  3. [bug] coproc construct-level fd redirect (3>file) is dropped at the spawn boundary: body writes fail 'Bad file descriptor' #2200 — coproc construct-level redirects bind fds ≥ 3 (file input/output/append, read-write, close, dup incl. pipe sources and {word} ambiguity) and transport them into the coproc child on both the respawn and direct routes.

Evidence

  • 16 pins (8/4/4 per issue) with GNU 5.3.20 expectations, in 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.
  • fd-9 rule: pins were executed by running the test binaries directly, outside the build slot (the slot's flock leaks fd 9 into test children); probe scripts were run the same way against script files.
  • Script-file probes for every issue shape were re-run against the fixed binary and match the oracle, including payload-carrying numbered procsubs (fd 3 and fd 7), the rebind shape, coproc input redirect, and the 3>&1 pipe-source dup.

Cluster REPORT with the full mechanism map, boundary list, and probe ledger lands in the workdir (not in this PR).

caomengxuan666 and others added 4 commits October 30, 2026 14:13
…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.
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