Skip to content

fix(executor): undo partial binds on mid-list redirect failure (Refs #3733) - #3853

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3733-redirect-mid-list-undo
Nov 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3733-redirect-mid-list-undo

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #3733

  • Issue fd: redirect failure mid-list does not undo partial binds (GNU undo_partial_redirects) #3733: a redirect list that fails part-way must undo every bind the earlier entries already applied. GNU registers a per-entry undo save in add_undo_redirect (redir.c:1326, kernel fcntl (fd, F_DUPFD, SHELL_FD_BASE) at redir.c:965/1082/1144) and replays them via undo_partial_redirects (execute_cmd.c:855) when do_redirections returns an error.
  • Root cause: rubash's compound/function scopes accumulate SavedNumberedFd/SavedOutputFd snapshots for scope-end restore, but open_compound_numbered_input_redirects (command_input_scope.rs) dropped the accumulated saved vector on the _in error path — so { :; } 8<f9 9<f8 7</non/nope left fd 8 reading f9 and fd 9 reading f8. The output wrapper already unwound on error; the input wrapper now mirrors it.
  • Same hole in the same-exe script face's segmented walk bind_script_numbered_inputs_and_fd_vars (trap_exec.rs): its Err arm now restores the numbered input/output snapshots and the {var} close records the walk performed, in the scope's outputs → inputs → dynamic unwind order. (That face is only reachable from library hosts — owns_process_image == false — so no CLI reproducer; the arm is mechanism-aligned with the in-shell fix.)
  • Pin: tests/issue3733_redirect_mid_list_undo.rs — 49 cells, #![cfg(unix)], covering every compound face (while/until/for/if/case/select/brace/inverted/time/nested/function-def), fd 0/3/8/9/12, heredoc binds, {var} close/dup composition, move/close entries, output/append/<> binds, and controls.

GNU basis

  • execute_cmd.c:854-856 — compound/control commands run do_redirections (..., RX_ACTIVE|RX_UNDOABLE) and call undo_partial_redirects() on failure.
  • redir.c:1326 add_undo_redirect — undo save = kernel dup of the displaced descriptor; undo list is LIFO.
  • execute_cmd.c:5608 area — builtin/function redirections get the same undoable treatment.
  • Verified against WSL GNU Bash 5.3.20 (/usr/local/bin/bash) from script files: { :; } 8<f9.txt 9<f8.txt 7</non/nope leaves fd8=f8a/fd9=f9a; rubash pre-fix left them swapped.

Adjacent residual (not fixed here — separate face)

  • Rubash applies numbered INPUT binds before OUTPUT binds (two passes), while GNU's do_redirections (redir.c:237) is one ordered walk. Observable only as open side-effect ordering: { :; } 8>newfile 7</non/nope — GNU creates newfile (the > open ran before the failure); rubash does not (input pass fails first). FD-table state after the failure is correct in both. Fixing this needs interleaved input/output binding — architectural, out of 1.5.1 scope.
  • On the script face, {var} ambient binds/var-values from apply_dynamic_fd_var_redirects are undone by undo_child_fd_var_redirects's own records; this change restores only what the segmented walk itself accumulated.

Validation

  • Pin fail-first: 28/49 cells red on the unfixed base (3d8c9ff8f), all 49 green with the fix — WSL Linux native cargo test (rustc 1.98.1, /root/.cargo), CARGO_TARGET_DIR=target-wsl.
  • Sibling fd stems all green in WSL: issue3706/3725/3726/3727/3728/3729/3730/3731/3732 + issue1593/1392/1313 redirect stems (0 failures).
  • lib 双边: head 1274 passed / 0 failed / 4 ignored; base 1274 / 0 / 4 — 差集空.
  • RUSTFLAGS="-D warnings" cargo check: clean. rustfmt --check on touched files: clean. check-marker-literals.py: OK. check-utf8-boundary-hygiene.sh: clean.
  • GNU script-file comparison (WSL bash 5.3.20 vs target/debug/rubash.exe): c1/c2/c3 compound faces + h1-h4 output/fd0/dupclosed shapes — fd-state and file-creation side effects byte-identical post-fix.

Test plan

  • cargo test --test issue3733_redirect_mid_list_undo (WSL native): 49/49
  • fail-first on base: 28 red
  • sibling fd stems green
  • lib 双边差集空
  • -D warnings / rustfmt / marker-lint / utf8-hygiene
  • GNU 5.3.20 script-file oracle comparison

Generated with Devin

…3733)

GNU's do_redirections registers an undo save per entry
(add_undo_redirect, redir.c:1326 — kernel fcntl F_DUPFD saves) and
undo_partial_redirects (execute_cmd.c:855) replays them when a later
entry fails, so a failed list leaves the fd table exactly as found.

Rubash's compound/function scopes already accumulate
SavedNumberedFd/SavedOutputFd snapshots for scope-end restore, but
the numbered-input wrapper dropped them on the _in error path:
`{ :; } 8<f9 9<f8 7</non/nope` left fd 8 reading f9 and fd 9 reading
f8. Restore the accumulated snapshots before propagating, mirroring
the output wrapper's existing unwind.

The same-exe script face's segmented walk
(bind_script_numbered_inputs_and_fd_vars) had the identical hole:
its Err arm now restores the numbered input/output snapshots and
the {var} close records the walk performed, in the scope's
outputs->inputs->dynamic unwind order.

Pin: tests/issue3733_redirect_mid_list_undo.rs (49 cells, cfg(unix);
28 red on the unfixed base, all green with the fix; WSL cargo test
native, siblings issue3706-3732 + redirect stems green).

Generated with [Devin](https://devin.ai)

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

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】h1 续作完成 — 建议:PASS(待队长终审)
head: 2dea6fe73 base(merge-base): 3d8c9ff8f 文件: 3 个 +919/-39
关联 issue: #3733(open)

机制核查:

  • GNU undo_partial_redirects(execute_cmd.c:855)+ add_undo_redirect 内核 dup 保存(redir.c:1326);输入 wrapper Err 臂补齐与输出 wrapper 一致的 snapshot restore;script face 分段 walk 同法。
  • 无顺手无关改动;无桥接/翻译层。

腿:

  • 钉自跑:issue3733_redirect_mid_list_undo 49/49(WSL 原生)
  • fail-first:base 28 红/21 绿,红集 = 全部 mid-list 失败 cell
  • lib 双边:head 1274/0/4 vs base 1274/0/4,差集 ∅
  • -D warnings:0;fmt/marker-lint/utf8:clean
  • GNU 对照:bash 5.3.20 脚本文件,c1-c3 + h1-h4 全一致

相邻残差(未扩 scope):输入/输出两遍绑定 vs GNU 单次 walk 的副作用顺序差(文件创建时机),fd 状态两侧正确,建议另立 issue。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】i1 审计 — 建议:PASS(#3853)

PR #3853 第1轮 — 建议:PASS
head: 2dea6fe73fc4c49afd318b83bbff22ce5826f2ef base(merge-base): 3d8c9ff8f745f288b48c5f9d1a6c81b79b0890d0 文件: 3 个 +919/-39
关联 issue: #3733(open,我核过的时间:2026-11-17 23:55 +0800)

机制核查:

  • GNU 侧:execute_cmd.c:854-856 复合/控制命令走 do_redirections(..., RX_ACTIVE|RX_UNDOABLE),失败即 undo_partial_redirects();redir.c:1326 add_undo_redirect 的 undo 存档是被顶替 fd 的 kernel dup(fcntl(F_DUPFD),redir.c:965/1082/1144),undo list LIFO;execute_cmd.c:5606-5608 一带 builtin/function 重定向同径。
  • Rust 侧:command_input_scope.rs 的 open_compound_numbered_input_redirects_in 错误臂补 restore_compound_numbered_input_redirects(saved)(此前 _in 失败丢弃已累积 SavedNumberedFd 快照 → fd8/fd9 残留错绑);trap_exec.rs 的 bind_script_numbered_inputs_and_fd_vars Err 臂按 scope 的 outputs→inputs→{var} close 记录顺序恢复。与 GNU per-entry undo 机制对齐,属根因修复非个案补丁。
  • diff 无顺手改动;src/lexer/continuation.rs 未触碰(gh pr diff --name-only 已核,仅 3 文件)。

腿(逐项数字):

  • 钉自跑:issue3733_redirect_mid_list_undo 49/49 绿(WSL Linux 原生 cargo test,rustc 1.98.1,head 2dea6fe73,RC=0)
  • fail-first:base 3d8c9ff8f 上 21P/28F,红名 28 个全部为 mid-list 失败格:{bind_from_other_fd_then_failure, brace_group_two_partial_binds_both_restored, case_compound_mid_list_failure_restores_fd8, close_entry_before_failure_restored, close_then_failure_reopens_fd, devnull_output_then_failure, elif_compound_mid_list_failure_restores, failure_then_clean_compound, fd3_partial_bind_restored, fd_var_close_before_failure_restored, fd_var_dup_before_failure_restored, first_failure_aborts_walk, for_loop_mid_list_failure_restores_fd8, function_def_mid_list_failure_restores, high_fd_partial_bind_restored, if_compound_mid_list_failure_restores_fd8, intervening_successful_compound, inverted_compound_mid_list_failure_restores, loop_heredoc_plus_failure_restores, missing_dir_append_failure_restores, move_partial_bind_restores_source, multiline_brace_mid_list_failure_restores, nested_compound_inner_failure_restores, select_loop_mid_list_failure_restores_fd8, three_partial_binds_all_restored, time_prefixed_mid_list_failure_restores, until_loop_mid_list_failure_restores_fd8, while_loop_mid_list_failure_restores_fd8} — 与 PR 声称 28 red 一致
  • lib 双边(WSL Linux 原生):head 1274P/0F/4I / base 1274P/0F/4I / 失败名差集 ∅
  • -D warnings:cargo check --lib clean(0 警告);--all-targets 口径下 tests/issue3806_procsub_input_rebind_body.rs run_driver dead_code 为 merge-base 既有基线,与本 PR 无关
  • 卫生:rustfmt --check 3 个触碰文件 clean;check-marker-literals.py OK(118 consts / 269 allowlist);check-utf8-boundary-hygiene.sh clean
  • CI(run 44099350129):红 = census + goldens(已知基线集);Rust tests (macOS) 15min 后 cancelled、零 steps 零日志 → 基础设施取消,无法归因到具体测试;注意该钉 #![cfg(unix)] 在 macOS CI 本应执行,Linux 原生已覆盖同语义,残留为平台面缺口
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release vs WSL 构建 target/debug/rubash(head),脚本 scratch-i1/3733-cmp.sh,8 形状(brace/while/output/ordered/dup/close/if/heredoc):fd-table 恢复结果全部一致(仅诊断行内 shell 名前缀差异)。

分歧/残差(如实上报,不阻塞本 issue 的 fd-table 语义范围):

  • R1(PR 已披露的 adjacent residual,已独立复现确认):rubash numbered 重定向为 input/output 两阶段绑定,GNU do_redirections(redir.c:237)是单次有序 walk。可观测差异仅在 open 副作用顺序:{ :; } 8>o8.txt 9>o9.txt 7</non/nope GNU 先建 o8/o9 再失败,rubash input pass 先败 → 两文件均不建;{ :; } 8>newfile 7</non/nope 同理(GNU 建 newfile / rubash 不建)。fd-table 终态两边一致。PR 正文「GNU basis」节已如实披露;但同文 Validation 节「file-creation side effects byte-identical」的表述偏宽——按本探针,含 output-before-failing-input 序的形状并不 byte-identical。issue fd: redirect failure mid-list does not undo partial binds (GNU undo_partial_redirects) #3733 语义范围是「undo the binds / leave the earlier fd untouched」(fd-table),不含 open 副作用顺序;修复需交错 input/output 绑定,属架构面,超 1.5.1 范围。建议队长终审后为该残差单开跟踪 issue。
  • R2(次要,疑似基线未单独归因):heredoc 形状中重定向失败诊断行号 GNU line 2 vs rubash line 1;本 PR 未触碰诊断行号逻辑。

F 点:无

未跑/VOID 的腿:无(macOS CI 腿以 WSL Linux 原生测试替代,已在 CI 条注明)

续作记录:本轮未改代码(审计-only)。

独立复核备注:h1 lane 已有 PASS 建议评论,本报告为 i1 独立审计结论,两者一致。

Generated with Devin

@caomengxuan666
caomengxuan666 merged commit cbe91f8 into master Nov 17, 2026
9 of 12 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】i1 批次汇总 — i1 波 3 条 g1 修复 PR 审计完毕

阻塞项:无。CI 侧 census+goldens 为已知基线红;#3853 的 macOS job 为 infra cancelled(零日志不可归因),钉为 cfg(unix),Linux 原生已覆盖。

Generated with Devin

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.

fd: redirect failure mid-list does not undo partial binds (GNU undo_partial_redirects)

1 participant