Skip to content

fix(executor): scope in-process ${THIS_SH} child's exit-trap latch to the child (Refs #3605) - #3670

Merged
caomengxuan666 merged 2 commits into
masterfrom
devin/this-sh-exit-trap-latch
Nov 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
devin/this-sh-exit-trap-latch

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Refs #3605 (macOS gate, run-trap).

trap.tests ends with trap 'echo exiting' 0; exit, but niu printed nothing: an earlier ${THIS_SH} ./trap6.sub / ./trap9.sub run had already fired an EXIT trap on the shared executor, latching exit_trap_ran, so the parent's re-armed trap was dropped.

  • GNU model: a bash script child is a fresh process (execute_cmd.c:6139 execute_disk_command forks and execs); run_exit_trap (trap.c:1027-1050) keys on the child's own sigmodes[EXIT_TRAP] — its SIG_TRAPPED/SIG_INPROGRESS never touch the parent.
  • execute_direct_shell_script (src/executor/external_finish.rs) emulates that boundary in-process for library hosts: it now parks the parent's exit_trap_ran, clears it for the fresh-shell child, and restores it after the child's run_exit_trap_for_status — the same save/restore contract the other per-process state already holds at this boundary.

Verified (macOS, niubash GNU gate)

  • run-trap: PASS (was: missing final exiting — output now byte-identical to trap.right)
  • run-glob-bracket: PASS, no regression
  • New tests/issue3605_this_sh_child_exit_trap.rs pins the parent-latch isolation across one and multiple in-process ${THIS_SH} children.

… the child (Refs #3605)

run-trap tail: ${THIS_SH} ./trap6.sub ran the child's EXIT trap on the
shared executor, latching exit_trap_ran; the parent's later
`trap 'echo exiting' 0; exit` then never fired.

GNU: a `bash script` child is a fresh process (execute_cmd.c:6139
execute_disk_command forks and execs), and run_exit_trap
(trap.c:1027-1050) keys on the child's own sigmodes[EXIT_TRAP] — its
SIG_TRAPPED/SIG_INPROGRESS never touch the parent's.

execute_direct_shell_script now parks the parent's latch and clears it
for the fresh-shell child, restoring it after the child's
run_exit_trap_for_status (external_finish.rs), mirroring the
save/restore contract the other per-process state already holds at
this boundary.

Verified on macOS (niubash GNU gate): run-trap PASS (was: missing
final 'exiting'), run-glob-bracket PASS, no regressions.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3670 第1轮 — 建议:FAIL(不合、不关单)
head: 44bc68c base(merge-base): 96802ca 文件: 2 个 +96/-0
关联 issue: #3605(open,umbrella 目录 issue — 按铁律不关单;核于 2026-11-16 21:0x 本地)

机制核查:

  • 方向与 GNU 一致:trap.c:1027 run_exit_trap 的判据是 (sigmodes[EXIT_TRAP] & SIG_TRAPPED) && (sigmodes[EXIT_TRAP] & (SIG_IGNORED|SIG_INPROGRESS)) == 0(trap.c:1045-1050),sigmodes 是进程内状态;GNU 的 ${THIS_SH} script 子壳是 fork+exec 的新进程(execute_cmd.c:6139),父壳的 sigmodes 不可能被子壳改写。rubash 的 in-process 仿真(execute_direct_shell_script)共享 exit_trap_ran,park/restore 的写法是合理方向;改动点是 src/executor/external_finish.rs:1099-1103 与 :1177,恢复语句在函数线性尾部、无早退绕过,写法本身没发现泄漏。
  • 但证据腿不成立(见 F1/F2):本 PR 的钉走的是 CLI 的「真实子进程」路径,根本进不到被改的那段仿真代码,所以钉在 base 上也是绿的。

F 点:

  • F1(阻断,废钉):钉文件 tests/issue3605_this_sh_child_exit_trap.rs 在 base(96802caf)与 head 上都全绿(本机 #![cfg(unix)] 门控,我按上一轮同样做法只在本地临时删该行取数、未提交):
    • base:2 passed; 0 failed;head:2 passed; 0 failed。
      静态原因:钉用 Command::new(env!("CARGO_BIN_EXE_rubash")) 起 CLI,而 src/main.rs:126 调 enable_real_shell_children();execute_same_shell_script 在 external_finish.rs:181-183 的 real_shell_children_apply()(= owns_process_image && enabled)为真时于 :314-320 直接 return Ok(false),走普通 disk-command spawn,execute_direct_shell_script 不执行。
      实测佐证(${THIS_SH} ./childpid.sub 各自打印 $$):base 二进制 parent=68752 child=24720 after=68752;head 二进制 parent=62796 child=12652 after=62796 —— 子壳确实是独立进程,exit_trap_ran 不共享,钉不可能红。
  • F2(证据与声称不符):PR/钉注释声称的 trap.tests 尾形在 base 上不复现:${THIS_SH} ./child6.sub(child 里 trap 'ReturnFalse && echo woops' EXIT)+ 父壳 trap 'echo exiting' 0; exit,base 二进制与 head 二进制都输出 exiting、rc0(GNU 5.3.20 同)。所以「base 上打印不出 exiting」这一前提在本机不成立;要证明该修复,必须让钉走 in-process 仿真路径(例如库内 Executor/Session 且不 enable_real_shell_children,或直接命中 ENOEXEC 仿真),而不是 CLI spawn。
  • F3(观察记录,不计账):head 的第一次全量 lib 跑出现 api::consumer_tests::issue863_gap8_finish_trap_runs_once_across_entries 红(fires 内容 "fired\nfired\n" vs "fired\n",consumer_tests.rs:3682)。单测隔离跑两次全绿、第二次全量 lib 也只剩 issue2084;base 全量 lib 同样只有 issue2084 → 判为顺序/并行 flake,不作为本 PR 的账,但请后续留意它和 EXIT 闩锁的交互。

腿(逐项给数字与名字):

  • 钉自跑(head,本地去 #![cfg(unix)]):issue3605_this_sh_child_exit_trap → 2 passed; 0 failed(issue3605_this_sh_child_exit_trap_does_not_latch_parent、issue3605_parent_exit_trap_after_multiple_children)。
  • fail-first(base 96802caf,同一去 cfg 钉文件原样放入):2 passed; 0 failed → 红名集合 = ∅,与 PR 声称的失败面不一致(见 F1)。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1350 passed/1 failed/4 ignored;另一次跑出 issue863 flake,见 F3);base 同集 {同一条};差集 ∅。
  • -D warnings(cargo check --all-targets):rc=101,唯一 error 为基线既有告警(非本 PR 文件,tests/ 既有);本 PR 两文件 0 告警。
  • 卫生:rustfmt --check(external_finish.rs、新测试)clean;check-marker-literals.py → OK;check-utf8-boundary-hygiene.sh → clean。
  • CI(head 44bc68c5,run 43694500138):红 = Rust tests / Rust tests (macOS) / GNU regression goldens and canaries,唯一 FAIL 均为 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(consumer_tests.rs:3503 "no rubash binary found")→ issue2875 基线,非本 PR;无 E0063/编译类红。
  • GNU 对照(WSL GNU bash 5.3.20(1)-release):${THIS_SH} ./child.sub + 父壳 trap 'echo exiting' 0; exit → GNU exiting,rc0;rubash base/head 均 exiting,rc0(无分歧,也就没有可判别的修复证据)。

F 点结论:F1 废钉是硬伤(规程:base 全绿的钉 = 废钉 = FAIL 点)。建议改法:把钉改成能进入 in-process 仿真的形状(库内 Executor/Session、不启用 real shell children;或 ENOEXEC 无 shebang 脚本在 CLI 下走的那条仿真),并让它在 base 上确实红。

未跑/VOID 的腿:钉的 Windows 原样运行为 0 tests(#![cfg(unix)]),已用本地去 cfg 补跑并如实披露,未提交、已还原。
平台局限:GNU 对照为 WSL Linux 5.3.20;结论不依赖平台专属行为。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3670 第2轮 — 建议:PASS
head: 45269c2 base(merge-base): 96802ca 文件: 2 个 +144/-0
关联 issue: #3605(状态:open umbrella,我核过的时间:2026-11-17 01:57 本地)

机制核查:

  • 产品 diff(44bc68c51,external_finish.rs +21)本轮未再动:fresh_shell 子进程执行前 park exit_trap_ran、执行后无条件 restore。GNU trap.c:1045-1050 run_exit_trap 以进程私有 sigmodes[EXIT_TRAP] 为准——真实 bash script 子进程的 latch 随进程消亡,不回渗父进程;in-process 仿真必须显式隔离,共享 executor 状态正是原缺陷根因。fresh_shell=false 路径子进程不经过 run_exit_trap_for_status(:1168 门),latch 不可能被改,unconditional restore 对其是无害 no-op——机制成立。
  • diff 无无关改动、无桥接/翻译层。

腿(逐项给数字与名字):

  • 钉自跑(head,Windows):tests/issue3605_this_sh_child_exit_trap.rs → 2 passed / 0 failed(issue3605_this_sh_child_exit_trap_does_not_latch_parent、issue3605_parent_exit_trap_after_multiple_children)
  • fail-first(merge-base 96802ca 独立 worktree 原样放钉):0 passed / 2 failed,红名 = {issue3605_this_sh_child_exit_trap_does_not_latch_parent, issue3605_parent_exit_trap_after_multiple_children},均为 "parent EXIT trap wrote its file: NotFound"——父 trap 被泄漏 latch 吞掉,与 PR 声称缺陷一致,钉有效
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同集 / 差集 ∅(已知 Windows 环境 flake,两侧同现)
  • -D warnings:tests/issue3605_this_sh_child_exit_trap check 0 警告;全仓 -D warnings 被无关基线警告阻塞(tests/issue1794 未用导入等,与本 PR 无关)
  • 卫生:rustfmt --edition 2021 --check 钉文件 clean;marker-lint OK;utf8-boundary clean
  • CI(head 45269c2,run 43917212798):Rust tests / Rust tests(macOS) / Shell functionality / Compatibility CLI / Cross-target darwin / Cross-target linux/android / Windows compile / niubash builds / API ledger 全 pass;GNU goldens 红 = {issue_753::case_9_declare_indexed_operand_binds_nothing, upstream_suite_slice_snapshots}(master 同 run 已知基线 2 红);census 89 红,与 master 83d19f9 同 run 90 红逐名 diff:head 无新增失败名(master 侧多 element_assigned_indexed_array_keeps_at_a_body 一条,非本 PR 账)
  • GNU 对照:WSL /usr/local/bin/bash = GNU bash 5.3.20(1)-release;脚本 target/u4-probes/i3605-probe.sh(${THIS_SH} child-with-EXIT-trap 后父 trap 'echo exiting' EXIT; exit)→ GNU 输出 exiting / rc=0;rubash CLI 同脚本输出 exiting / rc=0 → 一致。判别性证据在 Session API 钉:CLI 路径 real_shell_children_apply 走真实 spawn 本就不经过被改的 in-process 仿真,原 Command::new(CARGO_BIN_EXE_rubash) 钉在 base/head 双绿属废钉;新钉经 Session API(不启用 real children)真实进入 execute_direct_shell_script in-process 分支

F 点:无

续作记录:

未跑/VOID 的腿:无

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 01:48
@caomengxuan666
caomengxuan666 merged commit a733ea0 into master Nov 17, 2026
10 of 12 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