Skip to content

fix(executor): restore fd N after loop heredoc (Refs #3725) - #3737

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3725-loop-heredoc-fd-restore
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3725-loop-heredoc-fd-restore

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

execute_simple_loop(src/executor/alias_loops.rs)在绑定 loop heredoc 前用裸 Rc clone 保存被顶掉的 fd 条目,随后 open_input 的绑定 retire(close)了该 clone 共享的内核描述符(rubash#3213),循环结束后的 restore 又把 retired entry 插回表里——下一次 read <&N 读到 EBADF 或错误目标。这是 #3706 同一 hazard 的 loop-heredoc 面(#3723 修的是 group redirect 面)。

修法

GNU 锚点

  • 循环重定向可 undo:execute_cmd.c:854 do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE);失败 unwind:execute_cmd.c:855 undo_partial_redirects ()。
  • undo 保存即内核 dup:redir.c:965/1082/1144 fcntl (fd, F_DUPFD, SHELL_FD_BASE)。
  • 锚点均在 third_party/bash(Bash-5.3 patch 20,9c465866)上核实。

回归钉

tests/issue3725_loop_heredoc_fd_restore.rs(6 cell):fd 8 主元、fd 3 变体、body 内读 heredoc + 恢复、until 变体、两次循环共享 offset 连续读、无 heredoc 对照(对照在未修基底上亦绿)。

本 PR 删除了什么

什么也没删除。纯加法:fd_table 增加 3 个 helper(+107 行),alias_loops 改保存 + 抽 restore helper(+49/−11 行),新增 1 个测试文件(+163 行)。无公开面新增,无行为删除。

验证状态

UNVERIFIED-BY-CAPTAIN:构建/验证路线当前全断(Simular box 钱包空 hibernated、X299 与老服务器同断、本 VM 无 cargo 且禁本地构建),以下腿全部 OWED,由队长/Win-agent 在路线恢复后补跑:钉 fail-first(基底红)、修后绿、邻茎、全量 lib 双边、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins、GNU 5.3 oracle 对照。已做:6 条期望输出在系统 bash 5.2.21 下作指示性核对(5.3 script-file oracle 亦 OWED);git diff --check 通过;rustfmt 本机无工具链未跑。

execute_simple_loop saved the displaced fd with a bare Rc clone, then open_input retired the descriptor the clone shared, so the post-loop restore re-inserted a retired entry (the #3706 hazard, loop-heredoc face). Save via FdTable::saved_entry — a kernel dup of the displaced descriptor, the same undo-save #3723 introduced for group redirects — and unwind already-bound heredocs if the dup fails (GNU undo_partial_redirects, execute_cmd.c:855).

GNU: loop redirections are undoable (execute_cmd.c:854 do_redirections RX_ACTIVE|RX_UNDOABLE); the undo save is fcntl(fd, F_DUPFD, SHELL_FD_BASE) (redir.c:965/1082/1144). New pins: tests/issue3725_loop_heredoc_fd_restore.rs (6 cells). UNVERIFIED-BY-CAPTAIN: all cargo legs owed, routes down.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3737 第1轮 — 建议:FAIL
head: 2ad2400(= merge 095bd29 + origin/master 5d253cf)base(merge-base): e3a7b7b 文件: 3 个(gh 口径 +308/-11)
关联 issue: #3725(open)

机制核查:

  • alias_loops.rs 的循环 heredoc 绑定改 saved_entry dup + 失败回卷——机制本身与 GNU undo_partial_redirects(execute_cmd.c:855)同向。
  • 但钉覆盖的 while/until N<<< 形状实际走 compound_exec::bind_numbered_heredoc_redirects(fix(executor): restore fd N after group redirect (Refs #3706) #3723 修的那处),不是本 PR 改的 alias_loops 路径——修复面与钉面错位。

腿:

  • 钉自跑:issue3725_loop_heredoc_fd_restore — Windows head 6/6(仿真 fd table 假象);Linux head(WSL rustup 1.99.0 原生构建)1 passed / 5 failed。
  • fail-first:Linux merge-base e3a7b7b 上同样 1 passed / 5 failed,红名与 head 完全一致 = {loop_body_reads_heredoc_then_fd8_restored, loop_heredoc_restores_fd3_file, loop_heredoc_restores_fd8_file, two_sequential_loops_restore_fd8_with_offset, until_loop_heredoc_restores_fd8};stderr 均为 read: 0: read error: Bad file descriptor。钉在 head 上依旧红 = 修复未生效。
  • 交叉验证:同一钉文件放到 fix(executor): restore fd N after group redirect (Refs #3706) #3723 head(4075d34fb)Linux 上 6/6 全绿——证明 fd: loop heredoc bind restores a retired entry (same hazard as #3706) #3725 的形状由 compound_exec 路径承载,fix(executor): restore fd N after group redirect (Refs #3706) #3723 已覆盖,本 PR 冗余且无效。
  • lib 双边(Windows):head {issue2084} / base(5d253cf) {issue2084} / 差集 ∅。
  • -D warnings:cargo check 通过,0 警告。卫生:fmt/marker/utf8 无新增问题。
  • CI(head 2ad2400):goldens 红 = 基线同名两格(issue_753 + upstream_suite_slice_snapshots);census 红含本 PR 自己的 5 枚钉(issue3725_loop_heredoc_fd_restore::*,Linux runner 上复现,与本地 WSL 一致)+ issue371 抖动簇 3 格。
  • GNU 对照:probe-3725.sh 六形状在 Windows head 与 GNU 5.3.20 输出一致——但 Windows 仿真掩盖了 Linux 真 fd 缺陷;Linux head 上同探针形状仍复现 Bad file descriptor(钉 stderr 为证)。

F 点:

  • F1: 修复面错位——src/executor/alias_loops.rs 的 saved_entry 改动不覆盖 while/until 循环 heredoc 的实际绑定点 compound_exec::bind_numbered_heredoc_redirects;复现 printf 'f8a\nf8b\n' > f8; exec 8<f8; while IFS= read -r l <&9; do :; done 9<f9 8<<<"B"; IFS= read -r r <&8 在 Linux head 上 fd8 读 Bad file descriptor,GNU/应然为恢复原文件描述符。建议:要么把 saved_entry+回卷接到 compound_exec 路径(即 fix(executor): restore fd N after group redirect (Refs #3706) #3723 已做的面,会与 fix(executor): restore fd N after group redirect (Refs #3706) #3723 冲突/重复),要么证明 alias_loops 路径另有钉不到的失效场景并补钉。

续作记录:

  • 2ad2400 Merge remote-tracking branch 'origin/master' into fix/3725-loop-heredoc-fd-restore(fd_table.rs 冲突保留 master 侧);新 head: 2ad2400

未跑/VOID 的腿:无(关键腿均已跑,结论为负)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 17:36
@caomengxuan666
caomengxuan666 merged commit 41ff84c 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