Skip to content

fix(fd): duplicate symbolic stdio endpoints in exec-walk undo saves (Fixes #4024) - #4032

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/4024-walk-save-stdio-dup-fd
Nov 21, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/4024-walk-save-stdio-dup-fd

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Root cause

GNU's undo save is a real descriptor (redir.c:1296 add_undo_redirect() runs fcntl(fd, F_DUPFD, SHELL_FD_BASE) before the redirect mutates the source), so the save survives the redirector's own close. Rubash's exec walk cloned the symbolic Stdout/Stderr/InheritedProcessStdin endpoint for the save; when >&- retired the real descriptor (kernel_close_persistent), a later 1>&10 in the same list bound a dead endpoint and subsequent output silently vanished.

GNU C citations (third_party/bash/redir.c, execute_cmd.c):

  • redir.c:1296 (add_undo_redirect), redir.c:86 (SHELL_FD_BASE = 10)
  • redir.c:790,1138-1271 (do_redirection_internal), redir.c:230-267 (do_redirections)
  • execute_cmd.c:5606-5634 (RX_UNDOABLE / exec undo-list disposal)

Fix

  • src/executor/fd_table.rs: add FdTable::exec_walk_saved_entry to upgrade symbolic stdio endpoints (Stdout, Stderr, InheritedProcessStdin) to private descriptor duplicates (dup_adopted_fd on Unix / duplicate_handle on Windows) when they name the real process descriptor.
  • src/executor/trap_exec.rs: use exec_walk_saved_entry at the exec-walk save site and add stdout_marker_names_process so active capture routing is preserved.
  • Add tests/issue4024_exec_walk_save_stdio_source.rs (7 tests).

Verification

  • Verified against WSL /usr/local/bin/bash 5.3.20.
  • rustfmt --edition 2021 --check clean on all 3 files.
  • cargo test --test issue4024_exec_walk_save_stdio_source: 7/7 pass on Windows and Linux.

Captain graft + verification

This PR was adopted as the merge vehicle for #4024 after captain review
found its mechanism sound and its capture routing more careful than the
captain lane's own (retired) reconstruction. The graft commit on top
(59b4f358) adds verification, the missing pin coverage, and one
dup-failure handling fix (§3). The PR's own product change is otherwise
unchanged.

1. Oracle matrix (captain's cloud GNU bash 5.3.20)

The captain lane's probe set for #4024 (script files; stdout + stderr +
rc byte-compared) run against this PR's tree, pristine c885c5a5, and
GNU bash 5.3.20:

  • p01–p11 (incl. both p08 stdin forms): this tree matches GNU on all
    11
    ; pristine diverges on exactly the 9 known silent-loss shapes,
    and the p05/p07 guards agree on all three columns.
  • p13 (stderr capture sibling): matches GNU (captured value empty,
    payload delivered to the script's stderr).
  • p12 (stdout capture discriminator): differs — decomposed in §4,
    residual A. This tree is byte-identical to pristine there; the PR
    neither fixes nor regresses that shape.
  • pfail pair (fd-exhaustion): differs — decomposed in §3/§4.

2. Grafted pins

tests/issue4024_exec_walk_save_stdio_graft.rs (8 pins) ports the
captain lane's probe shapes the PR's own pin file did not cover: p02
(two saves claiming slots in order), p03 (dup to a numbered fd), p04
(multi-line service), p08 file-form stdin continuation reads, p06
(save-slot disposal with diagnostic text), p11 (two-stdio full
diagnostic text), and the p05/p07 guards.

  • On this tree: 8/8 green; the PR's own pins stay 7/7.
  • On pristine: 6 red / 2 green — all six fix-face pins fail
    (including p02), both guards pass: fail-first verified.

3. Dup-failure handling (the graft's one product change)

GNU errors when the undo save's F_DUPFD fails (oracle pfail:
redirection error: cannot duplicate fd: Too many open files). The
review flagged this PR's silent fallback to the symbolic clone as its
one design flaw. The graft removes it, strictly inside
FdTable::exec_walk_saved_entry: the stdio_dup closure now returns
io::Result, all three .or_else silent fallbacks are deleted in
favor of ? propagation, and the doc comment is corrected. Method
signature and the trap_exec.rs call site are unchanged.

Verified non-regressing: after the change the full matrix above is
unchanged (p01–p11 and p13 still byte-match GNU; own pins 7/7; graft
pins 8/8).

One honest limit: the pfail shape itself still does not emit GNU's
cannot duplicate fd diagnostic in rubash. strace shows that when the
fd table is saturated, rubash's save is never materialized through a
kernel dup at all (no F_DUPFD is attempted for that walk), so there is
no failure at this site to propagate; the shape still fails loudly
(rc=1, echo: write error: Bad file descriptor). Reproducing the
EMFILE diagnostic belongs to the save-slot allocation layer, not to
this PR — recorded for the captain as residual B.

4. Residual A: nested exec walks leave the parent's fd 1 dead

The p12 difference and the pfail shapes' missing final line share one
root, proven pre-existing (pristine byte-identical) and independent of
this PR's mechanism — no dup-back and no save content are involved:

  • ( exec >&- 1>&10 ); echo after — GNU prints after; rubash
    prints nothing.
  • x=$(exec >&-; echo hi); echo "cap=[$x]" — GNU prints cap=[]
    (with the write-error diagnostic on stderr, which rubash matches);
    rubash loses the outer echo as well.

An exec redirect walk that closes fd 1 inside a subshell or command
substitution leaves the parent shell's fd 1 dead in rubash. That is
a cross-context walk-disposal defect adjacent to — but distinct from —
#4024's save-endpoint form; it is recorded for the captain as a
separate residual and is not fixed here.

5. Adjacency (this tree, cloud)

issue3991 pins 15/15; issue2613 9/9; issue2615 13/13; issue3806
4/4; issue3731 11/11; issue3633 coproc_child green (dead_coproc
red, byte-identical result on pristine — baseline environmental);
lib fd_table stem 8/8.

What this PR deletes

The PR itself deletes nothing beyond the single replaced call line at
the exec-walk save site (saved_entry → exec_walk_saved_entry);
saved_entry itself and its other call sites (procsub snapshot, fd0
snapshot) are untouched. The captain graft additionally deletes the
three silent .or_else fallback arms inside exec_walk_saved_entry
(§3) and adds one pin file.

Two captain-graft changes on top of the PR's fix for #4024:

- fd_table.rs: inside FdTable::exec_walk_saved_entry only, the
  stdio_dup closure now returns io::Result and the three silent
  .or_else fallbacks to the symbolic clone are deleted in favor of ?
  propagation (method signature and the trap_exec.rs call site
  unchanged; doc comment corrected). GNU's undo save is the same
  fcntl and its failure fails the redirection; the silent fallback
  parked exactly the dead alias #4024 is about.
- tests/issue4024_exec_walk_save_stdio_graft.rs: 8 pins ported from
  the captain lane's probe set (p02/p03/p04/p08-file/p06/p11 plus
  p05/p07 guards), covering the faces the PR's own pin file did not.

Verified on the captain's cloud oracle (GNU bash 5.3.20): probe
matrix p01-p11 + p13 byte-identical to GNU; graft pins 8/8 on this
tree, 6 red / 2 guard-green on pristine (fail-first); own pins 7/7;
issue3991 15/15, 2613 9/9, 2615 13/13, 3806 4/4, 3731 11/11,
3633 coproc_child green (dead_coproc red is baseline-identical).
Full account in the PR body's Captain graft + verification section.
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