Skip to content

fix: async job signal-death notice lost / doubled (Refs #3637) - #3799

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3637-async-death-notice
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3637-async-death-notice

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Refs #3637

根因

signal-killed 异步作业的死亡通知(bash: line 1: PID Killed <cmd>)在两种情形下出错:

  1. 丢失:作业在 intervening foreground 外部命令等待期间被 reap,通知进入 pending_signal_notices 队列;随后的 wait $p 只排空 wait 当次产生的通知(queue[mark..]),预先排队的通知留在队列里;-c 单 list 结束时驱动丢弃剩余队列,通知永不打印。
  2. 翻倍:jobs 先列出死亡作业(render 标记 notified),随后 jobs 内置命令后的 report_pending_signal_notices() 冲刷仍把队列里的同一通知打印一次 —— 冲刷路径没有任何 IS_NOTIFIED 守卫。

GNU 锚点(bash-5.3,third_party/bash/;本地对照 ~/workspace/rubash-audit/build-gnu/bash-5.3/):

  • jobs.c:3385 wait_for 调用 notify_and_cleanup(job) —— wait 打印所等作业的通知。
  • jobs.c:3434-3437 wait_for_job 在应答后标记 J_NOTIFIED。
  • jobs.c:3474/3486 wait_for_any_job 扫描未通知的死亡作业并 notify_of_job_status。
  • jobs.c:3642 notify_and_cleanup = maybe_print_job_notifications + cleanup_dead_jobs。
  • jobs.c:4570 notify_of_job_status 以 IS_NOTIFIED (job) == 0 为整打印守卫。
  • jobs.def:141/172 jobs 先列出作业再 notify_and_cleanup(-1)。

修复(机制修,两面同修)

  1. Executor::wait_notice_pids: Vec<u32>(src/executor/mod.rs):记录一次 wait 经已完成臂解析的 pid;两个穷举字面量构造点(init.rs、command_substitution.rs)以 Vec::new() 补齐。
  2. 已完成臂(wait_for_background_pid)解析时 push pid。
  3. execute_wait 在 inner wait 前清空、后排空:除 wait 当次产生的通知外,另取走预先排队且属于本次解析 pid 的通知(只取所等作业的,不取其它作业的),经 wait 自身 fd-2 路由([bug] KILLed-job Killed notice bypasses 2>/dev/null on wait (GNU: suppressed) #1303 纪律)。
  4. emit_signal_notices 增加 IS_NOTIFIED 守卫:作业表里已标记 notified 的 pid 不再打印(边界 / 流结束 / jobs 冲刷路径)。wait 的排空直接走 format_signal_notices、有意绕过该守卫 —— wait 本身就是通知点(jobs.c:3385),且 wait 的记账在排空前已标记所等作业 notified;若守卫放在 formatter 里会破坏当前已工作的即时 wait 与 bare wait 通知。无作业表条目的 pid 仍打印(前台通知与 procsub 角,行为不变)。

#3642 的 CONT 面(limit_builtins.rs + tests/issue3637_kill_cont_stopped_job.rs)未动;其钉在邻居腿中 1/1 通过。

本 PR 删除了什么

无删除。仅新增 wait_notice_pids 字段与回归钉;无公开 API 变动,无行为删除。

验证(Linux)

  • fail-first(base 88fd543):seed seed_r118a.sh(rubash 作驱动)4/9,检查 1–5 失败、控制项通过;GNU 9/9。注:BASH=<sut> bash seed.sh 会因 bash 重置 $BASH 而空跑,必须以 shell under test 作驱动。
  • 修绿:seed 9/9;回归钉 tests/issue3637_async_death_notice_regressions.rs 8/8。
  • 邻居:issue1604 1/1、issue3637_kill_cont_stopped_job(fix(executor): publish Running at kill -s CONT delivery on a recorded-stopped job #3642 钉)1/1、issue1494 2/2、issue2340 9/9(首跑 1 格翻转,重跑 9/9 —— 时序抖动)、issue1048_1256 5/5、issue2735 3/3、issue2769 5/5、issue2928 6/6、issue1303 被忽略的单测 --ignored 6/6(文档化残差未复现);issue2398 1/2 与 issue2697 3/5 —— 两红均在 base 上逐格相同,为既存红(2398:kill→wait reap 竞态;2697:[A2-14] Signal-death job notice renders the whole subshell text instead of the killed member command #2697 自身的 subshell 命名面),非本 PR 回归。
  • RUSTFLAGS="-D warnings" cargo build --locked --bin rubash rc=0;cargo check --locked --lib --bins rc=0;marker-lint OK;rustfmt clean;LF 行尾。

验证(X299 / Windows)

  • 构建:修后与 base 在 X299 上均 cargo build --locked --bin rubash rc=0(各约 1m05s)。钉为 #![cfg(unix)],Windows 上不跑;X299 腿 = 构建 + seed 在 rubash.exe 下的复核。
  • Seed(seed_r118a.sh,rubash.exe 作驱动):base 3/9 → 修后 6/9。丢失面已修:检查 1、4、5 由 FAIL→PASS。
  • 翻倍面(检查 2、3)在 Windows 上读数为 0:修后输出仅有 jobs 表一行 [1]+ Exit 137 sleep 30,无 stderr 通知。直接对照探针(sleep 30 & kill -9 %1; sleep 0.2; jobs; wait %1)证明:base 的表行同样是 Exit 137,其 seed 计数 1 来自翻倍的 stderr 通知(60736 Killed sleep 30),修后该通知被正确抑制。行为(恰一次死亡报告 = 表行)正确;seed 的 Killed 字数假设在 Windows 上不成立 —— Windows 表行把死亡渲染为 Exit 137 而非 Killed,为既存 Windows 措辞面,宜另立单。
  • 控制项即时 wait(检查 6)在 Windows 上 base 与修后均失败(无通知):wait 自身 reap 的通知在 Windows 上不打印 —— 既存 Windows 面缺口,宜另立单。
  • Base 另见 wait: %1: no such job(kill→wait reap 竞态,与 issue2398 同源),修后该形状无此报错。

UNVERIFIED-BY-CAPTAIN

caomengxuan666 and others added 2 commits November 16, 2026 14:16
GNU jobs.c:3385 wait_for calls notify_and_cleanup(job): the
`bash: line 1: PID Killed <cmd>` notice prints inside `wait`, exactly
once. rubash lost it when the reap happened at an earlier boundary
(the pre-queued notice survived `wait`'s queue[mark..] drain and was
dropped at stream end) and doubled it when `jobs` listed the death
first (the post-listing flush printed the still-queued notice with no
IS_NOTIFIED guard).

Fix: `Executor::wait_notice_pids` records the pids a `wait` resolves
through the completed arm; `execute_wait` drains their pre-queued
notices (only the waited pids') through the wait's own fd-2; and
`emit_signal_notices` drops notices for jobs already marked notified
(GNU jobs.c:4570). The wait drain bypasses that guard by design —
wait IS the notify point (jobs.c:3385).

Regression pin: tests/issue3637_async_death_notice_regressions.rs
(8 cells: lost face, doubled face, set -m, rerun, immediate-wait
and TERM controls, notice-during-wait order cell). Seed 4/9 -> 9/9.

Refs #3637
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 16:56
@caomengxuan666
caomengxuan666 merged commit 7ddcd42 into master Nov 16, 2026
24 of 32 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
#3729)

## Summary

Fixes the stdio-marker reinstate hazard in `close_persistent_output_fd` (rubash#3729): the face took a bare `Rc` clone of the fd's entry, then `open_output(fd, Stdout/Stderr marker)` retired the displaced descriptor (`retire_read_arm`, rubash#3213), and the restore re-inserted the retired entry — the same hazard as #3706. The snapshot is now a kernel duplicate via `FdTable::saved_entry` (the #3723/#3739 pattern), exactly like GNU's `fcntl (fd, F_DUPFD, SHELL_FD_BASE)` save (redir.c:965/1082/1144). A dup failure propagates (GNU fails the redirect the same way).

## 本 PR 删除了什么

无删除。仅将一行裸 clone 改为内核 dup 快照,并新增回归钉文件 `tests/issue3729_stdio_marker_fd_restore.rs`(7 格)。

## Reachability note (read before reviewing the pin)

The face needs a File-backed read arm AND the output-procsub registration to coexist on fd 1/2 at close/flush time. They cannot: every input bind calls `clear_output_fd_mirrors` (rubash#3655) and drops the registration, and every output bind retires any read arm. A full trigger battery against GNU bash 5.3.20 and the base binary (f56716b) confirms it — `exec 2> >(...)` + `exec 2<input.txt` + close/rebind/dup-to-closed/failed-rebind, group variants, and the `{var}`-close form all agree with GNU on the read result. So there is NO shell-level fail-first cell for the face itself: the 7 pin cells are GUARDS on the face's code path (stdio-marker reinstate + fd state after close/rebind), written so a future change that makes the face reachable cannot silently reintroduce the hazard. All 7 pass on the base and on the fixed head. Full battery + evidence in REPORT.md (workdir `~/workspace/fix-3729/REPORT.md`).

A separate real divergence in the same area — the deferred procsub body is dropped after an input rebind (GNU runs it at close, rubash does not) — is documented as a residual in the REPORT for the captain to file as its own issue; it is deliberately NOT pinned here (it fails on the fixed head too).

## Legs (队长复核)

- X299 base pin stem (`issue3729_stdio_marker_fd_restore`): **7 passed / 0 failed** (base build RC=0, 45.10s).
- X299 head pin stem: **7 passed / 0 failed** (head build RC=0, 14.02s).
- X299 lib bilateral: head **1362 passed / 1 failed** vs base **1362 passed / 1 failed** — sole fail on both sides is the known `api::consumer_tests::issue2084_session_end_restores_host_process_state` (no new red).
- `RUSTFLAGS="-D warnings" cargo check --locked --all-targets`: **RC=0**, zero diagnostics (39.83s).
- `rustfmt --edition 2021 --check` on the 2 changed files: **RC=0**.
- marker-lint: **OK** (118 registry consts, 268 allowlisted sites).
- CI(更新头 6fd24c2,run 43926778677,已 update-branch 到尖 52ffeba):goldens 恰 2 基线格(issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots);census 78 格、issue3729 钉全绿;其余 8 项作业全绿、1 项跳过。3 格 head-only(elf_operand_is_refused_not_run_as_script、issue1902_stdout_stderr_names_follow_their_bindings、issue3575_posix_cd_out_after_failure_restores_pwd)皆为已备案 census 抖动格(issue1902 为 #3799 复核裁定的抖动格;elf/issue3575 同现于 #3801/#3807 头;修法面已证不可达,钉为 guard 钉)。旧头 census 重跑已证同头两次跑 4 格翻动。

队长复核(2026-11-17 02:2x CST):diff 已读——trap_exec.rs 1 行裸 clone 改 kernel-dup 快照 + 7 格 guard 钉;reachability §3.2 要点已亲核(clear_output_fd_mirrors 删双键、三处 set_fd_input_* 尾调用、open_output 替换双臂);X299 腿钉 7/7 双边、lib 双边同名同数、-D warnings rc=0、fmt 干净、marker-lint OK。
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
… (#3811)

Refs #3803

## 根因

GNU jobs.c:3385 `wait_for` calls `notify_and_cleanup(job)` — the
`bash: line 1: PID Killed <cmd>` stderr notice prints inside `wait`
as soon as the death is noticed. On Windows the wait's own reap never
queued the notice: `wait_for_background_pid`'s poll loop queues it
only when `exit_status_signal` returns `Some(signal)`, and that helper
is always `None` on Windows (no WTERMSIG). The boundary sweep usually
loses the immediate-wait race (its `try_wait` runs microseconds after
the kill), so the poll loop does the reap — and it was blind. Hence
`sleep 30 & kill -9 %1; wait %1` printed nothing, on base and on the
#3799 head alike. The boundary reap already decodes the same death
from the flattened status (`apply_job_completion`: `status > 128` ->
`status - 128`), which is why delayed-wait checks (with an intervening
foreground wait) already printed on Windows.

GNU 锚点(bash-5.3,`~/workspace/rubash-audit/build-gnu/bash-5.3/`):
- `jobs.c:3064` `wait_for`,`:3385` 调用 `notify_and_cleanup(job)`。
- `jobs.c:3401` `wait_for_job` 标记 `J_NOTIFIED`;`jobs.c:3456`
  `wait_for_any_job` 扫描未通知死亡作业并 `notify_of_job_status`。
- `jobs.c:3642` `notify_and_cleanup`;`jobs.c:4543`
  `notify_of_job_status`,`:4625` 起 JDEAD 打印分支。

## 修复(机制修)

1. `src/executor/wait_status.rs`:新增 `exit_code_signal(status)`——
   Windows 上把 128+signal 退出码解码为信号(`code > 128` ->
   `code - 128`),与边界收割已用的约定完全一致;unix 上恒返回
   `None`(内核 wait status 为准)。rubash 自己的 kill 本就按此编码
   (`src/builtins/kill.rs` `signal_process` ->
   `TerminateProcess(128+signal)`)。
2. `src/executor/job_builtins.rs` `wait_for_background_pid` 轮询臂:
   `exit_status_signal(&done).or_else(|| exit_code_signal(&done))`。
   该臂覆盖全部 wait 形态(显式 pid / jobspec / bare wait /
   procsub 等皆经此函数)。
3. 有意不并入 `exit_status_signal`:其余调用方(jobs 表
   `term_signal`、SIGINT-unwind、`#1604 fresh_fatal` 门)在 Windows
   上必须继续看到 `None`——否则会悄悄改写 jobs 表渲染
   (`Exit 137` -> `Killed`,即另立的 #3802 措辞面)并重排 #1604
   的 stash 时序。只修 wait 收割这一面;接受的歧义与边界收割一致
   (自然 `exit 137` 读作信号 9,边界收割今天已如此)。

## 本 PR 删除了什么

无删除。仅新增 `exit_code_signal` 解码函数与 5 格 Windows 回归钉;
无公开 API 变动,无行为删除(Linux 面行为恒等:unix 臂返回 `None`)。

## 验证(X299 / Windows)

- Base(`fcc1de52`)与 head 构建皆 rc=0。
- Fail-first:新钉在 base 上 2/5(3 格即时-wait 全红,
  `notice missing in: wrc=137`;TERM 控制与顺序格通过);seed 在
  base 上 check 1/2/3 皆无通知。
- 修绿:新钉在 head 上 5/5,重跑 5/5;seed 5 格全过,check 1/2/3
  皆打印 `... line N: PID Killed sleep 30`,check 4(TERM)正确静默,
  check 5 通知落在 MARKER1/MARKER2 之间。
- `RUSTFLAGS="-D warnings" cargo check --locked --lib --bins` rc=0;
  `rustfmt --edition 2021 --check`(3 个改动文件)rc=0。注:`cargo
  fmt --check` 整仓在 Windows 上报 `os error 206`,在原样 base 树
  上亦同,为既存 Windows 环境问题,与本改动无关。

## 验证(Linux / 新服务器)

- `RUSTFLAGS="-D warnings" cargo check --locked --lib --bins` rc=0
  (TUNA `--config` 绕行:rsproxy.cn 索引对 os_pipe 404)。
- 邻居钉(`scrubbed_cargo.py`):issue3637 8/8、issue1303(1
  ignored)、issue1604 1/1;issue2698 4/5——唯一红格
  `wait_jobspec_after_boundary_notice_reports_no_such_job` 在 head
  与 base 上皆 3/3 同红(同断言、同 `waitrc=137` 输出),为既存红,
  非本 PR 回归。

UNVERIFIED-BY-CAPTAIN

Co-authored-by: 曹梦轩 <[email protected]>
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