Skip to content

fix(executor): fork-boundary fd snapshots keep live descriptor handles - #3849

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3814-subshell-fd-close-isolation
Nov 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3814-subshell-fd-close-isolation

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #3814

Summary

  • GNU's subshell / command substitution are real forks (execute_cmd.c:1576 execute_in_subshell; subst.c:7143 command_substitute): the child owns copies of the parent's descriptors, so ( : ) 8<&- closes only the child's fd 8 — the parent's stays readable (P5min rc=0 l2=[a]).
  • Rubash emulates the boundary with fd_table.clone(), sharing every Rc<FileFd>; an in-region close_input retires the kernel descriptor the parent's restored entry still names → parent read fails EBADF.
  • Adds FdTable::saved_table() — the whole-table form of the existing per-slot saved_entry/dup_file_endpoints — and uses it at every snapshot/restore fork boundary (subshell, comsub, scoped-redirect, function-call, trap, exec-scoped saves).

GNU anchors

  • execute_cmd.c:1576 execute_in_subshell — forked child gets its own descriptor table.
  • subst.c:7143 command_substitute — same fork boundary for $( ).
  • redir.c save_fd/restore_fd/undo_partial_redirects — descriptor save/restore discipline the scope snapshots mirror.

Verification (WSL, GNU bash 5.3.20 script-file oracle)

  • ( : ) 8<&- + parent read <&8 → P5min rc=0 l2=[a] identical; file-backed fd, nested subshell, $( ) comsub, and double-close variants all byte-identical.
  • New pin: tests/issue3814_subshell_close_fd_isolation.rs (5 tests, cfg(unix)).
  • Fail-first: reproduced on base a41909d in script mode (read: 0: read error: Bad file descriptor, P5min rc=1 l2=[]).

Generated with Devin

…s (Refs #3814)

GNU's subshell and command substitution are real forks
(execute_cmd.c:1576 execute_in_subshell; subst.c:7143
command_substitute): the child owns copies of the parent's
descriptors, so `( : ) 8<&-` closes only the child's fd 8 and the
parent's stays readable. Rubash emulates the boundary with
`fd_table.clone()`, which shares every `Rc<FileFd>` — an in-region
close_input retires the kernel descriptor the parent's restored
entry still names, and the follow-up read fails EBADF.

Add FdTable::saved_table() — the whole-table form of the existing
per-slot saved_entry — duplicating each kernel-backed endpoint via
dup_file_endpoints (dup(2): shared open file description, region-
local closed flag), and use it at every snapshot/restore fork
boundary: subshell, command substitution, and the scoped-redirect /
function-call / trap / exec-scoped saves that emulate the same
boundary.

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

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

g3 lane: WSL Linux-native verified. Pin stem issue3814_subshell_close_fd_isolation 5/5 pass; GNU 5.3.20 script-file comparison byte-identical (subshell/comsub/nested/file-fd/double-close); -D warnings clean; rustfmt applied. Fail-first reproduced on base a41909d (script mode: P5min rc=1 l2=[] EBADF).

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.

子 shell 的 8<&- 泄漏到父 shell:( … ) 8<&- 后父 shell fd 8 损坏

1 participant