Skip to content

fix(session,script_driver): gate die_by_signal and tail exec replacement on process ownership (Refs #4211) - #4246

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4211-isolate-library-process-exit
Nov 27, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4211-isolate-library-process-exit

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #4211 (item 4: isolate library-crate die_by_signal / std::process::exit from embedded Session hosts).

GNU C Reference & Embedding Contract

  • third_party/bash/sig.c:649-667 (kill_shell): a standalone shell process dies by restoring SIG_DFL and re-raising the terminating signal so waitpid reports signal death.
  • In rubash, standalone CLI invocations (src/main.rs) opt into process-level ownership via builtins::exec::enable_process_replacement(). Embedded library hosts (Session inside niubash, editors, test harnesses) never opt in (src/builtins/exec.rs:93-111), and fork-copy executors (!owns_process_image) share a host process that must survive them.

Root Cause

  1. FinishPass::run_stream_end_duties (src/script_driver.rs:9501-9504) unconditionally called die_by_signal(sig) (libc::raise(sig) + std::process::exit(128 + sig)) whenever termsig_death was Some(sig). Because Session::close / Session::close_with_report / Session::begin_finish all run FinishPass, an embedded Session finishing with a pending termsig_death terminated the entire host process instead of returning 128 + sig.
  2. Executor::exec_replace_tail_ready (src/executor/external_inner.rs:2839) relied only indirectly on the -c final-frame stamp rather than explicitly enforcing process_replacement_enabled() && self.owns_process_image before Command::exec / fallback std::process::exit.

Summary of Changes

  • src/script_driver.rs:
    • Gated die_by_signal(sig) in FinishPass::run_stream_end_duties on crate::builtins::exec::process_replacement_enabled() && self.executor.owns_process_image().
    • When gated off (embedded Session or fork-copy executor), sets self.resolved_status = 128 + sig and returns normally so the host observes 128 + sig without process termination.
    • Added unit test finish_pass_process_ownership_tests::embedded_finish_reports_termsig_status_without_process_death.
  • src/executor/public_accessors.rs:
    • Added pub(crate) fn owns_process_image(&self) -> bool.
  • src/executor/external_inner.rs:
    • Added explicit if !crate::builtins::exec::process_replacement_enabled() || !self.owns_process_image { return false; } guard at the entry of exec_replace_tail_ready.
  • tests/session_api.rs:
    • Added #[cfg(unix)] regression test embedded_session_termsig_close_does_not_kill_host_process inside existing tests/session_api.rs (no new top-level tests/issue*.rs binary per #4210): re-executes the test binary as a child, injects SIGTERM into an embedded Session, verifies the EXIT trap runs, close_with_report().status == 143, and the host process survives cleanly.

What this PR deletes

  • Deletes the unconditional die_by_signal(sig) call from embedded/non-process-owner FinishPass runs.

Verification

  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked — passed
    • cargo test --test session_api — 21/21 passed (including embedded_session_termsig_close_does_not_kill_host_process)
    • cargo test --lib script_driver — 21/21 passed
    • cargo test --test issue2346_termsig_death_regressions — 11/11 passed (standalone CLI kill_shell signal death preserved)
  • Windows:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked — passed
    • cargo test --lib script_driver — 20/20 passed
  • Hygiene: scripts/check-api-ledger.py, scripts/check-marker-literals.py, and pre-commit scripts/check-rustfmt.py all passed.

@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY(只读复核):达合入门(只读,待运行时)。双门正对 #4211 第 4 项的嵌入 Session 被 die_by_signal 拉崩宿主根因,两枚新钉 fail-first 形态且 CI 内绿;census 11 名零 PR-only 新名(issue1902_stdout_stderr 系分叉基点早于 #4245 合入的旧基线名),goldens 为 #4038 面。待补运行时:devin-mac 实跑新钉 + 宿主崩溃形复现对照,过后终裁。

@caomengxuan666

Copy link
Copy Markdown
Member Author

CAPTAIN-VERIFY(devin-mac 运行时复验):达合入门,队长合入。两枚新钉实跑绿——集成钉即嵌入宿主端到端形:SIGTERM 注入后宿主存活、status=143;fail-first 成立(中和双门→测试进程被 SIGTERM 自杀、还原复绿)。issue2346 首跑 9/11 后连跑 3 次 11/11,定谳时序抖动非回归;session_api 唯一红为先存的 macOS /private 路径规范形断言,非本 PR 账。

@caomengxuan666
caomengxuan666 merged commit 17837ac into master Nov 27, 2026
11 of 13 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.

1 participant