Skip to content

fix(executor): give times fork-face baselines in subshell/comsub/pipeline (Refs #3387) - #3481

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3387-times-subshell-rusage
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3387-times-subshell-rusage

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

times 印的是宿主进程的 getrusage(RUSAGE_SELF) + getrusage(RUSAGE_CHILDREN),而三 fork 面在 rubash 里都是进程内模拟、共用进程 rusage:( ) 子 shell、$()/${ }/<( ) 体、管道段全都印宿主累计值,而非真 fork 的近零时钟。

GNU 锚点:builtins/times.def:65-84(子进程内读两组 rusage);execute_cmd.c:1576 execute_in_subshell、subst.c:7143 command_substitute、subst.c:6362 process_substitute、execute_cmd.c:2620 execute_pipeline 均为真 fork。

修法

read_helpers.rs 增线程本地基线栈(仿 profiler.rs 的 thread_local 惯例):每个 fork 面在进入时快照四组原始计数,process_times() 返回当前值减栈顶基线(saturating_sub)。

  • CloneBoundary:ShellState::clone_for_child_save() 内压栈,覆盖 flat ( )、in-place comsub 快捷路径、fresh-shell 子脚本;restore_flat_subshell() 与 external_finish.rs 直接赋值恢复处按 (kind, depth) 条件弹栈。
  • PipelineStage:execute_pipeline_stage() 顶 RAII guard;lastpipe 末段不走该漏斗(execute_lastpipe_stage),其 times 保持累计值(execute_cmd.c:2758,负对照)。
  • ComsubBody:$()、${ }、<( ) 体执行点各置 guard。

time 关键字不动(走 rusage_counters() 差分读数)。type -t 快捷路径不跑用户代码,未加基线;外部命令替换是真子进程,无需基线。

回归钉

tests/issue3387_times_fork_faces_regressions.rs:50k 迭代父面 burn 共享;subshell/comsub/pipeline/procsub 四面 SELF user ≤ 20ms 且 < 父;lastpipe 末段保持累计(> 20ms)。

本 PR 删除了什么

无删除。纯加法:新增基线栈与 guard(read_helpers.rs),其余 6 个文件各加数行压栈/弹栈调用,新增 1 个回归钉文件。

UNVERIFIED-BY-CAPTAIN

本轮未做本地构建与测试运行(本地写墙 EDQUOT 禁构建;Simular box 盘满;老服务器 ssh 未恢复;X299 直连未通)。以下腿留队长复核轮在 X299 上跑:新钉 stem、lib 全量与 base 逐数对齐、RUSTFLAGS="-D warnings" cargo check、逐文件 rustfmt --check。

Refs #3387

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3481 第1轮 — 建议:PASS(附 1 条 fmt 卫生红,见下)
head: 0a678f1 base(merge-base): 3745712 文件: 8 个 +350/-0
关联 issue: #3387(状态:open,我核过的时间:2026-11-15 本地)

机制核查:

  • GNU 在每个 fork 面里 getrusage 读到近零时钟:builtins/times.def:65-84(RUSAGE_SELF/RUSAGE_CHILDREN)、execute_cmd.c:1576 execute_in_subshell、subst.c:7143 command_substitute、subst.c:6362 process_substitute、execute_cmd.c:2620 execute_pipeline。rubash 在进程内跑这些面,本 PR 加线程局部 baseline 栈:clone_for_child_save 推 CloneBoundary(state.rs:988+),execute_pipeline_stage 顶推 PipelineStage(pipeline_exec.rs),comsub/procsub 体推 ComsubBody(command_substitution.rs / read_io.rs:1943+),process_times 对栈顶做 saturating 差值(read_helpers.rs)。pop 侧按 (kind, depth) 匹配(pop_times_baseline_if / RAII Drop / public_accessors.rs:2348 restore_flat_subshell / external_finish.rs 直接赋值路径先 pop 再 restore)——错位的 restore 不会吃掉外层 baseline,不变量设计正确。lastpipe 不经 execute_pipeline_stage 走 execute_lastpipe_stage,保留累计值,与 GNU execute_cmd.c:2758 一致。
  • 范围注记:read_helpers.rs +146 即 baseline 设施本体、read_io.rs/public_accessors.rs/state.rs 为 push/pop 配套,全部在修复面内,非无关改动。

腿(逐项):

  • 钉自跑:issue3387_times_fork_faces_regressions 2 passed / 0 failed(Windows head 实跑 6.82s)
  • fail-first:WSL Linux merge-base 1 failed / 1 passed,红名 = {fork_faces_report_near_zero_times}(panic: subshell=4849 parent=4848,完全复现 bug);对照 lastpipe_last_stage_keeps_accumulated_times 双侧绿
  • lib 双边(Windows):head {issue2084_session_end_restores_host_process_state}(1349/1/4)= base → 差集 ∅
  • -D warnings:lib+bins 0 警告
  • 卫生:rustfmt --check 红 3 处(src/executor/read_helpers.rs:319 可折叠元组、tests/issue3387_times_fork_faces_regressions.rs:62 与 :96 可折叠链式调用——一次 cargo fmt 即修,建议合入前补一个 fmt commit);marker-lint OK(117/268);utf8-boundary-hygiene clean
  • CI:3 红全部 issue2875_input_procsub_child_runs_as_rubash_shell=基线(GNU goldens / Rust Linux / Rust macOS 各一)
  • GNU 对照:wsl /usr/local/bin/bash 5.3.20(1)-release 脚本探针(target/devin-w8-audit/probe3387.sh):50k 算术循环后 parent SELF user=140ms;subshell 内=1ms;comsub=0ms;pipeline=0ms;procsub=0ms——钉的 ≤20ms 且 <parent 阈值与 GNU 实际量级吻合,lastpipe 累计面与 GNU 当前 shell 语义一致

F 点:无。(fmt 红为机械卫生项,非机制问题)

续作记录:无。

未跑/VOID 的腿:无。

caomengxuan666 and others added 2 commits November 15, 2026 10:25
…line (Refs #3387)

Root cause: `times` prints getrusage(RUSAGE_SELF) + getrusage(RUSAGE_CHILDREN)
of the host process, but the three fork faces run in-process on the shared
rusage — a `( )` subshell, a `$( )`/`${ }`/`<()` body, and a pipeline element
all printed the host's accumulated clocks instead of the near-zero clocks a
true fork would show (GNU builtins/times.def:65-84 reads both rusages in the
forked child; execute_cmd.c:1576 execute_in_subshell, subst.c:7143
command_substitute, subst.c:6362 process_substitute, execute_cmd.c:2620
execute_pipeline).

Fix: a thread-local baseline stack in read_helpers.rs (same thread_local
idiom as profiler.rs). Each fork face snapshots the four raw counters at
entry — CloneBoundary via ShellState::clone_for_child_save (covers the
flat `( )` regions, the in-place comsub fast paths, and the fresh-shell
child script), PipelineStage via an RAII guard at the top of
execute_pipeline_stage, ComsubBody via guards around the `$( )`, `${ }`
and `<()` body executions — and process_times() reports the current
counters minus the innermost baseline (saturating). Pops are conditional
on (kind, depth) so a restore belonging to another boundary can never eat
an outer face's baseline. The `time` keyword is untouched (it reads
rusage_counters() differentially). The lastpipe last element never comes
through execute_pipeline_stage, so its `times` keeps the accumulated
reading (execute_cmd.c:2758, negative control in the nail).

Regression nail: tests/issue3387_times_fork_faces_regressions.rs — one
shared 50k-iteration parent burn; subshell/comsub/pipeline/procsub SELF
user must be <= 20ms and below the parent, lastpipe last stage must stay
accumulated (> 20ms).

UNVERIFIED-BY-CAPTAIN: no local build or test run was possible in this
round (local builds prohibited by the EDQUOT write wall; Simular box disk
100% full; old-server ssh banner-dead; X299 direct lane not yet open).
Verification legs (new nail stem, full lib vs base, RUSTFLAGS="-D warnings"
cargo check) are owed to the captain review round on X299.
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