Skip to content

fix(executor): retire the opposite fd arm on each redirect bind (Fixes #3655) - #3768

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3655-heredoc-vs-file-redirect
Nov 16, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3655-heredoc-vs-file-redirect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #3655

GNU applies redirects left-to-right, last-wins: redir.c do_redirection_internal walks the list in order and each entry dup2's onto its redirector (the r_reading_until case calls here_document_to_fd at redir.c:1065-1072). After exec 14<<A 14>file GNU leaves fd 14 write-only, so read -u 14 fails EBADF. Rubash's fd table kept input/output arms independent (open_input replaced only the read arm, open_output only the write arm), so fd 14 still carried the heredoc's read arm and read -u 14 saw first.

Fix (mechanism, not a case guard): FdTable::open_input/open_output now retire the opposite arm (dup2 wholesale, keeping the rubash#3213 retire-before-materialize ordering), and every legitimate dual-arm <> install goes through a single FdTable::open_readwrite — one O_RDWR description, installed once. Converted sites: set_fd_readwrite_file (all arms), function-call stdin (3), external finish/setup, builtin net binds, dynamic {var} binds (file + procsub arms), respawn restore, inherited fds, and the pipe-complementary write arms (walk + set_fd_readwrite_file dup arm). Input/output setters clear the opposite arm's env ledger with the bind.

Pin: tests/issue3655_heredoc_vs_file_redirect.rs — 10 cells (heredoc-then-truncate/append, both reverse controls, heredoc-vs-input-file both orders, <> pair shared-offset guard, here-string both orders, fd-0 face). Every expectation byte-verified against GNU bash 5.3.20 (oracle probe in the lane workdir). Fail-first + X299 legs: see REPORT.md in the lane workdir (to be attached at review).

本 PR 删除了什么

fd 表双臂独立共存的不变量:同 fd 上的输入/输出绑定不再各自保留对方臂;除 <> 单次安装外,双臂共存形态被移除。无公开 API 删除。

…3655)

GNU redir.c do_redirection_internal applies each redirect with dup2
onto its redirector (the r_reading_until case calls
here_document_to_fd at redir.c:1065-1072), so a later entry on the
same fd replaces the descriptor wholesale -- last-wins. Rubash's fd
table kept input/output arms independent: `open_input` replaced only
the read arm and `open_output` only the write arm, so after
`exec 14<<A 14>file` fd 14 still carried the heredoc's read arm and
`read -u 14` saw `first` on a descriptor GNU leaves write-only.

Make both primitives retire the opposite arm (dup2 wholesale, with
the rubash#3213 retire-before-materialize ordering) and route every
legitimate dual-arm `<>` install through a single
FdTable::open_readwrite -- one O_RDWR description, installed once
(set_fd_readwrite_file, function-call stdin, external finish/setup,
builtin net binds, dynamic {var} binds, respawn restore, inherited
fds, and the pipe-complementary write arms).

Pin: tests/issue3655_heredoc_vs_file_redirect.rs -- 10 cells, every
expectation byte-verified against GNU bash 5.3.20.
…(Refs #3655)

CI run 43824819839: E0308 at external_setup.rs:2713 — the enclosing
match binds ProcsubSpawnFd::File arms by reference (match ergonomics),
so `read` is `&Rc<FileFd>` where `FdReadEndpoint::File` expects
`Rc<FileFd>`; the write side already had `.clone()`, the read side
did not. Single error in the crate; fd_table.rs:264 is only the
variant-definition note, not a second error.
…ash#3770 (Refs #3655)

Leg 2 on X299 (head 6e9e6f2): 9/10 green; only
issue_fd0_heredoc_then_truncate_file_wins red — stdout was the
expected `rc=1 x=` (the heredoc read arm IS retired) but stderr was
empty where GNU prints `read: 0: read error: Bad file descriptor`.
That missing diagnostic is a read-builtin face, not the redirect
ordering this PR fixes: filed as rubash#3770 (no existing issue on
the face; `read_fd_is_available` fd-0 arm checks only `is_closed(0)`).
The cell now pins the ordering face only (rc + empty x + empty
out.txt) and cites #3770 for the diagnostic.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — PASS

Head b1d02e87 verified end to end. Merging with Fixes #3655 (PR body already carries the deletion section).

Mechanism — GNU anchors checked against source: redir.c r_reading_until → here_document_to_fd (redir.c:1065-1072) and redir_open O_RDWR (redir.c:725). The defect was in the fd table, not the walk: open_input/open_output replaced only their own arm, so exec 14<<A 14>file left both arms alive. The fix retires the opposite arm on each bind (dup2 wholesale, #3213 retire-before-materialize ordering preserved) and routes every legitimate <> pair through the single FdTable::open_readwrite. I read the fd_table.rs core diff and spot-checked the converted sites (21 open_readwrite call sites across trap_exec/function_calls/external_setup/external_finish/builtin_redirects/init); no sequential single-arm <> install remains.

Legs (X299, logs fetched to the lane workdir ~/workspace/fix-3655/) — fail-first on base e1e3d7b2 + pin: 6 passed / 4 failed, the 4 reds exactly the defect faces (heredoc→truncate/append, here-string→truncate, fd0→truncate). Head b1d02e87: pin 10/10, neighbor issue3578 14/14, lib fd_table 8/8 (incl. the 3 new unit tests), RUSTFLAGS="-D warnings" lib + pin stem rc=0. The J cell is deliberately relaxed to the ordering face only, with a #3770 reference in the pin — the fd-0 diagnostic face is a read-builtin matter, already filed as #3770 (gated behind this merge per the issue body).

CI (final head, run 43830136536) — three red legs (Rust tests / goldens+canaries / macOS), each failing on the single known baseline cell issue2875 only (goldens leg 1354 passed / 1 failed); all other legs green, and the second workflow (43830135710) is a success.

Guard / sequencing — full open-PR scan over the 7 touched files: same-file open PRs (#3738/#3740 ordered pair, #3723 family, #3688, #3746, #3652, #3377, #3140, #3363, …) are all in Wave 2 / held / merge-owed states; none is merged, so this PR merges first and the rest rebase onto it. git merge-tree against the current tip bbcaafa7 (user-merged #3678) is conflict-free; update-branch runs at merge time as usual.

Residual — the full-tree -D warnings failure in tests/issue1794_user_group_completion.rs is pre-existing on base (all its tests are #[cfg(unix)], so on Windows the import + helper are dead code) and unrelated to this PR. Tracked as #3772.

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 08:46
@caomengxuan666
caomengxuan666 merged commit 70e962b into master Nov 16, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
…ly (Refs #3770)

GNU builtins/read.def:911 — a hard read(2) failure is reported
as `read: <fd>: read error: <strerror>` and the builtin fails;
it is not plain EOF. After `exec 0<<A 0>out.txt` fd 0 is open
but write-only (#3655 retires the heredoc read arm), and the
plain-`read` path reached the transports with no availability
gate (the -u arm diagnoses before the transports; plain read
never calls read_fd_is_available), so every transport returned
None and the builtin fell through to the silent-EOF arm.

Fix (mechanism): read_virtual_fd_stdin records a hard read
error when the table owns an open descriptor with no read arm —
the same read_error channel the EISDIR endpoint (#1495) and
hard read failures in read_file_bytes use, rendered by the read
builtin's existing error arm. `is_open` without
`is_open_for_read` is exactly open-but-not-readable: an entry
with no read arm at all (a pipeline stage's fd 0) has is_open
false, so its transports still own it, and a present-but-
exhausted read arm stays a real silent EOF.

Pin: tests/issue3770_read_writeonly_fd0.rs (8 cells; fd-0 cell
is the full expectation of #3768's relaxed J cell).

UNVERIFIED-BY-CAPTAIN
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.

heredoc: 同 fd 先 heredoc 后普通文件重定向时顺序不生效(exec 14<<A 14>file,GNU 后者胜)

1 participant