Skip to content

executor: bracket per-process latches at the script-child boundary - #3724

Merged
caomengxuan666 merged 2 commits into
masterfrom
devin/child-latch-restore
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
devin/child-latch-restore

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

execute_direct_shell_script emulates GNU's bash script fork (execute_cmd.c:6139 shell_execve -> shell.c:429-464 shell_reinitialize) on the shared Executor, so every per-process latch the child mutates must be save/restored at the boundary. Two leaked:

  • exit_trap_ran — trap.c:1128 run_exit_trap's once-only flag. A child script's trap 'echo EXIT' EXIT fire left the latch set, so the next in-process child's ( trap T 0; ... ) subshell found it latched and skipped its own EXIT trap. In the macOS execscript gate this is what silenced exec12.sub's exit code: 1 line after exec3.sub ran (and the same leak ate the foo 0 / WORKS lines and exec5's echo $?).
  • exec_replaced — the exec.def replacement-unwind latch. A child's successful in-process exec latched it for the parent, suppressing the parent's own EXIT-trap adjudication (run_exit_trap_for_status early-out).

Both latches now start clear in the fresh child and the parent's values are restored in the epilogue — matching the process boundary GNU's real child provides (shell.c exit_shell).

Refs #3605

Verification

  • Reduced repro against the rubash binary (macOS arm64): ${THIS_SH} p1.sh (trap 'echo E-EXIT' EXIT; exit 0) followed by ${THIS_SH} e12.sh (main() {( trap 'echo TRAP-FIRED' 0; "$@" )}; main f) — TRAP-FIRED now prints; previously the child died silently rc=1.
  • niubash run-execscript gate: the foo 0, exit code: 1 x6, WORKS x4, and 0 vs 5 diffs are cleared; remaining residuals are the separately-tracked items (E2BIG covered by fix(executor): report E2BIG wording for an oversize command line (Refs unixwin/niubash#993 S4) #3712, exec -a $0, x1–x3b async stdin, coproc, job-control noise).
  • New pin tests tests/issue3605_child_latch_leak.rs (2 tests) cover the leak in both directions.
  • Regression slice: issue1012_exec_exit_trap, issue1037_shebangless_exec, issue1102_exec_stdin, issue1124_trap_entry_ignore, issue1162..1166_trap_return, issue0085..0290_trap_diag, host_internal_trap_suppression — all green. (issue1175_exec_image_regressions fails identically on master — known macOS Mach-O exec environment issue, not this change.)

本 PR 删除了什么

产品代码无删除:src/executor/external_finish.rs 为纯增量(进/出边界各一组 latch save/restore)。钉文件 tests/issue3605_child_latch_leak.rs 本轮重写为库 API 驱动,删除了原 Command::new(rubash) + ${THIS_SH} 子进程驱动方式(binary main 会 enable_real_shell_children,子进程间不共享 Executor,无法观测 latch 泄漏,属废钉)。

Refs #3605

`execute_direct_shell_script` emulates GNU's `bash script` fork
(execute_cmd.c:6139 shell_execve -> shell.c:429-464
shell_reinitialize) on the shared Executor, so every per-process
latch the child mutates must be save/restored at the boundary. Two
leaked:

- `exit_trap_ran` — trap.c:1128 run_exit_trap's once-only flag. A
  child script's `trap 'echo EXIT' EXIT` fire left the latch set, so
  the NEXT in-process child's `( trap T 0; ... )` subshell found it
  latched and skipped its own EXIT trap. In the macOS execscript
  gate, exec3.sub's EXIT trap silenced exec12.sub's
  `exit code: 1` line (and the same leak ate the `foo 0`/`WORKS`
  lines and exec5's execfail `$?` print downstream).
- `exec_replaced` — the exec.def replacement unwind latch. A child's
  successful in-process `exec` latched it for the parent, suppressing
  the parent's own EXIT-trap adjudication
  (trap_exec::run_exit_trap_for_status early-out).

Both latches now start clear in the fresh child and the parent's
values are restored in the epilogue, matching the process boundary
GNU's real child provides (shell.c exit_shell).

Verified on macOS arm64 against GNU bash 5.3.20: the reduced pair
`trap 'echo E' EXIT` + `main() {( trap T 0; "$@" )}` now prints the
subshell trap output, and the execscript gate drops the
`foo 0`/`exit code: 1`/`WORKS`/`0` diffs. New pin tests cover both
leak directions.

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 #3724 第1轮 — 建议:FAIL
head: 35f2e80 base(merge-base): 0587028 文件: 2 个 +122/-0
关联 issue: 无直接可关单(正文只 Refs #3605,伞目录 issue 不合入关单)

机制核查:

  • 修法本身方向对:exit_trap_ran 是 trap.c:1128 run_exit_trap 的 once-only 标志,exec_replaced 是 exec 替换的 unwind 标志,两者都随真实子进程死亡(shell.c exit_shell);rubash 的 execute_direct_shell_script 在共享 Executor 上模拟 bash script 的子进程,所以在边界处 save/restore 这两个 latch 是正确机制。
  • 边界内没有提前 return/? 绕过 restore:execute_direct_shell_script(external_finish.rs:439)里 save 于 :783 之后,唯一的 returns 在闭包内(:803/:811/:849),epilogue 的 restore 必达。
  • 可疑改动:无(只 +19 行 save/restore 与一个钉文件)。

F 点:

  • F1(阻断):钉是废钉——在 base 与 head 上都是绿,永远抓不到本 bug。 证据链:
    1. 钉在 base(0587028) 上跑 issue3605_child_latch_leak = 2 passed / 0 failed(Windows 与 WSL Linux 各一次,都是全绿)。
    2. head(35f2e80) 同样 2/2。也就是说「有修复/无修复」都通过。
    3. 根因:钉用 ${THIS_SH} p1.sh 走的是 rubash 二进制,而 rubash 的 main 主动开了真实子进程(src/main.rs:126 enable_real_shell_children();判定见 external_finish.rs:181-183 real_shell_children_apply 与 :280-289 的递归守卫)。真实 exec 子进程有独立地址空间,latch 根本不会被父 Executor 共享,因此 execute_direct_shell_script 的 in-process 模拟路径在这条钉里 一次都不执行。
    4. 直接证据(base 二进制,Windows):echo "parent=$$"; "$THIS_SH" -c 'echo "child=$$"' → parent=2704 / child=60052,PID 不同 = 真子进程。
    5. 修复真正生效的宿主是 library host(niubash 的 Session 执行器)——real_shell_children_apply() 对库宿主恒 false,只有那条路径才会走 in-process 模拟。作者的复现环境是 macOS niubash execscript gate,正好是库宿主;换成 rubash 二进制就测不到。
    • 修法(钉侧,不动产品):把钉改成经过库 API 的形态(rubash::api::Session 连续 apply 两个脚本),或在 lib 内直接对 execute_direct_shell_script 写单测;关键是运行同一个 Executor。改好后必须能在 e81a1af/05870284 这类 base 上红、在修复后绿。
  • F2(次要):正文没有「本 PR 删除了什么」一节(仓库规程要求,非纯测试 PR 必须写)。
  • F3(次要,与 F1 同源):钉的模块注释写「exec3.sub silenced exec12.sub」是 macOS execscript gate 的现象,但钉没有把那个 gate 的运行条件(库宿主 / 同 Executor)复刻下来,注释与覆盖面对不上。

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

  • 钉自跑:issue3605_child_latch_leak 2/2(Windows);Linux WSL 2/2。
  • fail-first:base(0587028) 上 2 passed / 0 failed,红名 = {}(应为 {child_exit_trap_fire_does_not_latch_parent_executor} 之类),与 PR 声称不一致——见 F1。
  • lib 双边 / -D warnings / 邻居:本轮未跑(F1 是判定性失败,先返工;返工后按规程补齐)。
  • 卫生:未跑(同上;返工后补)。
  • CI:未按名重核(head 的旧 run 只有基线 issue2875 + canary ledger 漂移;本判定不依赖 CI)。
  • GNU 对照:未新增;F1 的判别不依赖 GNU 行为。

未跑/VOID 的腿:

  • lib 双边、-D warnings、卫生、CI 按名、GNU 对照(原因:F1 判定性失败,返工后再跑)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3724 第2轮 — 建议:PASS
head: 3d8f8e2 base(merge-base): 0587028 文件: 2 个 +165/-0(产品 external_finish.rs +19;钉重写)
关联 issue: #3605(状态:open,我核过的时间:2026-11-16 21:41 CST)

机制核查:

  • 产品机制维持上轮结论:execute_direct_shell_script(external_finish.rs)在脚本子进程边界 save/restore exit_trap_ran/exec_replaced,对应 GNU shell.c:429-464 shell_reinitialize 的进程边界语义。本轮未动产品代码。
  • 钉重写(本轮 commit 3d8f8e2):原钉经 Command::new(rubash) -c '${THIS_SH} …' 驱动——binary main.rs:126 enable_real_shell_children() 使 real_shell_children_apply() 为真,${THIS_SH} x.sh 走真实 spawn,每个子进程独立 Executor,latch 无法跨界泄漏 = 废钉(base/head 同绿)。新版改走库 API:Executor::new() + tokenize/parse/execute_ast,real_shell_children_enabled() 在库上下文为 false → execute_same_shell_script 取 in-process execute_direct_shell_script 分支(external_finish.rs:331-337),两子进程共享同一 Executor,latch 泄漏可观测。断言改走 events.log 文件(execute_ast 写真实 stdout 不可捕获)。

腿(逐项数字):

  • 钉自跑 head:Windows cargo test --test issue3605_child_latch_leak → 2 passed / 0 failed;WSL Linux(/root/rubash-wsl-t2 @35f2e809 产品码 + 新钉文件)→ 2 passed / 0 failed
  • fail-first:base 0587028 + 钉原样 → 1 passed / 1 failed,红名 = {child_exit_trap_fire_does_not_latch_parent_executor},events.log 实测 P1-RAN\nP1-EXIT\n(TRAP-FIRED 缺失 = exit_trap_ran 泄漏压制次子壳 subshell EXIT trap),与缺陷机理逐字吻合;subshell_exit_traps_survive_across_script_children 在 base 绿属预期(a/b 脚本无脚本级 EXIT trap,不置位 exit_trap_ran,为守护格非 fail-first 格)
  • lib 双边(本机 Windows):head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同名集 / 差集 {}(本机环境性失败,两侧同名;本轮仅改 tests/ 文件,lib 不受影响)
  • -D warnings:CARGO_INCREMENTAL=0 RUSTFLAGS="-D warnings" cargo check --locked --lib --bins → 0 警告
  • 卫生:rustfmt --edition 2021 --check 钉文件 OK;marker-lint OK(117 consts, 268 allowlisted);utf8-hygiene clean
  • CI:新 head 3d8f8e2 的 CI 刚触发,待跑;旧 head 35f2e80 的 CI 红 = {GNU regression goldens(基线签名)、Rust tests、Rust tests (macOS)、niubash builds(Session::cwd ledger 跨仓基线)},与 master 基线一致
  • GNU 对照:上轮已核——execscript exec3.sub→exec12.sub 的 exit code: 1 静默正是 exit_trap_ran 泄漏形状(trap.c:1128 once-only);本轮钉在共享 Executor 上复现同一机理,语义锚点不变
  • 正文:已补「本 PR 删除了什么」一节(产品无删除;钉删除 Command 子进程驱动方式)

F 点:无(上轮唯一 F 点 = 废钉,已按机制重写并验证 fail-first 有效)

续作记录:

未跑/VOID 的腿:CI 全绿与否待新 head 跑完(约 10 分钟),其余无。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 17:19
@caomengxuan666
caomengxuan666 merged commit 83d19f9 into master Nov 16, 2026
9 of 12 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3724 第3轮 — 建议:PASS(已合并 83d19f9)
head: 3d8f8e2 base(merge-base): 0587028 文件: 2 个 +165/-0
关联 issue: #3605(伞目录,保持 OPEN)

本轮为合入前补验轮(u1),head 与第2轮审计完全一致,未重跑全腿,补齐缺口并复核:

腿:

  • 钉自跑(本机 Windows 复验):cargo test --locked --test issue3605_child_latch_leak → 2 passed / 0 failed(child_exit_trap_fire_does_not_latch_parent_executor、subshell_exit_traps_survive_across_script_children)
  • fail-first / lib 双边 / -D warnings / 卫生:第2轮已在同 head 跑齐(base 0587028 红名 {child_exit_trap_fire_does_not_latch_parent_executor};lib 差集 {};warnings 0;fmt/marker/utf8 净),本轮未重复
  • CI(head 3d8f8e2,run 43880632420)逐名归因:
    • GNU regression goldens 红 = {issue_753::case_9_declare_indexed_operand_binds_nothing, upstream_suite_slice_snapshots},与 master 08979f2 基线 run 43910187524 同名同数 → 基线
    • Regression pin stem census 红 93 名 vs master 基线 88 名:head 多出 6 名全在 issue371_assoc_assignment_transform(at_sign_at_a_quoted×3、assoc_member_and_bare_at_a、assoc_star_at_a_quoted、indexed_star_at_a_quoted),已核 88fd543(Refs [bug] 带引号的 "${a[@]#...}" 单 "" 元素应诊断,rubash 静默(#3501 残差) #3581 quoted-aware list path)不在 merge-base 0587028 内 → 陈旧基线非本 PR 账;master 侧多出 issue3275_ghost_recreated_cwd 为时序性。其余 87 名两侧相同
    • Cross-target check (darwin):conclusion=cancelled(19m57s 后取消,infra);linux/android pass
    • Rust tests / Rust tests (macOS) / Compatibility CLI / Shell functionality / Windows compile check / niubash builds / API ledger 全 pass
  • 合并性:git merge-tree vs origin/master 9defebd 干净(外部_finish.rs + 钉文件无冲突)
  • GNU 对照:第2轮已核(execscript exec3/exec12 静默 = exit_trap_ran 泄漏形状,trap.c:1128 once-only),本轮未新增

F 点:无

已执行:gh pr ready → gh pr merge --squash → MERGED,mergeCommit 83d19f9(gh pr view 复核 state=MERGED)。#3605 为伞目录 issue,不关单。

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