Skip to content

fix(read): serve fd-0 self-dups through the ordinary fd-0 transports - #975

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/938
Oct 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/938

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

  • Fix rubash#938 — the surviving empty-read face of the read builtin hangs forever on /dev/stdin #700/read builtin: empty from /dev/fd/N, and rc=0 at EOF under compound/fd redirection (while read off-by-one) #701 fd family: read x </dev/stdin, read x <&0 (plus /dev/fd/0, /proc/self/fd/0, the <&0- move form, and -u 0) inside a pipeline group stage saw EOF where fd 0 held the pipe data.
  • Root cause: GNU redir.c:1128 do_redirection_internal runs a descriptor dup's whole action — the dup2 and the r_move_input close — only when redir_fd != redirector, so every fd-0 self-dup is a complete no-op; -u 0 likewise names fd 0 itself (read.def:294). Rubash served those reads from the fd-table-only descriptor path, which has no endpoint for a pipeline stage's FUNCTION_STDIN mirror and returned EOF.
  • read_input_for_command now lets -u 0 and any fd-0 self-dup redirect fall through to the ordinary fd-0 transports (fd-table endpoints → FUNCTION_STDIN mirror → inherited process stdin), matching a bare read exactly.
  • read_fd_is_available gates the move-close check on redirector != source (the close is inside the same redir.c:1128 guard) and treats fd 0 as a valid descriptor unless the table recorded an explicit close — the F_GETFD rule sh_validfd applies (read.def:377-381).
  • read_timeout_zero_status polls fd 0 for -u 0 -t 0 like the no--u arm.
  • scripts/regression-catalog: issue-700 promoted diverges → must-match (the read builtin reads empty via </dev/stdin / <&0 in pipeline group context (residual of the #700/#701 fd family) #938 residual face now matches); new tests/issue938_read_fd0_selfdup.rs pins the family as a script file (the -c shape binds the inherited-stdin transport and masks the bug).

Test plan

  • Ticket repro byte-identical to the stated GNU output (devstdin=[SIN], dup0=[SIN], plain=[SIN], gotf=[FILE], empty stderr, rc 0)
  • Old-vs-new binary differential on regression-catalog issue-699..704/714/723/726: byte-identical except the intended issue-700 flip (pipe=[SIN], dup0=[DUP])
  • cargo test: issue700/701 (fd family pinned, no regress), issue275/276/277/279/280/714 read family, fd0 cursor transport, issue429/436/615/586/591 stdin transports, issue324/325/330/353/355/359/362/368/370/385/392/438/508/514/518/525/535/575/699/773/804/817/826/844/846/861 redirect/procsub family, parser_redirection_tests — all green
  • Edge probes match GNU: while read <&0 / while read </dev/stdin iterate the pipe; sequential <&0 then bare reads share the cursor; { read <&0; } < file reads the bound file; -u0 -t2 reads with rc 0; empty pipe under a self-dup still fails rc 1 with the empty record assigned
  • 1 environmental failure noted: issue647::function_call_file_redirect_survives_external_spawn embeds a backslashed winuxcmd true.exe path unquoted in the generated script — any POSIX shell strips the backslashes; identical failure reproduces on base master binary, unrelated to this change
  • CI

Fixes #938

Generated with Devin

…938)

Residual face of the #700/#701 fd family: `read x </dev/stdin`,
`read x <&0` (and the `/dev/fd/0`, `/proc/self/fd/0`, `<&0-`, `-u 0`
forms) inside a pipeline group stage saw EOF where the pipe held data.

GNU redir.c:1128 do_redirection_internal gates a descriptor dup's whole
action — the dup2 AND the r_move_input close — on
`redir_fd != redirector`, so every fd-0 self-dup is a complete no-op
that leaves fd 0 bound exactly as it was; `-u 0` likewise names fd 0
itself (read.def:294). Rubash instead served those reads from the
fd-table-only descriptor path, which has no endpoint for a stage's
FUNCTION_STDIN mirror and returned None (EOF, rc 1, empty variable).

- read_io.rs read_input_for_command: `read -u 0` falls through to the
  ordinary fd-0 read chain; an input redirect on fd 0 resolving to
  descriptor 0 (any `<&0`-family word or /dev-stdin alias) is skipped
  wholesale as the GNU no-op it is.
- read_builtin.rs read_fd_is_available: the move-close check only fires
  when the redirector differs from the source (the close lives inside
  the same redir.c:1128 guard); fd 0 is a valid read descriptor whenever
  the table recorded no explicit close — the F_GETFD rule sh_validfd
  applies (read.def:377-381), matching validate_builtin_dup_source's
  stdio-always-open rule.
- read_builtin.rs read_timeout_zero_status: `-u 0 -t 0` polls fd 0 like
  the no-`-u` arm (read.def:539-562 select() on the one descriptor).

Verified: ticket repro byte-identical to the stated GNU output
(devstdin/dup0/plain=SIN, gotf=FILE, empty stderr, rc 0); regression-
catalog issue-700 output flips diverges->must-match (manifest promoted);
old-vs-new binary differential on issue-699..704/714/723/726 is
byte-identical apart from the intended issue-700 fix. Targeted suites
green: issue700/701/275/276/277/279/280/714 read family, fd0 cursor,
429/436/615/586/591/647 stdin transports, redirect/parser suites
(1 environmental failure in issue647 — the test embeds a backslashed
winuxcmd true.exe path unquoted; identical failure on base master).
EOF discipline preserved: empty pipe under a self-dup still returns
rc 1 with the empty record assigned.

Fixes #938

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit 020d9fe into master Oct 21, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/938 branch October 30, 2026 01:41
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.

read builtin reads empty via </dev/stdin / <&0 in pipeline group context (residual of the #700/#701 fd family)

1 participant