Skip to content

fix(subst,executor): #825 set -T DEBUG trap into $( ) + #826 compound redirect-failure diagnostic via the fd table - #842

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/825-826
Oct 19, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/825-826

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #825, closes #826. Two commits, one per ticket, both audited findings from the #713 nested-context second pass — recorded findings implemented as fixes, not blind patches.

Based on master `992d5c5e` (#831 and #811 already merged). Rebased on fetch at PR time; not merged from my side.


Commit 1 — #825: `set -T` DEBUG trap fires inside $( )

Root cause. The ticket hypothesized a missing functrace-scoped reset variant on the comsub copy path; the operative defect is one layer up: the `false`/`true`/`:` trivial-body status shortcuts in `expand_command_substitution_inner` (src/executor/command_substitution.rs) sat above the functrace+DEBUG disqualification gate, so `x=$(true)` took the hardcoded empty-output short circuit, never entered the substitution child, and GNU's inherited-DEBUG face was lost. (The functrace-scoped reset the ticket pointed at already exists in `reset_for_subshell_inner` and works.) Vendored anchors: bash trap.c:1466-1474 (`reset_or_restore_signal_handlers` clears DEBUG/RETURN in the comsub child only when `function_trace_mode == 0`), subst.c:7143 (`command_substitute` → `parse_and_execute`), execute_cmd.c:4648-4649 (head line for `true`/`false`/`:` too).

Fix. Both shortcuts move below the gate — the same disqualification shape they already carried for xtrace (rubash#254).

GNU differential (WSL GNU bash 5.3.0(1), stdin oracle, byte-exact):

form GNU 5.3.0(1) master (base 992d5c5) this PR
`set -T`+DEBUG; `x=$(true)` `D:x=$(true)` / `D:echo got=$x` / `got=D:true` `got=` identical to GNU
`x=$(false)` `got=[D:false] rc=1` `got=[] rc=1` identical to GNU
`y=$( (true) )` `got2=[D:true]` (one inner fire) `got2=[]` identical to GNU
`z=$(< cf.txt)` `got3=[hi]`, no inner fire (optimize_cat_file) `got3=[hi]` identical (stays on the fast path)
`w=$(true; true)` `got4=[D:true⏎D:true]` `got4=[]` identical to GNU
functrace OFF control `got=` `got=` unchanged
DEBUG armed, `set +T` control `got=` `got=` unchanged
`set -T`, no DEBUG trap control `got= rc=0` `got= rc=0` unchanged

Pre-existing cosmetic divergence left untouched (same on base): rubash's BASH_COMMAND printer re-renders `y=$( ( true ))` as `y=$(( true ))` for the top-level fire; fire set and capture are byte-identical.

Audit probe `debug-into-comsub-functrace-on` green — harness goes 4 known divergences → 3.

Commit 2 — #826: compound's own redirect-failure diagnostic via the fd table, emitted once

Root cause. Exit status was always correct (1); the diagnostic had three faces sharing one mechanism: the brace wrapper's open-failure report bypassed the fd table the command established.

  1. `{ :; } 2>err2.txt > /nonexistent_dir/f` — the stdio arms applied in FIELD order (`redirect_out` before `redirect_err`), so the fd-1 open failed before fd 2 was bound; the swallowed IoError then fell through to a re-dispatch that failed and printed again → doubled on the real stderr, err2.txt never created.
  2. `{ :; } > /nonexistent_dir/f` — same report-then-re-dispatch shape → doubled.
  3. `( { :; } > /nonexistent_dir/f ) 2>err.txt` — the wrapper's raw `eprintln!` bypassed the child's bound fd 2 → captured AND leaked.

GNU anchors (vendored): redir.c:767-955 `do_redirection_internal` applies the redirect list in parse order; redir.c:135 `redirection_error` reports through the fd 2 binding in force.

Fix (fix the reader, per the ticket's suggested direction):

  • `apply_brace_group_redirects` (src/executor/source_type_state.rs): the four stdio arms run in parse order (positions resolved against `command.redirects`). `2>`/`2>>` file arms register their target as the failure-report destination — established at report time with a fresh append-mode open (the arm's create did the single O_TRUNC; append keeps the message at EOF, modeling GNU's shared open file description). No standing fd_table[2] bind: a standing bind was tried first and rejected by the suite — it is visible to the walker's downstream `1>&2` dup, which GNU resolves against the fd 2 in force at the dup's own parse position (issue699 R1.1 `{ … } 1>&2 2>g`; issue726 nested shape).
  • On a failed open: exactly one diagnostic — into the established target, else through `write_default_stderr` (the executor's fd-table fd-2 endpoint; inside a subshell that is the child's own `2>` binding → face 3).
  • `open_compound_output_redirects_inner` (src/executor/command_input_scope.rs): fd-2 FILE bindings open append-mode (fd 1 unchanged) — reports now route through that binding, so writes stay at EOF.
  • `execute_brace_group_pipeline` (src/executor/pipeline_exec.rs): the splice failure is adjudicated once at `finish_brace_redirect_failure` (status 1, ERR trap once, errexit promotion, and-or RHS skip) — no re-dispatch (the doubled emission's second print).

GNU differential (WSL GNU bash 5.3.0(1), stdin oracle, byte-exact):

form (script) GNU stdout / stderr base this PR
face 1: `{ :; } 2>err2.txt > /nonexistent_dir/f; echo rc=$?; echo err=[$(cat err2.txt)]` `rc=1` + `err=[bash: line 1: …]` / stderr empty 2× on console, `err=[]`, no file identical to GNU
face 2: `{ :; } > /nonexistent_dir/f` one stderr line 2× on console identical to GNU
face 3: `( { :; } > /nonexistent_dir/f ) 2>err.txt; echo err=[$(cat err.txt)]` captured, console empty captured AND leaked identical to GNU
`set -E`+ERR: fire count E once, rc=1 E once (via the re-dispatch leaf) E once (wrapper adjudication)
errexit: `set -e` + face 2 dies, no `survived` dies identical
`{ :; } 2>>app.log > /nonexistent_dir/f` captured in app.log lost + doubled identical to GNU
`{ :; } 2>/dev/null > /nonexistent_dir/f` silent, rc=1 leaked doubled identical to GNU
`( … ) 2>/dev/null` silent, rc=1 leaked identical to GNU
`{ { :; } > /nonexistent_dir/f; } 2>outer.txt` captured in outer.txt leaked identical to GNU
`{ echo hi; } > /nonexistent_dir/f` body not run, one line, rc=1 hi absent, doubled identical to GNU
`{ :; } > /nonexistent_dir/f && echo SKIPPED` no SKIPPED, rc=1 SKIPPED suppressed, doubled identical to GNU
`{ echo x >&2; { :; } > /nonexistent_dir/f; } 2>cw.txt` `x` + diagnostic at EOF leaked doubled identical to GNU

Remaining audit-harness delta on the three #826 probes: the diagnostic prolog's script-name namespace rendering (`/d/repo/…` for the Windows binary vs `/mnt/d/repo/…` for WSL GNU) — an environment artifact, present identically on the base; capture destination, single emission and rc are byte-identical.

#831 coordination (per the review rounds)

Verification

  • New regressions: `tests/issue825_comsub_debug_functrace_regressions.rs` (8 cases, one per form), `tests/issue826_compound_redirect_diag_route_regressions.rs` (12 cases, one per form) — all green on this branch.
  • Full `cargo test --no-fail-fast` bidirectional failure-set comparison vs base `992d5c5e`: BASE 4783 passed / 37 failed (301 suites), FIX 4803 passed / 37 failed (303 suites — the +2 are the new files, +20 tests) — zero suites failing only on either side, zero per-suite count diffs, zero regressed tests, zero disappeared failures. The 37 are the same pre-existing environment failures both sides. issue699 7/7 and issue726 9/9 stay green; the `windows_listing_cache` path tests flaked once mid-runs and pass on re-run (known flaky, also noted in the fix(trap): ERR-trap nested-context semantics — collapse suppression, flat-region fire, return attribution (rubash#720) #831 review).
  • `cargo fmt --check` clean. No GPL text copied (original code only; vendored mirror untouched). file:line anchors resolve in the vendored mirror.
  • CHANGELOG `[Unreleased] → 修复` carries one entry per ticket, in the respective commit.

…body shortcuts drop below the functrace gate (rubash#825)

`set -T; trap 'echo D:$BASH_COMMAND' DEBUG; x=$(true)` printed got=: the
false/true/:: status shortcuts in expand_command_substitution_inner sat
ABOVE the functrace+DEBUG disqualification gate, so the hardcoded empty
output never entered the substitution child and GNU's inherited-DEBUG
face was lost entirely (vendored bash trap.c:1466-1474
reset_or_restore_signal_handlers clears DEBUG/RETURN in the comsub child
only when function_trace_mode == 0; subst.c:7143 command_substitute
routes every body through parse_and_execute; execute_cmd.c:4648-4649
fires the head line for true/false/: too).

Move both shortcuts below the gate — the same disqualification shape
they already carried for xtrace (rubash#254). WSL GNU 5.3.0(1) stdin
oracle, 8 forms byte-identical: x=$(true) -> got=D:true; x=$(false) ->
got=[D:false] rc=1; $( (true) ) -> got2=[D:true]; $(true; true) fires
twice into the capture; functrace-off / DEBUG-without--T / -T-without-
trap controls keep their exact prior behavior (the issue's functrace-off
face and the rubash#345 armed-inside face stay green).

Audit probe debug-into-comsub-functrace-on green: the #713 nested-
context harness goes 4 known divergences -> 3 (only the #826 probes
remain, their semantics closed separately).

Known pre-existing cosmetic divergence left untouched: for a top-level
assignment whose body is $( ( … ) ) or $(< …), rubash's BASH_COMMAND
printer re-renders the text (y=$(( true )) vs GNU's y=$( ( true ))).
The fire set and captured substitution output are byte-identical.

Regression: tests/issue825_comsub_debug_functrace_regressions.rs (8
cases, one per form).
…h the fd table, emitted once (rubash#826)

Three faces, one mechanism: the brace wrapper's open-failure report
bypassed the fd table the command established.

- { :; } 2>err2.txt > /nonexistent/f printed the diagnostic TWICE on
  the real stderr and never created err2.txt: the stdio arms applied in
  FIELD order (redirect_out before redirect_err) so the fd-1 open failed
  before fd 2 was bound, and the swallowed IoError fell through to a
  re-dispatch that failed and printed again (faces 1-2).
- ( { :; } > /nonexistent/f ) 2>err.txt captured AND leaked: the
  wrapper's raw eprintln! bypassed the child's bound fd 2 (face 3).

GNU (vendored redir.c:767-955 do_redirection_internal applies the
redirect list in parse order; redir.c:135 redirection_error reports
through the fd 2 binding in force): one diagnostic, in the redirected
target.

- apply_brace_group_redirects: the four stdio arms run in PARSE order
  (positions resolved against command.redirects). The 2>/2>> file arms
  register their target as the FAILURE-REPORT destination instead of
  binding fd_table[2]: a standing binding would be seen by the walker's
  downstream 1>&2 dup, which GNU resolves against the fd 2 in force at
  the DUP's own parse position (issue699 R1.1 { … } 1>&2 2>g regressed
  under the standing-bind variant and is green again here). On a failed
  open the diagnostic is written exactly once — into the established
  target (fresh append-mode open; the arm's create did the single
  O_TRUNC, append keeps the message at EOF like GNU's shared open file
  description) or, with none established, through the executor's
  fd-table fd-2 endpoint (write_default_stderr), which inside a
  subshell resolves to the child's own 2> binding (face 3).
- open_compound_output_redirects_inner: an fd-2 FILE binding opens its
  handle in append mode (fd 1 unchanged) — the wrapper's reports now
  route through that binding, so writes must stay at EOF instead of
  overwriting the file head after body writes.
- execute_brace_group_pipeline: the splice failure is adjudicated once
  at finish_brace_redirect_failure (status 1, ERR trap once, errexit
  promotion, and-or RHS skip) and the command is NOT re-dispatched.

WSL GNU 5.3.0(1) stdin oracle, byte-identical: the three ticket faces,
ERR single fire, errexit termination, 2>> capture, 2>/dev/null silence
(plain and subshell), nested-brace capture, body-not-run, and-or RHS
skip. Audit probes compound-redirect-fail-status / brace-redirect-err-
route / subshell-redirect-diag-route: semantics green (the remaining
harness delta is the $0 script-name namespace rendering /d/ vs /mnt/d/,
an environment artifact present on the base too).

Full cargo test --no-fail-fast failure profile is identical suite-by-
suite and count-by-count to the base commit (37 pre-existing failures
in 9 environment suites on both sides; zero regressed, zero missing;
issue699 7/7 and issue726 9/9 stay green).

Regression: tests/issue826_compound_redirect_diag_route_regressions.rs
(12 cases, one per form).
@caomengxuan666

Copy link
Copy Markdown
Member Author

独立代码评审:APPROVE-WITH-NITS(无 BLOCKER,无需改码;两条 NIT 级备注见文末)

独立评审树 D:/repo/rubash-cr-842(5daa47c4,base 992d5c5)。以下全部实做复核。

#825(3daa7fc2)—— 实现代理对票面假设的反驳成立

  • ✅ 票面假设(comsub copy path 缺 functrace-scoped reset)确不成立:reset_for_subshell_inner 的 functrace 门在 base 就已存在(src/builtins/trap.rs:962,DEBUG/RETURN 仅 functrace==0 时 reset,与 trap.c:1466-1474 对应)。真缺陷确为 expand_command_substitution_inner 里 false/true/: 快捷路排在 functrace+DEBUG 取消资格门之上——x=$(true) 走硬编码空输出短路,从未进替换子壳。
  • ✅ GNU 锚点核对(vendored):trap.c:1466-1474(function_trace_mode == 0 才清 DEBUG/RETURN)、subst.c:7143(command_substitute 全体走 parse_and_execute)、execute_cmd.c:4648-4649(true/false/: 照发头行)逐一属实。
  • ✅ WSL GNU 5.3.0(1) 自跑 oracle 逐字节对比:x=$(true)→got=D:true、x=$(false)→got=[D:false] rc=1、$( (true) )→got2=[D:true]、$(true; true)→双触发;三对照($(<file) 不触发=cat-file 优化、functrace 关→GOT5<[]>、-T 无 trap→无 D)+ $(:)、echo-sub=$(false) rc=0 全部与修复树一致。已知外观分歧(BASH_COMMAND 重渲染 y=$(( true )) vs y=$( ( true )))确认触发集合与捕获内容一致。
  • ✅ 非 functrace 零性能损失:门=两次 env 表查询、无新增 spawn;BASE/FIX 循环冒烟在进程启动噪声内持平。

#826(5daa47c4)—— 重设计裁定成立,常驻 fd_table[2] 的否决有据

  • ✅ GNU 锚点核对:redir.c:767-955 do_redirection_internal(解析序左到右)、redir.c:135 redirection_error(经当时生效 fd 2 报告)属实。
  • ✅ 12 形态 WSL oracle 自跑逐字节对比(仅 prolog $0 命名域差异):三票面(2>err2 单发入档/err.txt 捕获不泄漏)、ERR 单发、errexit 终止、2>> 落 EOF、2>/dev/null 静默(plain+子壳)、嵌套 brace、体不执行、&& 短路、|| RHS 照走、rc=1。
  • ✅ 重设计关键点自证:GNU 实测 { …; } 1>&2 2>g → OUT 落真实 stderr、ERR 落 g(dup 按自身位次当时的 fd 2 解析);2>g 1>&2 反序 → 双双入 g;经典 2>&1 >file、管道内 splice 形同样逐字节一致。常驻绑定版会被 walker 下游 1>&2 看到而出错——否决理由成立,track-destination 方案与 GNU 一致。诊断后 fd 状态无泄漏(下一命令 stderr 正常)。
  • ✅ finish_brace_redirect_failure 与主派发 IoError 臂的既有裁定契约(ast_exec.rs 1590 区:errexit enabled+active、suppress==0、非 inverted、无 and-or → ExitCode(1))逐条同形;failglob 分支保持原契约。
  • ✅ 审计 harness 45 例自跑:BASE 4 分歧 → FIX 3 分歧,残余三条均纯为诊断 prolog $0 渲染域(rubash /d/... vs WSL /mnt/d/...)环境伪差,base 同在。
  • ✅ fix(trap): ERR-trap nested-context semantics — collapse suppression, flat-region fire, return attribution (rubash#720) #831 协调:origin/master == base 992d5c5(fix(trap): ERR-trap nested-context semantics — collapse suppression, flat-region fire, return attribution (rubash#720) #831 已在内),merge-tree 零冲突;ast_exec.rs 本 PR 未触碰。CHANGELOG 的 :1593 探针(set -E; trap…ERR; f() ( x=1; { :; } > /nonexistent/f ))三腿自跑:FIX plain 3 发(体/区域收口/调用)、errexit 形 2 发静默,与 GNU 逐字节一致。

纪律与回归

NIT(不阻塞,无需按此改码)

  • N1 CHANGELOG compound's own redirection-failure diagnostic bypasses the ordered fd table: printed twice, 2>file capture dropped, leaks across a subshell's 2>err.txt #826 条「两形态前后不变」措辞偏宽::1593 探针的 errexit 形在 BASE 上 ERR 只发 1 次(GNU/FIX 为 2 次)——正是本 PR 单点裁定的包装层 ERR 触发把计数收到 GNU 位;plain 形才严格前后不变。方向向好,仅记录。
  • N2 顺带发现(两树一致、非本票范围的预存分歧,可作未来 audit probe):顶层 ! { :; } > /nonexistent/f GNU rc=1(复合重定向失败时顶层取反不生效),rubash rc=0;( ! { …; } > /bad ) 子壳形两树均 rc=0 与 GNU 一致。另 profiler_regressions::fixture_text_tree_matches_normalized_output 在本机两树均随机翻转(同树 3 跑内 FAIL/PASS 皆有),属环境 flake,与本次改动无关。

@caomengxuan666
caomengxuan666 merged commit b79e678 into master Oct 19, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/825-826 branch October 21, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant