Skip to content

fix(executor): snapshot displaced fd 0 with a kernel dup before read binds it (Refs #3728) - #3738

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3728-read-tcp-fd-restore
Nov 16, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3728-read-tcp-fd-restore

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

read 从 /dev/tcp 读会把一个已 retire 的 fd 0 entry 恢复回去——与 #3706 同一 hazard。瞬时 fd-0 绑定只对用户态 entry 做 Rc clone 保存,绑定时 retire 掉被顶掉的描述符(retire_read_arm/retire_write_arm,rubash#3213),恢复时又把已 retire 的 entry 插回去,于是恢复后的 fd 上的下一次读失败(EBADF),或读到复用了该号的别的描述符。

GNU 的 undo 保存本身就是被顶掉描述符的内核 dup(fcntl (fd, F_DUPFD, SHELL_FD_BASE),redir.c:965/1082/1144;复合组跑 do_redirections (..., RX_ACTIVE|RX_UNDOABLE),execute_cmd.c:854)。本 PR 让每次瞬时 fd-0 保存都做同样的内核 dup。

改动

  • FdTable::saved_entry / FdEntry::dup_file_endpoints / FileFd::duplicate(新增,src/executor/fd_table.rs):F_DUPFD_CLOEXEC(unix)/ 非继承 duplicate(Windows),与 fix(executor): restore fd N after group redirect (Refs #3706) #3723(未合入)为同一机制,此处自包含移植;若 fix(executor): restore fd N after group redirect (Refs #3706) #3723 先合入,其 fd_table.rs hunks supersede 本 PR 的同名 hunks。 <> 两臂共享一个 Rc 的,保存时保持共享,关一侧仍 retire 共享描述符(与 GNU close(2) 关闭整个描述符一致)。
  • read_io.rs 三处(/dev/tcp 臂、Windows-only winuxcmd-fifo 臂、非规则/device 臂):entries.get(&0).cloned() → saved_entry(0);dup 失败则该次 read 失败(GNU 在 F_DUPFD 保存失败时报 REDIRECTION_ERROR)。
  • builtin_redirects.rs bind_net_input_fd:同一 hazard 在 simple-command funnel 的活路——每个 in-process 内建分发都在 body 运行前经此地绑 net 输入,read x < /dev/tcp/... 的 read 内建随后直接用已绑好的端点;只修 read_io.rs 的话该形状的钉仍红。dup 失败时按 connect 失败同路 unwind(恢复已绑部分 + 诊断 + Err)。

回归钉

tests/issue3728_read_tcp_fd_restore.rs(4 cell):plain /dev/tcp fd-0 恢复、exec 0<>file 共享面、numbered read -u 3 ... 3</dev/tcp/...、read x < /dev/null device 臂。预期输出已用 vendored GNU bash 5.3(loopback listener)逐字核过。Windows-only fifo 臂无 Linux 钉,以同一机制覆盖。

本 PR 删除了什么

无。只改保存/恢复路径,不删任何行为、测试或公开面。

UNVERIFIED-BY-CAPTAIN

构建路线当前全断(Simular box 钱包空 hibernated、X299 与老服务器同断、本 VM 禁本地构建),以下腿全部 OWED,由队长/Win-agent 在路线恢复后补跑:

  • 钉 fail-first(在 e3a7b7b 基底上红)与修后绿(cargo test --locked --test issue3728_read_tcp_fd_restore,stem 已核对)
  • 邻茎:issue2124 / issue2098 / issue2102 / issue2121(/dev/tcp 系列)、issue3706(同 hazard 族)
  • 全量 lib 与 base 逐数对齐
  • RUSTFLAGS="-D warnings" cargo check --locked --lib --bins
  • GNU 对照:四 cell 驱动已在 GNU bash 5.3 上逐字跑过(见 REPORT §GNU),修后 rubash 侧待补跑

本地已跑绿:cargo fmt --check(4 文件)、git diff --check、scripts/check-marker-literals.py、scripts/check-utf8-boundary-hygiene.sh。

…binds it (Refs #3728)

`read` from /dev/tcp restored a retired fd 0 entry — the same hazard
#3706 isolated for group redirects. The transient fd-0 binds saved
only an Rc clone of the userspace entry, the bind retired the
displaced descriptor (retire_read_arm/retire_write_arm, rubash#3213),
and the restore re-inserted the retired entry, so the next read from
the restored fd failed (EBADF) or read whatever recycled the number.

GNU's undo save IS a kernel duplicate of the displaced descriptor
(`fcntl (fd, F_DUPFD, SHELL_FD_BASE)`, redir.c:965/1082/1144;
compound groups run `do_redirections (..., RX_ACTIVE|RX_UNDOABLE)`,
execute_cmd.c:854). This makes every transient fd-0 save the same
kernel dup: `FdTable::saved_entry` duplicates every File-backed
endpoint (`FdEntry::dup_file_endpoints`, `FileFd::duplicate`,
F_DUPFD_CLOEXEC on unix, non-inheritable duplicate on Windows),
applied at all three read_io.rs sites (the /dev/tcp arm, the
Windows-only winuxcmd-fifo arm, the non-regular/device arm) — and at
the simple-command funnel's `bind_net_input_fd`, which is the live
path for a plain `read x < /dev/tcp/...`: every in-process builtin
dispatch binds its net inputs there before the body runs, and the
read builtin then serves the already-bound endpoint. 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 redirect (GNU
REDIRECTION_ERROR), unwinding the partial bind like a connect
failure in the funnel and failing the read in read_io.rs.

The three helpers are the same mechanism #3723 (PR, unmerged)
introduces for #3706, ported here so this PR is self-contained on
master; if #3723 lands first, its fd_table.rs hunks supersede these.

Regression pin: tests/issue3728_read_tcp_fd_restore.rs (4 cells:
plain /dev/tcp fd-0 restore, `exec 0<>file` sharing face, numbered
`read -u 3 ... 3</dev/tcp/...`, `read x < /dev/null` device arm).
Expected outputs verified byte-for-byte against GNU bash 5.3
(vendored build) with a loopback listener; the Windows-only fifo
arm has no Linux pin and is fixed by the identical mechanism.

本 PR 删除了什么:无。只改保存/恢复路径,不删任何行为、测试或公开面。

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

队长 FAIL:钉文件编译不过。CI run 43747702011 上 error[E0382] borrow of moved value: tag,位置 tests/issue3728_read_tcp_fd_restore.rs:97:25(let tag = tag.to_string(); 后 tag 又被 move 闭包捕获再借用)——交叉目标 check(darwin、linux/android、Windows 编译)三 job 皆在构建步失败。主机 Rust tests 没抓到它,是因为新钉 stem 不在 CI 的 nextest 表里(只跑选定表),这正是 #2597 的教训:交叉 check 才是钉编译的兜底。产品 hunk 与其余 3 job 的基线红(仅 issue2875)暂不判;返工修钉的 tag 移动/借用后重推,复核时以交叉 check 全绿为准。

The run_driver helper moved `tag` (String) into the spawned thread's
move closure and then borrowed it again in the recv_timeout panic,
so cargo check --tests failed with E0382 on the cross-target legs.
Keep a clone for the timeout message; no assertion or product change.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3738 第2轮 — 建议:PASS
head: 634bf12 base(merge-base): d3f1cce 文件: 3 个 +237/-7
关联 issue: #3728(状态:open,我核过的时间:2026-11-16 18:45 CST)

机制核查:

  • 队长 FAIL 点(钉文件 E0382)已由 ccf3906 修好:tests/issue3728_read_tcp_fd_restore.rs 的 run_driver 先 tag.clone() 再 move,本轮交叉 check 四 job 全绿(见 CI 段)。
  • 本轮返工另做一件事:master 已由 e1e3d7b(fix(executor): dup-save function-call fd 0 before stdin binds (Refs #3727) #3739)把同一 dup-save 机制(FileFd::duplicate / FdEntry::dup_file_endpoints / FdTable::saved_entry)落到 src/executor/fd_table.rs,与本 PR 的同名 hunk 文本冲突。合并 master 时按「上游已有即取上游」解决:fd_table.rs 取 origin/master 版本(代码逐行等价,仅注释措辞不同),本 PR 只保留真正增量——src/executor/read_io.rs 三个 fd-0 快照点与 bind_net_input_fd 改走 saved_entry(内核 dup)。合并提交 634bf12。
  • GNU 锚:请求的 undo 保存就是内核 dup——redir.c:965(输入)/:1082(duplicating)/:1144(move)的 fcntl (fd, F_DUPFD, SHELL_FD_BASE),SHELL_FD_BASE 见 redir.c:86;rubash 的 retire_read_arm/retire_write_arm 在 #[cfg(unix)] 下才真正关描述符(src/executor/fd_table.rs:1060-1075),所以「恢复退役 entry」这条 hazard 只在 Unix 面可复现。保存失败走 REDIRECTION_ERROR 的处理与 GNU 同向(redir.c 的 REDIRECTION_ERROR)。
  • 可疑/无关改动:无。剩余 3 个文件全部围绕 fd-0 快照;bind_net_input_fd 的 Result 化只用于把 dup 失败升级为重定向失败,两处调用点均已接错分支。

腿(逐项给数字与名字):

  • 钉自跑(Windows):issue3728_read_tcp_fd_restore 4/4(test result: ok. 4 passed; 0 failed)。
  • fail-first(Linux,必需腿——本 hazard 的 retire 是 #[cfg(unix)],Windows 上恒绿属平台限制):在 merge-base 前的原始 base e3a7b7b3 上跑同钉 = 1 passed / 3 failed,红名 = {read_from_dev_tcp_restores_stdin_file, read_from_dev_tcp_numbered_fd_restores_fd3, read_from_dev_null_restores_stdin_file},失败原因 read: 0: read error: Bad file descriptor,与 PR 声称的机制一致;第 4 格 read_from_dev_tcp_restores_readwrite_stdin_file(<> 共享面)在 base 上本来就绿——PR 正文「every cell is fail-first」对第 4 格不成立(不是废钉,另 3 格判别有效;已在正文外记录)。head 同钉 Linux 4/4。
  • lib 双边(Windows,先 build --bin):head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base(d3f1cce) 失败名集 {同一条} / 差集 ∅。head 1354 passed 1 failed,base 1354 passed 1 failed。
  • -D warnings:cargo check --lib --bins --test issue3728_read_tcp_fd_restore = 0 警告。--tests 全量会在 tests/issue1794_user_group_completion.rs 报 2 条(unused import / dead fn),该文件本 PR 未触碰(git diff d3f1ccef HEAD -- 为空),属基线。
  • 卫生:rustfmt --check(builtin_redirects.rs / read_io.rs / 钉)= 0;marker-lint = OK(117 consts, 268 sites);utf8-boundary-hygiene = clean;git diff --check 干净。
  • 邻居:issue3655_heredoc_vs_file_redirect 10/10、issue3727_function_call_stdin_fd_restore 12/12(合并 master 的 fd-arm 语义后仍全绿)。
  • CI(head 634bf12,run 43851066025):红 4 项,逐项归因——GNU regression goldens and canaries:仅 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(基线);Rust tests:同一条(基线);Rust tests (macOS):1248 passed / 1 failed,同一条(基线);Regression pin stem census:96/10096 红,与 master@d3f1ccef 的 census 失败集逐条相同(base 96 / head 96,差集 ∅),全部基线;其余 7 job 全绿,其中 Cross-target check (darwin) / Cross-target check (linux/android) / Windows tests compile check 全绿 = 队长 FAIL 点的直接证据。
  • GNU 对照:WSL GNU bash 5.3.20(1)-release(脚本文件形,loopback listener)。4 个形状(read a </dev/tcp + 后续 fd0 读、exec 0<>file 后同形、read -u 3 ... 3</dev/tcp、read </dev/null)→ GNU 输出与本 PR head 逐字节一致(r0=0 a=[sockline] … / r0=1 a=[] …),期望值与我实跑的钉断言相同。平台注:GNU 侧为 Linux;rubash 侧 Windows 与 Linux 都跑过,行为一致。

F 点:无。

续作记录:

  • 634bf12 merge(master): supersede the #3728 fd-table hunks with e1e3d7b2's dup-save port (Refs #3728);新 head: 634bf12。
  • 未改写已推送历史(ccf39060 之上叠加合并提交,非 force-push)。

未跑/VOID 的腿:

  • 83 套上游 suite 全量未跑(时间预算;本 PR 面只有 fd-0 保存/恢复,已用 3655/3727 邻居 + lib 双边覆盖)。
  • macOS 运行时未跑(无 macOS 主机);macOS CI 的 Rust tests 腿已按名归因(仅基线格)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 11:06
@caomengxuan666
caomengxuan666 merged commit 0890647 into master Nov 16, 2026
8 of 12 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-BATCH-SUMMARY】
S4 lane 批次汇总(2026-11-16 19:4x CST,worktree D:/repo/rubash-devin-s4)

本批处置(编号从小到大):

PR 处置 head / 合并 SHA 判词或跳过原因
#3738 已合并 + 关单 #3728 head 634bf12 / squash 0890647 建议 PASS。队长 FAIL(钉 E0382)由 ccf3906 修好,交叉 check 四 job 全绿;本轮另把 master 的 fd_table 同机制 hunk 取上游(合并提交 634bf12)。Linux fail-first 3/4 红 → head 4/4 绿,GNU 5.3.20 四形状逐字节一致;lib 差集 ∅;CI 4 红全为基线(3× issue2875 + census 96 与 master 逐条相同)。
#3737 跳过不合 head 095bd29 CONFLICTING/DIRTY。
#3735 跳过不合 head b33edbf 队长裁断(2026-11-15):同 issue 的先行实现是禁碰车道 #3363,本 PR 暂缓、不并行实现;等 #3363 落地结论后再回到本 PR。
#3734 跳过不合 head 6fd4838 CONFLICTING/DIRTY。
#3724 建议 FAIL head 35f2e80 废钉:钉用 ${THIS_SH} 走 rubash 二进制,而 rubash main 开了真实子进程(main.rs:126),in-process 模拟路径不执行;base 与 head 都是 2/2 绿。PID 探针 parent=2704/child=60052 证真子进程。钉须改建在 library-host/execute_direct_shell_script 上。另:正文缺「删除了什么」节。
#3723 跳过不合 head 4bca3a3 CONFLICTING/DIRTY。
#3718 跳过不合 head 95cba99 CONFLICTING/DIRTY。
#3715 跳过不合 head 2d78c0e 队长裁断(2026-11-16):同 issue 的先行实现 #3362 暂缓,且 #3578 合入门未开,两支都不合入。
#3713 已合并 + 关单 #3574 head c1c3d82 / squash de97682 建议 PASS。产品机制与 GNU cd.def:529-534/common.c:592-624 对齐;本轮只修钉的期望(poison 脚本两行 pwd 诊断),Linux 10/10、fail-first base 7 红、邻居 47 格全绿、lib 差集 ∅、GNU 六形状 stderr 逐字节一致。
#3707 skip(already merged) squash 0c95350 已被并行合并,不重审。

清单外但本 lane 一并处理的:

阻塞项 / 需要队长裁断:

  1. executor: bracket per-process latches at the script-child boundary #3724 的钉必须换宿主(library Session 或 lib 单测)才可能 fail-first;产品修法本身我认为是对的方向。
  2. fix(executor): dup-save procsub spawn-plan fds before the plan bind retires them (Refs #3732) #3741 的 F1 是产品实现层(carrier 端点不该 dup),不是钉问题。
  3. fix(redirect): open output/append FIFO redirects via cwd anchor in bind phase (Refs #3286) #3735 / fix(heredoc): expand heredoc bodies on wordless null commands (Refs #2761) #3715 的合入顺序绑定在禁碰车道(fix(executor): open existing FIFO redirect targets via the cwd anchor in a deleted cwd #3363 / fix(executor): expand heredoc bodies on raw-empty null commands #3362 + heredoc: 限下双 heredoc 同 list 绑定丢失第一条(exec 14<<A 15<<B,#3576 复核带出的存量缺陷) #3578 门),本 lane 不动。
  4. fix(cd): emit GNU's chdir: diagnostic on cd entry in a poisoned cwd (Refs #3574) #3713 的 census 有 8 个旧基底额外红格(issue1902×1 / issue371×5 / issue754×2),本地 base 与 head 单跑全绿,已如实披露供复核。

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