Skip to content

fix(jobs): only a WIFSIGNALED status may use the kill delivery record (Refs #3802) - #3841

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3805-delivered-signal-guard
Nov 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3805-delivered-signal-guard

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3802(issue 保持 OPEN)

#3805 让 jobs 表用「本 shell 自己 kill 投递的信号」给 Windows 的 TerminateProcess(128+signal) 退出码命名(Killed/Terminated),但 decode 没有校验 reap 出来的 wait status:take_delivered_signal(pid) 只看 pid。于是在 unix 上,一个忽略/捕获了 SIGTERM 之后自己退出的 job 被按投递记录渲染成信号死亡——trap "" TERM; exit 5 打印 Terminated(GNU 是 Exit 5),trap "exit 143" TERM 打印 Terminated(GNU 是 Exit 143)。这是 #3805 合入后引入的 master 回归。

GNU 锚点(再核)

  • third_party/bash/jobs.c:1969-2015 printable_job_status():只有 WIFSIGNALED 分支走 j_strsignal (WTERMSIG (p->status));WIFEXITED 分支一律 Exit %d(es == 0 才是 Done)。所以「退出码等于 128+signal」不是 WIFSIGNALED 的证据:exit 143 是 WIFEXITED,GNU 打 Exit 143。
  • jobs.c:3988-4007 kill_pid:纯投递;投递 ≠ 死亡。
  • jobs.c wait_for/waitchld:死亡由 wait status 报告,jobs 表只读它。

修法(owner 分层,不在渲染层)

  • src/jobs/table.rs
    • take_delivered_signal(pid, status):只有 status == 128 + signal 才返回信号;不匹配时消费记录并返回 None(pid 复用不能借用旧投递)。
    • 新增 discard_delivered_signal(pid):内核 wait status 权威的平台只消费、不应用。
  • src/executor/job_builtins.rs
    • 新增 Executor::delivered_term_signal(pid, status):cfg(unix) 下丢弃记录返回 None(exit_status_signal 已给出真 WIFSIGNALED/WTERMSIG,exit 143 与 SIGTERM 死亡在 code 上不可区分,不能靠 code 猜);cfg(not(unix)) 下走带 status 门控的 take_delivered_signal。
    • apply_job_completion 用它替代原来的无门控 take_delivered_signal(pid)。

渲染层(render_background_jobs / printable_job_status 移植)未改。

GNU 5.3.20 矩阵(WSL 脚本文件;5 形 × jobs-then-wait / wait-then-jobs)

形 GNU 5.3.20 本修复(Linux 二进制)
a trap "" TERM; exit 5 [1]+ Exit 5 / rc=5 同
b trap "exit 143" TERM(睡眠后自退出) [1]+ Exit 143 / rc=143 同
c 真 kill -9 [1]+ Killed / rc=137 同
d 真 kill -TERM(默认处置) [1]+ Terminated / rc=143 同
e 普通 exit 7 [1]+ Exit 7 / rc=7 同
wait 先于 jobs(每形一遍) 无 job 行,rc 不变 同

10 个格逐字节比对,唯一差异是 c-wait 的死亡通知行(shell 路径 + pid,#3803 的既有措辞面,非本 PR)。fail-first:master 上 a/b 均为 Terminated。

Pin

tests/issue3802_delivered_signal_guard.rs(5 格):

  • issue3802_ignored_term_then_exit_5_is_exit_5(#[cfg(unix)])
  • issue3802_trapped_term_then_exit_143_is_exit_143(#[cfg(unix)])
  • issue3802_real_kill9_still_renders_killed
  • issue3802_plain_exit_7_is_exit_7
  • issue3802_wait_then_jobs_has_no_stale_row

结果:Windows 3 passed(2 个 unix 格按 cfg 跳过);Linux 5/5。既有 issue3802_jobs_table_killed_wording 在两侧仍 4/4。

邻接 stem:issue1048_1256(5)、issue1494(2)、issue1604(1)、issue3803(5) 绿;issue2340_wait_job_retention 的 2 格红与 master 逐格相同(既存/flake,非本 PR)。

本 PR 删除了什么

无功能删除:只把一个无门控的 decode 换成带 WIFSIGNALED 语义的分平台 owner(unix 只消费记录,Windows 才应用且需 status == 128+signal),并新增 1 个测试文件。

UNVERIFIED-BY-CAPTAIN

… (Refs #3802)

#3805 made the jobs table name the signal this shell's own kill
delivered, decoding the Windows `TerminateProcess (128+signal)` exit
code against a per-pid delivery record. The decode was unconditional:
`take_delivered_signal(pid)` ignored the reaped status, so on unix a
job that ignored or trapped SIGTERM and then exited on its own was
rendered from the record instead of its wait status --
`trap "" TERM; exit 5` printed `Terminated` where GNU prints `Exit 5`,
and `trap "exit 143" TERM` printed `Terminated` where GNU prints
`Exit 143`.

GNU decides the wording from the wait status alone: jobs.c:1969-2015
printable_job_status() calls j_strsignal(WTERMSIG(p->status)) only in
the WIFSIGNALED arm, and the WIFEXITED arm always renders `Exit %d`
(jobs.c wait_for reaps the status; kill_pid, jobs.c:3988-4007, is pure
delivery and never proof of death). A code equal to 128+signal is not
that proof: an `exit 143` is WIFEXITED.

Owner split:
- unix: the kernel wait status is authoritative (exit_status_signal),
  so the delivery record is only consumed, never applied
  (JobTable::discard_delivered_signal);
- Windows: no WIFSIGNALED exists, so the record supplies the signal
  only when the reaped code is exactly 128+signal
  (take_delivered_signal(pid, status)).

Oracle: WSL GNU bash 5.3.20 script files vs the fixed Linux binary,
5 shapes x {jobs-then-wait, wait-then-jobs} -- byte-identical except
the pre-existing Killed notice path (shell path/pid, #3803 face):
  a. trap "" TERM; exit 5          -> [1]+ Exit 5        rc=5
  b. trap "exit 143" TERM          -> [1]+ Exit 143      rc=143
  c. kill -9 (real SIGKILL)        -> [1]+ Killed        rc=137
  d. kill -TERM default            -> [1]+ Terminated    rc=143
  e. plain exit 7                  -> [1]+ Exit 7        rc=7
Fail-first on master: a/b print Terminated.

Pins: tests/issue3802_delivered_signal_guard.rs 5/5 (trapped shapes
`#[cfg(unix)]`; Windows now has no in-child signal disposition), plus
the #3805 pin 4/4 on Windows and Linux. Neighbouring jobs/wait stems
unchanged (issue2340's two reds are identical on master).
@caomengxuan666
caomengxuan666 merged commit a593395 into master Nov 17, 2026
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