Skip to content

fix(exec,fd): gate persistent OS fd mutations on process_replacement_enabled to spare host fds (Fixes #4225) - #4231

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4225-issue1838-adopted-fd-close-sigabrt
Nov 27, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4225-issue1838-adopted-fd-close-sigabrt

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes #4225: eliminates the SIGABRT (IO Safety violation: owned file descriptor already closed) in issue1838_shell_close_of_adopted_fd_spares_host_original (the sole SIGABRT in the master CI pin census).

Root cause & GNU contract

GNU redir.c:1017/:1120/:1190/:1239/:1267 mutates kernel descriptors directly because a standalone Bash process IS the shell. When rubash runs embedded inside a host process (!crate::builtins::exec::process_replacement_enabled(), e.g., niubash or an in-process Rust test harness using Executor::new() / Session), the host owns the process's original OS descriptors (#1838).

Previously, execute_exec, execute_stdio_only_exec_redirect, and close_orphaned_process_fd in src/executor/trap_exec.rs armed kernel_place_persistent / kernel_close_persistent (and invoked libc::close) without checking process_replacement_enabled(). Running exec N>&- on an adopted host std::fs::File therefore called libc::close(N) on the host's owned descriptor, triggering Rust's IO Safety SIGABRT on File::drop.

Changes

  • src/executor/trap_exec.rs:
    • Added single ownership predicate Executor::owns_process_fd_mutations(&self) -> bool requiring process_replacement_enabled() && self.owns_process_image && self.emulated_child_depth.get() == 0 && self.shell_state.subshell_depth.get() <= self.bashpid_origin_depth && self.compound_redirect_scope_depth == 0.
    • Wired owns_process_fd_mutations() into execute_exec and execute_stdio_only_exec_redirect, keeping persistent exec placements and closes virtual in FdTable for embedded/non-process-owning executors while preserving standalone CLI real-fd semantics.
    • Added !crate::builtins::exec::process_replacement_enabled() to close_orphaned_process_fd.
  • tests/issue1838_executor_host_fd_ownership_regressions.rs:
    • Added 4 regression pins covering persistent redirect overwrite (exec N>file), move-close (exec M>&N-), Session-level exec N>&-, and embedded exec >&- sparing host stdio (no new top-level tests/issue*.rs binary).

What this PR deletes

  • Nothing deleted in production semantics or public API; replaces duplicated ad-hoc subshell/compound depth checks in execute_exec and execute_stdio_only_exec_redirect with the shared owns_process_fd_mutations() predicate.

Verification

  • WSL Linux:
    • RUSTFLAGS='-D warnings' cargo check --lib --locked — pass (0 warnings)
    • issue1838_executor_host_fd_ownership_regressions — 7/7 passed (zero SIGABRT)
    • issue3560_numbered_fd_close_delivery — 6/6 passed
    • issue4044_nested_exec_stdio_close_isolation — 9/9 passed
    • issue4131_builtin_redirect_handle_retire — 3/3 passed
    • session_api — 20/20 passed
    • Also verified green: issue3640 (9/9), issue3938 (7/7), issue4181 (12/12), issue1557 (14/14), issue3578 (14/14).
  • Windows & repo gates:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked — pass (0 warnings)
    • check-api-ledger.py (298/298), check-marker-literals.py, check-rustfmt.py (1864/1864) — all pass.

@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY(head c546eada)— 达合入门(只读),只差运行时。

门控三处 + 4 钉,正对 #4225(issue1838 fd 所有权面);CI 0 SIGABRT——基线唯一的 issue1838 SIGABRT 已消除。运行时复验(issue1838 7/7 + 邻域钉 + standalone dup2 确认 + #3633 oracle 比对)一过即合入。

@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY — 证据达合入门,但 master 已前进、现冲突,待作者 rebase。

运行时复验(devin-mac,head c546eada):issue1838 文件 7/7 全绿、零 SIGABRT;邻域 issue4044 9/9、issue4131 3/3、issue1557/3578 各 14/14;standalone 探针 6 例与云端 oracle 逐项一致,foreign /bin/sh 子进程经 fd3 写入实证 persistent redirect 仍是真 dup2;放宽面正题 coproc_child_exec_close_delivers_eof_while_alive 绿。本地其余红项均已定性为非本 PR 面(macOS 无 /proc 的观测钉、BSD wc 填充、/private/var 规范化、基线 coproc 面)。

但队长刚合入十张 PR、master 已前进,本 PR 现 mergeable=false。请 rebase 到当前 master;若 rebase 为机械搬运(diff 语义不变),本轮证据直接沿用,队长复核后即合入。

@caomengxuan666
caomengxuan666 force-pushed the fix/4225-issue1838-adopted-fd-close-sigabrt branch from c546ead to 9b332b4 Compare November 27, 2026 08:39
@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY(rebase 校验):新 head 9b332b44 与已验 head 的 diff 两文件逐行全等(钉文件仅迁目录),机械 rebase、运行时证据沿用(issue1838 7/7 绿、零 SIGABRT、邻域全绿、探针 6/6 合 oracle)。新 CI census 10 FAIL + 0 SIGABRT——基线唯一 SIGABRT(issue1838)全名缺席、消除保持,零 PR-only。队长合入。

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] issue1838: shell-level close of adopted fd aborts with IO Safety violation (SIGABRT)

1 participant