Skip to content

WIP: fix #2522 capture-path child stdio loss - #2542

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2522-capture-stdio
Nov 2, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2522-capture-stdio

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2522

#2522)

GNU applies a command's redirect list as one ordered walk
(do_redirections, redir.c:237); a {var} close lands on the
descriptor the variable's value names (do_redirection_internal,
case r_close_this, redir.c:1239; the REDIR_VARASSIGN arm resolves
redirector = redir_varvalue(redirect), redir.c:1244).

The two stdio probes behind the external spawn's closed-fd branch
read a {var} entry's STATIC redirector instead: Redirect.fd is
None for a {var} entry (the name lives in fd_var), and both probes
defaulted a missing fd to 1 — external_output_fds_closed via
map_or(true, |fd| fd <= 2), external_final_closed_stdio via
unwrap_or(1). Every {var}>&- (e.g. {v}>&- with v=9) was therefore
misread as "fd 1 closed", and since the stdio planner declines
every list containing an fd_var entry, all such lists landed in
the closed-stdio branch: fd 1 inherited + pre_exec-closed in the
child, fd 2 piped for parent drain. The foreign child's fd-1
writes failed EBADF inside the child, its report lines vanished,
and its exit status flipped to 1, while the numbered binds
(fd 9/10) were delivered correctly, so target files stayed
byte-correct. In $() the same misread emptied the whole capture.
This is the residual face of #2368/#2322.

Fix the invariant in src/executor/external_redirects.rs: a {var}
redirect has no static redirector.
- external_output_fds_closed: the static scan skips fd_var
  entries; a new varfd_close_hits_stdio judges them by the
  descriptor the variable names — the dynamic-fd applier's
  recorded closes for this command (fd_var_pending_closes,
  generation-guarded, the same records the redirect gate folds),
  or, where the spawn delivery already consumed the records (the
  post-wait routing caller), dynamic_fd_variable_value, the
  resolver the applier itself uses.
- external_final_closed_stdio: the static walk skips fd_var
  entries (a {var} bind allocates a fresh descriptor, never
  fd 1/2) and folds the recorded {var} output closes into the
  state at their list positions (fold_varfd_output_closes),
  with a final drain for a trailing close.

Numbered/defaulted closes (>&-, 2>&-, >&N-) keep their exact
prior behavior; the #2378 closed-stdio branch is untouched for
them.
#2522)

GNU resolves a dup's source at the dup's own position in the
ordered walk (redir.c:237 do_redirections). The legacy stdio
arms resolve dup sources against the AMBIENT fd table only, so
for `cmd 9>o1.txt 2>&9` the child's fd 2 dup'd whatever fd 9
meant in the parent (an `exec 9>seed` binding) instead of the
list's own `9>o1.txt` — the stdio planner declines exactly
these spellings to "the legacy route, whose walks resolve the
binding positionally" (rubash#2329), and the walk that does
resolve positionally is the post-wait routing walk.

New predicate dup_source_bound_by_own_list (the planner's own
in_list_binding test at the dup entry's position) extends the
existing piped-drain conditions in the fd 1/2 arms: when the
dup source is bound by the command's own list, the stream is
piped and the drained bytes are routed through the ordered
walk after the wait. Self-dups stay a no-op (rubash#1236).

This also restores the issue2368 errdup pin's delivery now
that the {var} close misattribution no longer routes that
list through the closed-stdio branch by accident: pre-fix it
passed only via the capture route; with live stdio it must
take the drain for its `2>&9`, and now does.
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2542 PASS on the pinned faces, with one new residual found by the fd/procsub combo sweep (posted on #2522 — not fixed here per wave rules).

  • Issue-repro A/B vs WSL GNU 5.3.20, script files, stdout+stderr+rc byte-identical on both faces: (a) headline exec 9>seedout.txt; v=9; CHILD reader.sh 9>o1.txt {w}>&9 {v}>&- → w10=OK/w9=FAIL/rc=0 w=[]/o1=[W10] including GNU's reader.sh: line 2: 9: Bad file descriptor on stderr; (b) minimal external-child face "$CHILD" -c 'echo hi' {v}>&- → hi/rc=0/x=[hi], stderr empty. Pre-fix signature (rc=1, lost fd-1 output, extra EBADF) gone. Probes: D:/repo/win-accept/wave3/probes/p2522{a,b}.*.
  • issue2322_close_position_regressions 13/13 ×2. issue2368_foreign_spawn_post_wait_trunc_regressions is #![cfg(unix)] — 0 tests on Windows by design (foreign child is /usr/bin/bash); the same shapes ran green through the direct A/B above.

Combo-sweep residual (procsub body): cat < <(CHILD reader.sh 9>o1.txt {w}>&9 {v}>&-) — GNU delivers the reader's fd 1 (w10=OK, got4=[]) and the {w}>&9-assigned fd 10 (o1=[W10]); rubash loses the external child's fd 1 output entirely and fails echo W10 >&10 with 10: Bad file descriptor → o1=[]. Deterministic ×2. The command-substitution route of the identical shape is byte-identical (x=[w10=OK/got4=[]] o1=[W10]), so the residual is specifically the < <(...) producer path's external-child stdio/varfd delivery. Probes: pc1b/pc1c in the same directory. No BLOCKER to this merge — the merged fix's own faces are green; the residual is filed for the fd family.

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 2ce27e6 (contains this merge), oracle wsl /usr/local/bin/bash 5.3.20.

caomengxuan666 added a commit that referenced this pull request Nov 2, 2026
Wave-3 residual of #2522: inside a `<( ... )` body, an external child
carrying a `{var}` redirection list lost its fd 1 output entirely and
the `{w}>&9` varfd descriptor was never delivered (EBADF) —
`cat < <(CHILD reader.sh 9>o1.txt {w}>&9 {v}>&-)` printed
`rc=0 o1=[]` where GNU prints `w10=OK` / `got4=[]` / `rc=0 o1=[W10]`.
The identical list was already correct at top level (#2542) and through
`$()` — only the procsub producer path was broken.

Root cause: the procsub body is re-printed by joining tokens with
spaces (process_substitution_source, parser/process_substitution.rs).
`hostname 9>o6.txt {w}>&9` became `hostname 9> o6.txt {w} >& 9`, and
the producer respawn (`rubash -c`, the port of subst.c:6362
process_substitute's forked child) re-parsed that re-print: `{w}` read
as a literal argument and `>&9` as a plain fd-1 dup — wiring the
child's stdout onto the `9>` file (fd-1 crossing, verified with a
foreign child) — while `{v}>&-` re-printed `{v} >& -` closed the
child's fd 1, losing its output entirely.

GNU owners: the body handed to the forked producer is verbatim
(subst.c:1311 extract_process_subst → subst.c:6362 process_substitute
→ parse_and_execute), and the one spacing-semantic boundary is the
REDIR_WORD adjacency — parse.y:5826-5837 read_token_word returns
REDIR_WORD only when the '<'/'>' immediately follows the '{name}' word.
GNU's own printer re-prints every REDIR_VARASSIGN spelling glued
(print_cmd.c:1237-1297: `{%s}>&%d` at :1244, `{%s}>&%s` at :1260,
`{%s}>&-` at :1297). Operator→target spacing is re-parse-neutral in
both shells (`>& -` is still r_close_this; oracle-verified).

Fix: the re-join reuses the parser's own column-adjacency predicate
(redirect_fd_var_prefix, parser/redirections.rs:488 — the same test
the grammar keys on) to keep that boundary glued; a deliberately
spaced `{w} >&9` keeps its original spacing and stays WORD + fd-1 dup
in both shells (oracle-verified). No second copy of the rule, no
character-class heuristics.

Verified against WSL GNU Bash 5.3.20 (script files): the wave-3 probe
family (pc1/pc1b/pc1c, p2522a/b, p2521/p2524/p2527/p2531/p2534/p2535)
is byte-identical on stdout and stderr. New pins:
tests/issue2522_procsub_varfd_reprint_regressions.rs (7 tests).
Neighbors green: issue2322 13/13, issue2363-PR suites (2335/2336/2341/
2343), issue1313 5/5; issue2368 is unix-gated. cargo test --lib delta
0 (2 pre-existing env-bound failures reproduce on pristine
origin/master). Full procsub-family sweep (92 binaries): failure set
identical to pristine baseline modulo two intermittently flaky
binaries (issue325 read race, issue1933), each observed flaking on
BOTH builds.

#2526 is NOT fixed here (different subsystem: producer wait/spawn
timing, not body fidelity) — evidence captured under
D:/repo/nbbuild/evidence/procsub-residual/ for the battery owners:
with the marker rendezvous active the abandoned-producer boundary is
already at GNU speed on this host; the 10s shape is the #2473-gated
DrainToTemp fallback, and the bare-`wait` face (w1.sh) is a separate
membership bug in the #2179 family.

Refs #2522
@caomengxuan666
caomengxuan666 deleted the fix/2522-capture-stdio branch November 3, 2026 09:06
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