Skip to content

WIP: fix(exec): die by the fatal signal after the EXIT trap (#2346) - #2351

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2346-termsig-death-form
Oct 31, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2346-termsig-death-form

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

审计第一章 RC3(#2346)的修复:未 trap 的致命信号在 EXIT trap 跑完后应被信号杀死,rubash 此前只有 SIGHUP 对,TERM/INT/USR1 一律以 128+n 正常退出。

GNU 机制:sig.c:506 termsig_sighandler → sig.c:590 termsig_handler 跑 run_exit_trap() → sig.c:649-667 kill_shell(sig) 复位 SIG_DFL 并 re-raise。

根因与收敛(一提交 140d137):死亡裁决是按信号名分叉的——note_untrapped_hangup 只认 SIGHUP,其余被捕获但未 trap 的终止信号在三个邮箱 drain(边界分发、阻塞等待观察者、wait 内建)全部落到普通 128+sig 退出。现泛化为单一机制:标记 hangup_death_pending: bool → termsig_death_pending: Option<i32>;裁决 note_untrapped_termsig 覆盖整个被捕获终止集(EXIT trap 已武装 ∧ 信号在 sig.c:226-229 捕获集内——复用 kill.rs 的 exit_reroutes 单一所有者;或交互式 SIGHUP);交互式 SIGINT 永不武装(GNU 走专用 sigint handler 的 DISCARD)。finish_shell 漏斗以 die_by_signal(sig) 收尾;hangup 仍单独门控交互式 SIGHUP(sig.c:621-622);漏斗运行中新到的信号按 GNU 的二重信号短路语义取代旧标记。

连带两处(均经钉子逼出):

错钉更正:issue1331_1334 的 exit_trap_runs_on_untrapped_fatal_signal 原断言 Some(143)(#741 立论的形态),已被本单 waitpid 证据推翻,改为断言 trap 输出 + 被 SIGTERM 杀死,与同文件 SIGPIPE 钉同形。

本地门(主会话/CI 分工见簇报告):新钉 issue2346 11/11、lib 985/985、对照 issue2236 9/9、issue1331_1334 7/7、issue1853 6/6、issue1035_1036 9/9、issue1043_1054 6/6、issue1493 1/1、issue1990_1991 5/5、issue1997 2/2、issue2241 5/5、issue526 7/7 全绿;waitpid 探针全表与 GNU 逐格一致。全量回归面与 Windows/macOS 面归 CI + 收口方。

相邻观察(不在本单):EXIT trap 入口的 $? 在信号死亡时 rubash 读 128+sig、GNU 读信号前的末命令状态(HUP 路径同样存在),建议另行立案。

Refs #2346

…2346)

GNU termsig path: sig.c:506 termsig_sighandler -> sig.c:590
termsig_handler runs run_exit_trap() and then sig.c:649-667
kill_shell(sig) restores SIG_DFL and re-raises, so the process dies
BY the signal (waitpid: WIFSIGNALED). Rubash ran the EXIT trap (the
#1332 half) but then exited normally with 128+n for TERM, INT and
USR1; only SIGHUP took the kill_shell path, because the #2233 funnel
was built for the interactive-hangup face alone.

Root cause: the death verdict was per-signal. note_untrapped_hangup
accepted only SIGHUP, so every other caught-but-untrapped
terminating delivery fell through the three mailbox drains
(boundary dispatch, blocked-wait observer, wait builtin) to the
plain 128+sig exit classification.

Converge all of them onto the one mechanism, no per-signal cases:

- Executor::hangup_death_pending (bool) becomes
  termsig_death_pending: Option<i32>, the signal owed a kill_shell
  death. The public hangup accessors stay as the SIGHUP view of the
  same mark; finish_shell remains the sole consumer.
- note_untrapped_termsig (was note_untrapped_hangup) now accepts
  the whole caught terminating set: termsig_death_armed = an EXIT
  trap is armed and the signal is in the sig.c:226-229 catch set
  (kill.rs exit_reroutes, now pub(crate) and reused rather than
  duplicated), or an interactive SIGHUP (sig.c:315-316). SIGINT in
  an interactive shell is never armed: GNU routes it to the
  dedicated sigint handler whose verdict is DISCARD.
- finish_shell: the hangup stays gated on an interactive SIGHUP
  (sig.c:621-622); a mark recorded while the funnel itself ran
  supersedes (termsig_sighandler short-circuits a second signal
  through kill_shell with the new one); the funnel ends in
  die_by_signal(sig) (was die_by_sighup): SIG_DFL + unblock + raise.
- The boundary dispatch still records the adjudicated signal death
  on the verdict path (rubash#2236's in-process-child notice).
- external_finish: the termsig mark is an Executor transient
  outside the saved ShellState, so a contained in-process child's
  span now parks the parent's mark and drops the child's, exactly
  like the adjudicated-death record -- otherwise the child's
  simulated death leaked into the parent's finish and killed the
  hosting process (found by the issue2236 pins).

Wrong-pin correction: tests/issue1331_1334's
exit_trap_runs_on_untrapped_fatal_signal asserted the plain
Some(143) form #741 had enshrined; the #2346 waitpid evidence
(GNU signaled 15) overturns it, so the pin now asserts the trap
output plus death by SIGTERM, mirroring the SIGPIPE pin below it.

Pins: tests/issue2346_termsig_death_regressions.rs (11) assert the
wait-status form via ExitStatusExt::signal() -- TERM top level and
-c, INT, USR1, the HUP control, TERM during an external wait and
during the wait builtin, exit-in-EXIT-trap not overriding the
death, and the trapped/ignored/no-trap controls.

Refs #2346
…ath-form

# Conflicts:
#	src/builtins/kill.rs
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