Skip to content

WIP: fix(fd): stage-0 delivery on the resolved descriptor + born-closed fd 0 seed (#2502, #2503) - #2515

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2502-2503-fd-delivery
Nov 1, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2502-2503-fd-delivery

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: cluster H — fd delivery residuals from the #2476/#2479 harvest (PR #2497). Both in the 1.5.0 gate.

Refs #2502
Refs #2503

#2502 — pipeline stage-0 stdin lands on the resolved descriptor

GNU forks each pipeline element with the shell's descriptors (execute_cmd.c:2620); the element applies its own redirections in the fork (execute_cmd.c:4617, redir.c:237 ordered walk). A duplication shares the open file description (redir.c:1138/1193); a /dev/fd/N spelling is a plain open of the magic link (redir.c:725) that re-reads from offset 0.

  • cat 3<&6 <&3 | wc -c printed 0: snapshot/materialize/fold keyed on the spelling fd 3 (no table entry) while consumers read fd 6. initial_pipeline_input now plans delivery on the resolved terminal descriptor (in_list_binding AmbientOf verdict); a dup OF an alias-open keeps the [fd] 编号绑定 3< /dev/fd/6 后 <&3 第二条命令读到空(fresh open 游标被首命令消费,#2256 存量红钉) #2479 spelling-fd delivery.
  • cat < /dev/fd/6 | wc -c printed 12 then 0 in one process: a spelled winner is a fresh open in GNU and never drains the descriptor. The snapshot rewrite and stage-0 materialization now skip spelled winners whose source reopens fresh; a virtualized copy of a fresh-reopening file keeps its origin path (FdReadEndpoint::Text origin) so later spellings reopen the file, while dup siblings of one ambient open share the single copy.
  • The stage spawner never cleared own_input_bindings (the 列表级输入 procsub 3< <(...) 未物化(#2196 输入侧兄弟面) #2235 discipline the plain path applies), so one command's drained temp carrier answered the next command's <&N walk; cleared per command on the stage path.

Pins: tests/issue2502_pipeline_alias_delivery_regressions.rs — 6/6 scrubbed, GNU 5.3.20 expectations.

#2503 — born-closed fd 0 seeded; self-dup is a no-op

A dup whose source fd equals its destination never reaches GNU's dup2 — redir.c:1151 gates the whole application on redir_fd != redirector — so <&0 on a closed fd 0 is not a redirection failure: the command runs against the closed descriptor and reports it at read time (read.def:911; coreutils cat's read + closing-standard-input lines). A dup from a DIFFERENT closed fd still fails the redirection.

Pins: tests/issue2503_born_closed_stdin_regressions.rs — 4/4 scrubbed; pin scripts also run as GNU twins for byte comparison.

Verification (all scrubbed, scrubbed_cargo.py, CARGO_INCREMENTAL=0)

issue2502 6/6 · issue2503 4/4 · issue2479 8/8 · issue2256 25/25 · issue2476 7/7 · issue2029 10/10 · cli fd pin 1/1 · cargo check clean.

Residuals (in the cluster REPORT, not filed): operand faces of origin copies (wc -c /dev/fd/6, stage operand serving the copy remainder — #2256 operand channel, not a redirect shape); chain-partial fold through a spawned stage (master: 0+empty, now 6+empty; full parity needs the external-stage consumption channel to report the consumer's true read — #2029 machinery); validate_builtin_dup_source's std-fd early return for non-self dups FROM a born-closed std fd (pre-existing, same shape as fd 1 since #2476).

…ubash#2502)

GNU forks each pipeline element with the shell's descriptors
(execute_cmd.c:2620) and the element applies its own redirections
in the fork (execute_cmd.c:4617, redir.c:237 ordered walk): a
duplication shares the open file description and advances the
shared offset (redir.c:1138/1193), and a /dev/fd/N spelling is a
plain open of the magic link (redir.c:725) that re-reads the file
from offset 0. rubash's stage-0 materialization instead landed on
the SPELLING's fd and destroyed the descriptor's file identity:

- cat 3<&6 <&3 | wc -c printed 0 (the snapshot/materialize/fold
  keyed on fd 3, which has no table entry, while consumers read
  fd 6's descriptor). initial_pipeline_input now plans the
  delivery on the fd the chain actually resolves to: when the
  fd-0 winner is a dup whose in-list binding verdict is an
  ambient descriptor (in_list_binding AmbientOf), the terminal
  descriptor is delivered and folded; a dup OF an alias-open
  keeps the #2479 delivery on the spelling fd.
- cat < /dev/fd/6 | wc -c printed 12 then 0 in one process: the
  first stage drained the descriptor and the fold left the copy
  at EOF, but a spelling winner is served by a fresh open in GNU
  and never drains the descriptor. The snapshot rewrite in
  command_with_process_substitution_files and the stage-0
  materialization now skip spelled winners whose source reopens
  fresh (fd_alias_reopen_path), and a virtualized copy of a
  fresh-reopening file remembers its origin path
  (FdReadEndpoint::Text origin_path) so later alias spellings of
  the same descriptor reopen the file, while dup siblings of one
  ambient open share the single copy (one open file description).
- The stage spawner never cleared own_input_bindings, so a
  drained temp carrier materialized for one command's list
  answered the next command's <&N walk (the #2235 discipline the
  plain path already applies); the stage path clears it per
  command.

Regression pins: tests/issue2502_pipeline_alias_delivery_regressions.rs
(6 pins, GNU bash 5.3.20 expectations). Guards scrubbed green:
issue2479 8/8, issue2256 25/25, issue2476 7/7, issue2029 10/10,
cli fd pin 1/1.
…#2503)

A process born with fd 0 closed (or a session after exec 0<&-)
read the descriptor as a silent EOF: read x <&0 failed rc 1 with
no diagnostic and the in-process cat exited 0 silently. GNU
reports the descriptor: read prints 'read: 0: read error: Bad
file descriptor' (read.def:911) and coreutils cat prints its
read + closing-standard-input EBADF lines, both rc 1.

Key GNU fact: a dup whose source fd equals its destination is a
total no-op — redir.c:1151 gates the whole dup application,
including the dup2 at redir.c:1193, on redir_fd != redirector —
so <&0 on a closed fd 0 is not a redirection failure; the
command runs against the closed descriptor. A dup from a
DIFFERENT closed fd still fails the redirection (dup2 EBADF).

- FdTable::new now seeds fd 0 from the at-exec capture #2476
  built (std_fd_closed_at_exec), beside the fd 1 seed; the
  closed-entry machinery (read ladder, stdin consumers) then
  produces the GNU faces, as it already did for an in-session
  exec 0<&-.
- The read dispatch skips its redirection-failure gate when the
  winning redirect is the self-dup, so the read-time diagnostic
  fires; non-self dups of closed fds keep the failure.
- apply_external_stdin_redirect hands a self-dup child the
  closed state (closed_stdin) instead of a redirection error.
- The emulated cat classifies a dup winner whose source is
  closed in the table as closed stdin, so cat <&0 reports both
  coreutils lines like bare cat does.

Regression pins: tests/issue2503_born_closed_stdin_regressions.rs
(4 pins, GNU bash 5.3.20 expectations incl. GNU-twin runs of the
pin scripts). Guards scrubbed green: issue2476 7/7, issue2502
6/6, issue2479 8/8, issue2256 25/25, cli fd pin 1/1.
@caomengxuan666
caomengxuan666 force-pushed the fix/2502-2503-fd-delivery branch from 533e093 to f744f52 Compare November 1, 2026 22:24
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2515 PASS on the #2502 face; #2503 face unix-effective only (documented boundary) — issue2502_pipeline_alias_delivery_regressions 6/6 x2 @--test-threads=4 on Windows; live probe (script file, seed=12 bytes) byte-identical to WSL GNU Bash 5.3.20 on all three shapes: cat 3<&6 <&3 | wc -c → 12, cat < /dev/fd/6 | wc -c → 12 and → 12 again in the same process (probes D:/repo/win-accept/wave2/probes/p2502.*).

issue2503_born_closed_stdin_regressions is #![cfg(unix)] (0 tests on Windows): the born-closed fd 0 seed reads std_fd_closed_at_exec(0), and src/fd/windows_impl.rs:1381 documents Windows as always "not closed at exec". Live wrapper probe (exec 0<&- parent, child script): rubash cat <&0 rc=0 silent (EOF) vs GNU rc=1 + cat: closing standard input: Bad file descriptor; read face rc matches (1) but the diagnostic is absent — pre-existing Windows platform boundary, NOT a regression of this PR (probes p2503*, w2503*).

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

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