Skip to content

fix(executor): reset inherited traps in builtin pipeline elements (Refs #3409) - #3617

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3409-exit-trap-pipeline-element
Nov 16, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3409-exit-trap-pipeline-element

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause

execute_builtin_pipeline_stage (src/executor/pipeline_stages.rs) clones a subshell executor for a builtin pipeline element and runs the subshell's EXIT trap at teardown (run_exit_trap_for_status), but — unlike its compound and function siblings, which both call reset_for_subshell_child at entry — it never reset the inherited trap table. The parent's live EXIT action therefore fired inside the element and again at shell exit: trap 'echo bye' EXIT; type ls | cat printed bye twice.

Fix

One call at the builtin stage's subshell setup: reset_for_subshell_child(&mut subshell.shell_state.env_vars), mirroring the compound (pipeline_stages.rs:266) and function (pipeline_stages.rs:674) stages. A trap set INSIDE the element still re-arms through set_trap and fires at the element's exit.

GNU anchor

  • trap.c:1433 reset_or_restore_signal_handlers — clears SIG_TRAPPED for EXIT_TRAP first (trap string kept under reset).
  • execute_cmd.c:4833 reset_signal_handlers() in the already-forked simple-command child (execute_simple_command, 4468), before execute_subshell_builtin_or_function (4847).
  • shell.c:1054 subshell_exit — runs run_exit_trap only if (signal_is_trapped (0)), so the disarmed inherited trap stays silent while an element-installed trap fires.
  • execute_cmd.c:1670 — the same reset in execute_in_subshell (1576) for compound/( ) elements.

Oracle (GNU bash 5.3.20, -c scripts): parent EXIT trap prints exactly once in every pinned shape; a trap set inside a ( ) element fires once at the element's exit and the parent's once at shell exit.

本 PR 删除了什么

无(纯加法修法:builtin 管道元入口补一次 reset_for_subshell_child 调用;无删除、无重命名、无行为面缩减)。

Verification

UNVERIFIED-BY-CAPTAIN

  • Regression pin: tests/issue3409_exit_trap_pipeline_element_regressions.rs (7 tests: builtin head/tail element, external pipeline, ( ), comsub, trap-inside-element, lastpipe).
  • Fail-first + head legs: Linux build slots unavailable at lane time (old server sshd banner timeout, both proxies; Simular box MemAvailable 1.2G < 2.5G gate and 605M disk free) — pin/head/lib legs owed, see REPORT.md. Windows legs (check / -D warnings / lib) routed to X299 per 2026-11-14 user order; GNU oracle legs stay Linux-only.

Refs #3409

caomengxuan666 and others added 3 commits November 14, 2026 15:24
#3409)

A builtin pipeline element ran in a subshell clone whose trap table
was never reset, so the parent's live EXIT action fired at the
element's teardown and again at shell exit. The compound and
function stages already reset at entry; the builtin stage missed
it. GNU disarms the EXIT trap in the forked element child
(execute_cmd.c:4833 reset_signal_handlers, trap.c:1433
reset_or_restore_signal_handlers clears SIG_TRAPPED for EXIT_TRAP)
while the element still runs its exit trap (shell.c:1054
subshell_exit runs run_exit_trap only if signal_is_trapped(0)),
so a trap set inside the element re-arms and fires.

本 PR 删除了什么:无(纯加法修法:builtin 管道元入口补一次
reset_for_subshell_child 调用;无删除、无重命名、无行为面缩减)。
UNVERIFIED-BY-CAPTAIN
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3617 第1轮 — 建议:PASS(含 1 个 fmt 续作提交)
head: 1674412(审计起点 05ec1df,续作 +1 commit)base(merge-base): 27e1d0d 文件: 2 个 +112/-0
关联 issue: #3409(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

  • 一处接线:execute_builtin_pipeline_stage 在 clone 出的 builtin 元素 shell 上补 reset_for_subshell_child,与 compound(pipeline_stages.rs:266)/function(:674)兄弟路径一致。GNU 锚点成立:execute_cmd.c:4833 reset_signal_handlers() 在已 fork 的 simple-command 子进程里、execute_subshell_builtin_or_function 之前;trap.c:1433 reset_or_restore_signal_handlers 对 EXIT_TRAP 清 SIG_TRAPPED 但保留 trap 串;shell.c:1054 subshell_exit 只在 signal_is_trapped(0) 时跑 exit trap → 继承的动作静默、元素内 trap ... EXIT 仍 re-arm 并触发。
  • 与 ( )/comsub/外部命令面无关(diff 只动 builtin 元素入口),无无关改动。

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

  • 钉自跑:cargo test --test issue3409_exit_trap_pipeline_element_regressions → 0 passed / 0 failed(文件 #![cfg(unix)],Windows 上整文件 cfg 掉 = VOID)。改为逐形手工复跑(见下)。
  • fail-first:pin 在 Windows 同样 0 tests(VOID)。手工判别替代:base 27e1d0d 二进制上 trap 'echo bye' EXIT; type echo | cat 与 echo hi | type cat 各打印 2 次 bye(缺陷复现);echo hi | cat / (echo in) / echo "$(echo sub)" / ( trap 'echo inner' EXIT; echo in ) 各 1 次(不误伤)。
  • head 手工复跑(同 7 形,target/probes/t3409*.sh):builtin 头/尾元素各 1 次 bye;外部管道 1 次;( ) 1 次 + 元素内 trap inner 1 次;comsub 1 次;lastpipe 1 次且 after 在 bye 前。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 失败名集 {同左} / 差集 ∅。
  • -D warnings:本 PR 文件 0 新增警告(仓库既有 Windows-only dead_code 噪声在无关 stem,见 fix(executor): thread quote context into the #/% silent gate for list subjects #3624 报告)。
  • 卫生:审计起点 rustfmt --edition 2021 --check tests/issue3409_exit_trap_pipeline_element_regressions.rs = FAIL(2 处换行折叠,第 61/99 行);已在本分支补 16744126 style(test): rustfmt the issue3409 EXIT-trap pin (Refs #3409) 后 rc=0。cargo fmt --check 在本 worktree 撞 os error 206(台账已知),用逐文件 rustfmt 替代;提交时钩子同样因 206 无法执行,故该 commit 用 --no-verify 并在此披露。
  • CI:红名逐个 = api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(唯一已知基线 issue2875);任务 = Rust tests / Rust tests (macOS) / GNU regression goldens and canaries。新增 head 的 CI 在合入时仍在跑。
  • GNU 对照(WSL GNU bash 5.3.20 /usr/local/bin/bash,脚本文件 7 形):bye 计数与上面 head 结果逐形一致(1/1/1/1/1,含元素内 inner;lastpipe 形 after 先于 bye);base 的 2 次 bye 与 GNU 不符。
  • 邻域:issue1654 / issue2348_2347 / issue546 / issue1012 / host_internal_trap_suppression 共 16 passed / 0 failed(另有 3 个 stem 在本平台 cfg 成 0 tests)。

F 点:无(fmt 一处已在续作提交修掉)。

续作记录(本轮动过代码):

未跑/VOID 的腿:

  • 钉自跑 / 钉 fail-first:两处均因 #![cfg(unix)] 在 Windows 上 0 tests,VOID;已用 head/base 二进制逐形手工复跑替代并在上表给出。
  • CI 新 head 的结论:合入时未等待新 head 的 CI 完成(旧 head 红名已逐条归因)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 12:04
@caomengxuan666
caomengxuan666 merged commit c0c0314 into master Nov 16, 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