Skip to content

fix(executor): revoke delivered fd N on command-local N>&- / N<&- (Refs #3560) - #3858

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3560-numbered-close-revoke
Nov 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3560-numbered-close-revoke

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Fixes #3560

Summary

cmd N>&- / cmd N<&- (N >= 3) did not revoke fd N for the child: collect_external_fd_delivery stamps every ambient fd>=3 into __RUBASH_FD_* env keys (and unix pins) before the redirect walk, and the walk's close arm only continued — the stamps survived and the child still saw the fd (echo c >&3 wrote into the parent's file).

GNU services the close with r_close_this inside the ordered do_redirections walk (redir.c:237; close_buffered_fd at redir.c:1239) in the forked child — child-scoped, parent's binding untouched.

Fix: route the numbered close through apply_pending_close_at — the same revocation a {var} pending close uses — removing env stamps, pins, retiring carriers, clearing walk bindings, and snapshotting the table step into fd_var_close_restore so undo_child_fd_var_redirects restores the parent binding at command end. Last-wins ordering falls out of the walk's list order (3>f 3>&- closes; 3>&- 3>g rebinds; 4>&3 after 3>&- is a bad-fd redirect error).

Dependency

Stacked on #3857 (the __RUBASH_FD_* non-respawn strip fix, Refs #3856): on master the env channel is dead, so this pin is only observable once delivery works. This branch contains that commit; after #3857 merges the diff shrinks to the close fix + pin.

Evidence

  • GNU bash 5.3.20 (/usr/local/bin/bash, script-file probes target/scratch-i3/gnu-3560*.sh): A rc=1 B rc=1 C rc=0 amb bytes: 2, exec-close rc=1, own-then-close rc=1 f=[], close-then-own rc=0 g=[y], close-dup rc=1 + 3: Bad file descriptor, parent survives.
  • Pin tests/issue3560_numbered_fd_close_delivery.rs: 5/5 green here; 4/5 red with only the product fix stashed (leak shapes A rc=0, amb bytes: 6, exec-close rc=0, in-close rc=0).
  • tests/issue3311_exec_child_fd_delivery 4/4; fd-var close stems (issue2281/2272/2280/1313) 34/34; cargo test --lib fd_ 15/15.

Test plan

  • cargo build --locked --bin rubash
  • pin self-run 5/5 + fail-first 4/5 red
  • neighboring fd-delivery/close stems green
  • cargo test --lib full diff + -D warnings + unix leg (flagged for captain; env channel is shared so unix rubash children had the same leak)

…awn strip (Refs #3856)

0a405d8 (#3736) added __RUBASH_FD_ to fork_respawn_transport_key, so
Executor::new_inner deleted every fd-delivery stamp for any birth that
is not a validated internal respawn — before init.rs could collect them
into inherited_fd_handles. exec/external/comsub/pipeline children are
never respawns, so all fd>=3 delivery through the env channel died on
Windows (unix masks it via pin+adoption). The keys are per-spawn
delivery minted fresh for THIS child and consumed+stripped at entry —
not respawn residue — so they do not belong to the #3049 family.

Verified: tests/issue3311_exec_child_fd_delivery 4/4 red->green;
exec-own/exec-amb/plain-own fd>=3 writes reach children again.
#3560)

The external fd delivery stamped every ambient fd>=3 into the child's
__RUBASH_FD_* env keys (and unix pins) before the redirect walk, and
the walk's close arm only skipped the entry — so `cmd 3>&-` left the
ambient stamps in place and the child still saw fd 3. GNU services
the close with r_close_this inside the ordered do_redirections walk
(redir.c:237; close_buffered_fd at redir.c:1239) in the forked child.

Route the numbered close through apply_pending_close_at — the same
revocation a {var} pending close uses: env stamps removed, pins
dropped, carriers retired to the dead list, walk bindings cleared,
and the table step snapshots into fd_var_close_restore so
undo_child_fd_var_redirects restores the parent's binding at command
end (the close is child-scoped, never a parent exec N>&-).

Depends on the __RUBASH_FD_* delivery-strip fix (#3856): on master
the env channel is dead anyway, so this pin is only observable once
delivery works. Verified: pin 5/5 green here, 4/5 red without this
commit; GNU bash 5.3.20 script probes match byte-for-byte.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 15:47
@caomengxuan666
caomengxuan666 merged commit 3272325 into master Nov 17, 2026
10 of 12 checks passed
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.

[bug] Windows: a command's own N>&- does not revoke the ambient fd N handed to a rubash child

1 participant