Skip to content

fix(executor): kernel-dup the [[ ]] stdio save (Refs #3730) - #3742

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3730-cond-stdio-fd-restore
Nov 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3730-cond-stdio-fd-restore

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3730.

根因

execute_conditional_command_with_redirects(src/executor/command_prepare.rs:386-387)的 scope save 只 Rc-clone userspace FdEntry,随后 bind_conditional_stdio_redirects 绑定 fd 1/2 时 retire 被顶掉的 File 端点(retire_write_arm,rubash#3213)——bind 关闭的正是 save 打算恢复的那个描述符,两处 restore 循环把 retired entry 重新插回表内,下一次写或 spawn 的 pin pass 就 dup2 了内核已回收该号的描述符(fd 1 落到 [[ ]] 重定向的目标文件上,或写操作 EBADF 丢失)。与 #3706(group 面,#3723 修)同一 hazard,在条件命令 stdio 面。

GNU 锚点:execute_cmd.c:4138 SET_LINE_NUMBER (cond_command->line)(条件命令只在自身重定向绑定成功后才 stamp 行号);redir.c:965/1082/1144 fcntl (fd, F_DUPFD, SHELL_FD_BASE)(SHELL_FD_BASE=10,redir.c:86),由 add_undo_redirect 登记供 undo_redirections dup 回去——undo save 本身就是内核 dup。

修法

  • src/executor/command_prepare.rs:save 改为 FdTable::saved_entry(1)? / (2)?(dup 失败即命令失败,对应 GNU REDIRECTION_ERROR;只用 ?,避开 fix(executor): restore fd N after group redirect (Refs #3706) #3723 返工修过的 E0308 坑)。
  • src/executor/fd_table.rs(+111 行):新增 FileFd::duplicate()(unix F_DUPFD_CLOEXEC;Windows 非继承 duplicate)、FdEntry::dup_file_endpoints()、FdTable::saved_entry()——与 fix(executor): restore fd N after group redirect (Refs #3706) #3723 的 helper 逐字一致的移植(fix(executor): restore fd N after group redirect (Refs #3706) #3723 仍未合入;若它先合,本 PR rebase 去重,只留站点改动)。 <> 对的两臂共享一个 Rc,save 保留共享语义。
  • 前提:fd 1/2 出生时是符号型 Stdout/Stderr marker(retire 不碰),hazard 只在 File-backed 时触发(exec 1>file 之后);钉的 cell 均先 exec 打底。
  • 两处 restore 循环、退出路径、bind-error 路径均未动;未碰 src/lexer/continuation.rs;无新公开面(均为 pub(crate)),未改 docs/api-ledger.md。

回归钉

tests/issue3730_cond_stdio_fd_restore.rs(10 cells):fd 1 输出重定向、fd 2 stderr 重定向、close 形式、LOOP 形式、2>&1 dup 形式、双槽形式、offset 共享 guard、&& 列表形式、无重定向 control、Linux-only 外部子进程 /proc 面。除 control 外全部 fail-first 设计。

本 PR 删除了什么

未删除任何功能、测试、公开 API 或文档。仅删除两行裸 clone save(被 kernel-dup save 替代)。

UNVERIFIED-BY-CAPTAIN

构建路线全断(Simular box 钱包空 hibernated、X299 与老服务器同断、本 VM 禁本地构建),以下腿全部 OWED,由队长/Win-agent 在路线恢复后补跑:钉 fail-first(base 上红)、钉修后绿(10 cells)、全量 lib 双边、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins、GNU 对照探针、邻茎。

已绿:rustfmt --check(三文件)、hygiene 脚本(check-marker-literals.py OK;check-utf8-boundary-hygiene.sh clean)、git diff --check、完整 staged diff 人工通读。

REPORT:~/workspace/fix-3730/REPORT.md(不入仓)。

守卫

The [[ ]] stdio save only cloned the userspace FdEntry, so the
bind-time retire (retire_write_arm, rubash#3213) closed the very
descriptor the restore was about to replay, and the next write or
spawn's pin pass used whatever kernel descriptor had recycled that
number (fd 1 lands on the [[ ]] redirect's target file, or the
write is lost with EBADF).

GNU's undoable redirect saves the displaced descriptor BEFORE the
redirect binds, with fcntl (fd, F_DUPFD, SHELL_FD_BASE)
(redir.c:965/1082/1144; SHELL_FD_BASE is 10, redir.c:86),
recorded by add_undo_redirect for undo_redirections (the
conditional stamps its line only once its redirections have
bound, execute_cmd.c:4138 SET_LINE_NUMBER). This makes the scope
save the same kernel dup: FdTable::saved_entry duplicates every
File-backed endpoint (FileFd::duplicate, F_DUPFD_CLOEXEC on unix,
non-inheritable duplicate on Windows), applied at the [[ ]]
stdio save site — the same helper #3723 adds for the group
redirect face (rubash#3706), ported here because that PR is still
unmerged. A <> pair's two arms share one Rc; the save preserves
that sharing so closing one direction still retires the shared
descriptor, as GNU's close(2) closes the whole descriptor. A dup
failure fails the command (GNU REDIRECTION_ERROR) via ?.

The hazard needs fd 1/2 File-backed (at birth they are the
symbolic Stdout/Stderr markers, which no retire touches), so the
pin first runs exec 1>file / exec 2>file.

Regression pin: tests/issue3730_cond_stdio_fd_restore.rs
(10 cells: fd 1 output redirect, fd 2 stderr redirect, close
form, LOOP form, 2>&1 dup form, both-slots form, offset-sharing
guard, && list form, plain-conditional control, Linux-only
external-child /proc face).

UNVERIFIED-BY-CAPTAIN: no build slot was available (Simular
box hibernated, X299 unreachable, local builds forbidden), so
the pin's fail-first run and the verification legs are owed;
see the PR body.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3742 — 跳过(不合,不改代码)
head: 238ef37(gh 实时) 状态:draft / CONFLICTING / DIRTY

处置:按本波规程「DIRTY/CONFLICTING → 跳过不合,报告」,本轮不做全腿审计、不 rebase、不关单。

备注:

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3742 第2轮 — 建议:PENDING(缺腿)
head: 238ef37(gh 实时)state: OPEN draft / CONFLICTING / DIRTY
关联 issue: #3730

处置:维持跳过——draft + CONFLICTING + DIRTY,且同族 #3740(FAIL:两 <> 格 base+head 双红)、#3741(FAIL:saved_entry 对退役 carrier 句柄 F_DUPFD_CLOEXEC)未解,本轮不做全腿、不 rebase、不关单。

fd_table.rs helpers already landed via #3739; take master's version
(the PR's hunk was byte-identical modulo doc comments).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3742 第3轮 — 续作开工
head: 238ef37 → 本地 merge origin/master(969230b) 解冲突中(fd_table.rs helper 已被 #3739 落地,冲突仅为 doc 注释差异,将取 master 侧去重,只留 command_prepare.rs 站点改动 + 钉文件)。完成后跑全腿(含 WSL Linux 原生 fd 复核)再报判词。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3742 第3轮 — 建议:PASS
head: ce0d7b3 base(merge-base): 969230b 文件: 2 个 +273/-2
关联 issue: #3730(状态:open,我核过的时间:2026-11-17 10:15 CST)

机制核查:

腿(逐项数字):

  • 钉自跑:issue3730_cond_stdio_fd_restore — Windows 9/9(external_child_sees_restored_fd1 为 cfg(unix),Windows 不计);WSL Linux 原生(/root/rubash-v3 同 head)10/10。
  • fail-first:WSL Linux base 969230b 原样放钉 → 1 passed / 9 failed,红名 = 除 plain_cond control 外全部 9 格,与 PR 声称「除 control 外全部 fail-first」一致。
  • lib 双边(Windows):head 1368 passed / 1 failed / 4 ignored,失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state};同测试在产品代码与 master 逐字节相同的 fix(executor): net bind saves displaced fd via kernel dup (Refs #3726) #3740 分支上同样失败(已复验)→ 确认为基线,head−base 差集 {}。
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins Finished 0 警告;cargo test --locked --no-run --test issue3730_cond_stdio_fd_restore Finished 0 警告。
  • 卫生:rustfmt --edition 2021 --check 两文件 clean(cargo fmt os error 206,按规程逐文件替代并披露);scripts/check-marker-literals.py OK(118 consts / 268 allowlisted sites);check-utf8-boundary-hygiene.sh clean。
  • CI(head ce0d7b3,run 43998090344):Rust tests、Rust tests (macOS)、Shell functionality、Compatibility CLI、Cross-target darwin + linux/android、niubash builds、API ledger tripwire、Windows tests compile check 全 pass;红仅两项,均已逐名归因——
    • GNU regression goldens and canaries:失败名集 {issue_753::case_9_declare_indexed_operand_binds_nothing, upstream_suite_slice_snapshots},与 master@44bd73074 同 job 失败名集逐字一致 → 基线;
    • Regression pin stem census:36 个失败 stem,与 master@969230b9(本 PR merge-base)census 失败 stem 集合 diff 为空 → 零新增,全部基线。
  • GNU 对照:WSL /usr/local/bin/bash = GNU Bash 5.3.20(1)-release;脚本 target/pr-comments/probe3730.sh(7 形状:1>/2> 重定向、close 形、重定向循环、2>&1、双槽、偏移共享、&& 列表)GNU 全 rc=0、输出还原逐字节一致;rubash head 同脚本结果一致。

F 点:无。

续作记录:

未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】V 波批次汇总(v3 车道:#3742 / #3741 / #3740 / #3737 / #3734)— 核于 2026-11-17 10:15 CST

PR head 建议判词 要点
#3742 ce0d7b3 建议 PASS [[ ]] stdio 保存改内核 dup;merge master 解冲突后全腿绿(Win 9/9 + WSL 10/10,fail-first 1p/9f,GNU 5.3.20 probe 逐字节一致,CI 红全归因基线/flakes)
#3741 d134334 建议 PASS procsub spawn-plan fd 保存改 saved_entry;第3轮 F1(stale FileFd→EBADF)根因=pre-#3723 组面裸 clone,已在 merge-base 由 #3723 根治,原始复现转绿;WSL 11/11 + fail-first 3p/8f;残留「FileFd 不得持死句柄」不变量已在报告中如实记录
#3740 7ef7850 建议 PENDING 已执行第3轮选项(a):降级纯钉 PR(产品面=master 逐字节,#3728 已修 #3726);钉 Win 7/7 + WSL 8/8 绿;合并/关闭/补形状三选一待队长裁定
#3737 — 已合并(记录) state=MERGED,mergeCommit 41ff84c2fe466ea0f76f5c62456aa37ea96bd7ba(gh api 复核)
#3734 — 已合并(记录) state=MERGED,mergeCommit 308e97fc5fabeccc01e5115285d5b5d25b2cc24c(gh api 复核);关联伞 issue #3605 保持 OPEN

阻塞项 / 待队长裁定:

  1. fix(executor): net bind saves displaced fd via kernel dup (Refs #3726) #3740 三选一(合钉 / 关 PR+fixed-by-fd: read from /dev/tcp restores a retired fd 0 entry (same hazard as #3706) #3728 / 指认未覆盖形状)。
  2. fix(executor): dup-save procsub spawn-plan fds before the plan bind retires them (Refs #3732) #3741 残留风险知悉:saved_entry 对 File/coprocess 端点一律内核 dup,若未来再现 stale FileFd 生产者将以 EBADF 中止复合命令——当前钉覆盖形状全绿,建议 fd 族 issue 兜底该不变量。
  3. CI flake 记录:issue2771(SECONDS 计时)、issue1902(fd-alias)、issue371(assoc transform,master@44bd730 同红)在多 PR census 上抖动,均非本批 PR 账;master 自身 goldens 基线红 {issue_753::case_9、upstream_suite_slice_snapshots} 与 census ~36 stem 基线未变。

本 lane 未合入/未批准/未关闭任何 PR 或 issue;三条 draft 均为建议判词,等队长终审。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 02:18
@caomengxuan666
caomengxuan666 merged commit 6d3c749 into master Nov 17, 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