Skip to content

fix(executor): net bind saves displaced fd via kernel dup (Refs #3726) - #3740

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/3726-net-bind-fd-restore
Nov 17, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/3726-net-bind-fd-restore

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

fix(executor): net bind saves displaced fd via kernel dup (Refs #3726)

根因

bind_simple_command_net_inputs(rubash#2124 的 in-process net bind,builtin/function 路由)的 undo save 只做了被顶掉的 fd-table entry 的裸 Rc clone;随后 bind_net_input_fd 的 open_input/open_output 把 /dev/tcp|udp 载体绑到该 fd 上,retire_read_arm/retire_write_arm(rubash#3213)把被顶掉的描述符 retire 掉;dispatch 结束后 restore_simple_command_net_inputs 又把 retired entry 插回表里——下一次 read 见 EBADF,下一次 spawn 的 pin pass 会把回收了该内核号的描述符 dup2 到该 fd 上。与 #3706 同一 hazard,发生在网络重定向面上。

修法(按机制,不打地鼠)

GNU 的 undoable redirect 在 bind 之前用内核 dup 保存被顶掉的描述符:fcntl (fd, F_DUPFD, SHELL_FD_BASE)(redir.c:965 input、:1082 duplicating、:1144 move;SHELL_FD_BASE=10,redir.c:86),add_undo_redirect 记录,undo_redirections 再 dup 回来;compound 组跑 do_redirections (..., RX_ACTIVE|RX_UNDOABLE)(execute_cmd.c:854)。

  • src/executor/fd_table.rs(+107):FileFd::duplicate(unix F_DUPFD_CLOEXEC / Windows duplicate_handle)、FdEntry::dup_file_endpoints(File/Coproc 端点逐个 dup,<> 双臂共享一个 dup 以保持共享语义)、FdTable::saved_entry。本 hunk 与草稿 PR fix(executor): restore fd N after group redirect (Refs #3706) #3723 的 fd_table.rs hunk 逐字节相同;两支无论谁先合入,后合的一方 rebase 时丢掉重复 hunk。
  • src/executor/builtin_redirects.rs(+38/−4):bind_net_input_fd 改为经 saved_entry 快照并 fallible(Result<(), std::io::Error>);dup 失败时按 GNU REDIRECTION_ERROR 处理——已绑的按 undo_partial_redirects(execute_cmd.c:855) unwind,然后像 refused connect 一样报诊断 + status 1、body 不跑。调用方只有 bind_simple_command_net_inputs(codegraph + rg 双核),command_dispatch.rs 不用动。
  • tests/issue3726_net_bind_fd_restore.rs(新,8 cells):fd 8 原语、fd 0 winner、fd 3、<> 双臂存活、双 numbered bind、Linux-only /proc 面、plain-builtin 控制、<> close 共享 guard;listener harness 沿用 [bug] 内建命令的 /dev/tcp 胜出输入重定向在 Linux 上从不连接且静默无诊断 #2124 形状。

本 PR 删除了什么

无。只改了 undo save 的快照方式(裸 clone → 内核 dup)与 bind_net_input_fd 的签名(() → Result<(), std::io::Error>);retire_* 机制、restore_simple_command_net_inputs、dispatch_net_bound_fds 均未动;未碰 src/lexer/continuation.rs,未改 docs/api-ledger.md(无新公开面),未引入 trust/签名/加载期校验机制。

验证

UNVERIFIED-BY-CAPTAIN:本车道无任何构建槽(Simular box 钱包空 hibernated、X299 与老服务器同断、本 VM 禁本地构建),以下腿全部 OWED,由队长/Win-agent 在路线恢复后补跑,见 ~/workspace/fix-3726/REPORT.md §7:

  1. 钉 fail-first(base e3a7b7b3):cells 1–6 红,cells 7–8 绿。
  2. 钉 head-green:8 cells 全绿(Linux 上含 /proc cell)。
  3. 邻茎(fd/redirect/net-bind 家族)。
  4. 全量 lib head-vs-base 逐数对齐(先 build --bin rubash)。
  5. RUSTFLAGS="-D warnings" cargo check --locked --lib --bins。
  6. cargo fmt --check(本车道已用 rustfmt --check 过 3 个改动文件:clean;hygiene 两脚本 clean)。
  7. 跨靶 cargo check --tests(改动含 #[cfg(unix)] fcntl 分支与 #[cfg(windows)] duplicate 分支)。
  8. 合入前 update-branch 到当时 tip 并重核守卫表(§见 REPORT §5:fix(redirect): append funnel resolves the node's byte spelling (Refs #3372) #3565 hunks 距本站点 ≥154 行、fix(mapfile): run -C callbacks on mapfile's own input stream (Refs #3660) #3685 距 fd_table.rs hunk ≥176 行,均无重叠;fix(executor): restore fd N after group redirect (Refs #3706) #3723 的 fd_table.rs hunk 与本 PR 相同,需按合入顺序处理)。

守卫(fresh 扫描,PR 开时)

同文件 open PR 仅 #3565(builtin_redirects.rs hunks :266、:714/:748/:801)与 #3685(fd_table.rs hunk @@-43)、#3723(fd_table.rs 相同 hunk,见上)。在飞车道文件面无重叠。

bind_simple_command_net_inputs' in-process net bind saved the
displaced fd-table entry as a bare Rc clone; the bind's open_input /
open_output retired that descriptor (retire_read_arm /
retire_write_arm, rubash#3213), so restore_simple_command_net_inputs
re-inserted a retired entry and the next read saw EBADF (same hazard
as #3706, on the network-redirect face). The undo save is now a
kernel dup via FdTable::saved_entry / FdEntry::dup_file_endpoints
(GNU redir.c:965/1082/1144 fcntl (fd, F_DUPFD, SHELL_FD_BASE);
execute_cmd.c:854 RX_UNDOABLE); a dup failure fails the whole
redirect list like a refused connect (diagnostic + status 1, body
never runs; binds already made are unwound, execute_cmd.c:855).

NOTE: the fd_table.rs hunk (FileFd::duplicate /
FdEntry::dup_file_endpoints / FdTable::saved_entry) is byte-identical
to draft PR #3723's; whichever merges second drops the duplicate.

UNVERIFIED-BY-CAPTAIN: no build slot existed in this lane (see
REPORT); pin fail-first / head-green / lib / -D warnings legs OWED.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3740 第1轮 — 建议:FAIL
head: e56b821 base(merge-base): e3a7b7b 文件: 3 个 +462/-4
关联 issue: #3726(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

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

  • 钉自跑 issue3726_net_bind_fd_restore:Windows head 5 passed/2 failed(<> 两格红);Linux head 6 passed/2 failed(同两格红,含 Linux-only external_child_sees_restored_fd8 绿)。
  • fail-first:Linux base e3a7b7b 上 7 failed/1 passed——external_child_sees_restored_fd8、net_bind_restores_fd0/fd3/fd8、two_numbered_net_binds_restore_both 及两 <> 格红,plain_builtin_leaves_fd8_alone 绿。真 bug 已复现(read: 0: read error: Bad file descriptor)。注意:正文声称 "cells 1–6 红,7–8 绿",实测 <> close 守卫格在 base 也红(但红在 connections 断言,非 EBADF)。Windows base 上钉同 2 格红——retire_* 为 #[cfg(unix)] no-op(fd_table.rs:1071-1075),Windows 不触发本 bug(平台限)。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 失败名集 {同} / 差集 {}(本机环境绑,head/base 相同)。
  • -D warnings:cargo check --locked --lib --bins clean,0 新警告。
  • 卫生:rustfmt --check 3 文件 clean;marker-lint OK (117 registry consts);utf8-hygiene clean。
  • CI:红 4 个——Rust tests、Rust tests (macOS)、GNU regression goldens(三者仅 issue2875_input_procsub_child_runs_as_rubash_shell,基线签名)、niubash builds(Session::cwd ledger 缺口,master tip 8100d76 同红,跨仓漂移基线)。均非本 PR 引入。
  • GNU 对照(WSL /usr/local/bin/bash 5.3.20(1)-release):exec 8<>rw.txt; true 8<>/dev/tcp/127.0.0.1/$PORT → GNU r1=0 l=[x] r2=0、rw.txt=x\nafter、CONNS=1;本 PR Linux head 输出逐字节相同但 CONNS=2。

F 点:

  • F1(钉含永不绿格,合入即落地常红):net_bind_readwrite_pair_survives(:217)与 net_bind_readwrite_close_input_closes_both(:316)断言 connections_expect(1)==1。该期望对 GNU 正确(实测 bash 5.3.20 单 socket O_RDWR = 1 conn),但 rubash <> net bind 在 base 与 head 均产生 2 个 TCP 连接——这是早于本 PR 的 <> 双连 diverge(非 fd: /dev/tcp|udp bind restores a retired entry (same hazard as #3706) #3726 账,本 PR 的 bind_net_input_fd 自身只开一个 socket 绑双臂,第二连来自另一路径)。两格在 head 与 base 同红 = 对本修复而言的半废格(语义断言已过,红点落在出界断言上)。建议二选一:(a) 两 <> 格摘掉 connections_expect 断言(fd-restore 语义断言保留、已验证与 GNU 一致),双连问题另开 issue;(b) 把 <> 双连作为本单/姊妹单的修复面一并修。按现状合入会往 master 测试套落地 2 个永久红格。

未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3740 第2轮 — 建议:PASS
head: a383836 base(merge-base): e3a7b7b 文件: 3 个 +454/-14
关联 issue: #3726(状态:open,我核过的时间:2026-11-16 21:41 CST)

机制核查:

  • 上轮 F1 判定:<> 双连是真实产品缺陷但属出界面——GNU 对 <> net bind 开单个 O_RDWR socket(CONNS=1,bash 5.3.20 实测),rubash base+head 均 CONNS=2,第二连来自与本 PR 无关的存量路径。钉格断言本身 GNU-correct,但把出界缺陷断言钉进 fd: /dev/tcp|udp bind restores a retired entry (same hazard as #3706) #3726 钉会让两格永久红。按审计选项 (a) 处置:摘除两格的 connections_expect 断言、保留全部 fd-restore 语义断言,并另开 issue net bind <> opens two TCP connections where GNU opens one #3798 跟踪双连缺陷。
  • 本轮 diff 仅 tests/issue3726_net_bind_fd_restore.rs(-10/+8):两处 assert_eq!(connections_expect(1),1) 替换为注释说明;产品代码零改动。

腿(逐项数字):

  • 钉自跑 head a383836:Windows cargo test --test issue3726_net_bind_fd_restore → 7 passed / 0 failed;WSL Linux(/root/rubash-wsl-t2 @a38383678)→ 8 passed / 0 failed(含 Linux-only external_child_sees_restored_fd8 与两枚修复后 <> 格全绿)
  • fail-first:上轮已核 base e3a7b7b Linux 7 failed/1 passed(fd-restore 真 bug 复现 read: 0: read error: Bad file descriptor);本轮删的是出界断言——net_bind_readwrite_pair_survives 在 base 仍于 stdout 断言先红;net_bind_readwrite_close_input_closes_both 在 base 转绿属预期(该格 doc 自述为 sharing 守护格、非 fail-first 格,base 上写失败同样 rc=1)
  • lib 双边:上轮 head/base 失败名集同为 {api::consumer_tests::issue2084_session_end_restores_host_process_state},差集 {};本轮仅改 tests/ 文件,lib 不受影响
  • -D warnings / 卫生:钉文件 rustfmt --edition 2021 --check OK;cargo test 编译 0 警告;marker-lint/utf8 上轮已净(src 未动)
  • CI:新 head a383836 的 checks 尚未注册(push 后待跑);旧 head e56b821 的 CI 红 = {Rust tests、Rust tests (macOS)、GNU regression goldens(仅 issue2875 签名,基线)、niubash builds(Session::cwd ledger 跨仓基线)}
  • GNU 对照:上轮 WSL bash 5.3.20 已核——exec 8<>rw.txt; true 8<>/dev/tcp/… GNU r1=0 l=[x] r2=0、rw.txt=x\nafter、CONNS=1;本 PR Linux head 除 CONNS=2(已另立 net bind <> opens two TCP connections where GNU opens one #3798)外逐字节一致

F 点:无(上轮唯一 F1 已按 (a) 处置)

续作记录:

未跑/VOID 的腿:新 head CI 待跑完核对;其余无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3740 第3轮 — 建议:PENDING(产品面已被 master 覆盖,合并需队长裁定;非代码缺陷)
head: a383836 base(merge-base): e3a7b7b 文件: 3 个 +454/-14
关联 issue: #3726(状态:open;核于 2026-11-17 本地)

本轮新发现(决定性):本 PR 的产品修复已被 master 上的 #3728 落地,剩余价值仅钉文件。

证据:

  1. master(9defebd) 的 bind_simple_command_net_inputs/bind_net_input_fd(src/executor/builtin_redirects.rs:495-590)已是同一机制:fd=0 winner 与 numbered 两路均 if let Err(error) = bind_net_input_fd(...) → restore + net_input_bind_failure + Err;签名 -> Result<(), std::io::Error>;save 经 fd_table.saved_entry(fd)?(内核 dup)。注释锚点同为 rubash#3728 / redir.c:965。
  2. master 的 readwrite 臂用 open_readwrite 单描述符安装(heredoc: 同 fd 先 heredoc 后普通文件重定向时顺序不生效(exec 14<<A 14>file,GNU 后者胜) #3655 修复,注释明写 sequential pair 会 end write-only)——比本 PR 的 open_input+open_output 序贯对更新。若按本 PR 产品码合入会回退该修复。
  3. fd_table.rs 三个 helper(FileFd::duplicate :236 / dup_file_endpoints :400 / saved_entry :1243)master 已有同名同签名实现(fix(executor): dup-save function-call fd 0 before stdin binds (Refs #3727) #3739 e1e3d7b 已合入)。
  4. 钉在 master 上已全绿:本机 Windows cargo test --locked --test issue3726_net_bind_fd_restore(钉文件原样放入 origin/master 9defebd)→ 7 passed / 0 failed,含全部原 fail-first 格(fd0/fd3/fd8、two_numbered、两枚 <> 格)。即 fd: /dev/tcp|udp bind restores a retired entry (same hazard as #3706) #3726 的 Windows 可观测面在 master 已修复;Linux-only /proc 格按平台不跑。
  5. 合并预演:git merge-tree origin/master a3838367 → builtin_redirects.rs + fd_table.rs CONTEXT CONFLICT(非纯机械);正确解法只能是「产品文件取 master、仅保留钉文件」——这实质是把本 PR 降级为纯回归钉,属产品语义裁定,按规程 abort 报告。

CI:head a383836 无任何 check-run(gh api check-runs 为空、gh run list --commit 无记录)——push(2026-11-16T13:49Z)未触发 Actions,与 #3724 同批 push(13:56Z)却正常触发形成对照,疑似该次 push 事件丢失。腿不补也不影响本判词:产品面已被覆盖的结论不依赖 CI。

建议处置(供队长裁定,三选一):

腿:钉自跑(head 第2轮 7/7 Win + 8/8 Linux;本轮 master 侧 7/7 Win 复验);fail-first(第1轮 base e3a7b7b Linux 7f/1p,read: 0: read error: Bad file descriptor);lib 双边/警告/卫生(第1/2轮同 head 已净);GNU 对照(第1轮 WSL bash 5.3.20 已核,除 #3798 双连出界面外逐字节一致)。

续作记录:本轮为验证+裁定请求轮,未动代码、未解冲突。
未跑/VOID 的腿:head CI(Actions 未触发,见上);lib 双边等沿用第2轮同 head 证据

… pins-only

The product fix landed on master via #3728 (same saved_entry mechanism,
plus #3655's newer open_readwrite arm); master's builtin_redirects.rs and
fd_table.rs are taken wholesale. What remains is the regression pin
tests/issue3726_net_bind_fd_restore.rs (Refs #3726).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3740 第4轮 — 建议:PENDING(已按第3轮选项(a)降级为纯钉 PR;合并与否仍需队长裁定)
head: 7ef7850 base(merge-base): 969230b 文件: 1 个 +319/-0
关联 issue: #3726(状态:open,我核过的时间:2026-11-17 10:15 CST)

本轮处置:执行第3轮给出的选项 (a)——merge origin/master 解冲突时产品文件(builtin_redirects.rs、fd_table.rs)全取 master 侧,仅保留 tests/issue3726_net_bind_fd_restore.rs。产品修复已由 master #3728(+ #3739 helper、#3655 open_readwrite)落地,本 PR 现为纯回归钉,diff vs merge-base 仅钉文件 +319/-0。

机制核查:

腿(逐项数字):

  • 钉自跑:issue3726_net_bind_fd_restore — Windows 7/7;WSL Linux 原生(/root/rubash-v3 同 head)8/8(含 Linux-only /proc 外部子进程可见性格)。
  • fail-first:不适用——产品修复已在 merge-base 内(fd: read from /dev/tcp restores a retired fd 0 entry (same hazard as #3706) #3728),钉在 base 上即绿是设计使然而非废钉:钉的价值是防 fd: read from /dev/tcp restores a retired fd 0 entry (same hazard as #3706) #3728 修复回退。第1轮已在旧 base e3a7b7b 验证 fail-first(Linux 7f/1p,read: 0: read error: Bad file descriptor)。
  • lib 双边:产品代码与 master 逐字节相同 → lib 面平凡相等;Windows lib 中 api::consumer_tests::issue2084_session_end_restores_host_process_state 失败已在纯钉分支复验为基线(engine bookkeeping 宿主环境面,与本 PR 无关)。
  • -D warnings:RUSTFLAGS="-D warnings" cargo test --locked --no-run --test issue3726_net_bind_fd_restore Finished 0 警告。
  • 卫生:rustfmt --edition 2021 --check 钉文件 clean(cargo fmt os error 206,逐文件替代并披露);marker-lint OK;utf8 clean。
  • CI(head 7ef7850,run 43998093015):除下述两项红外全 pass——
    • goldens:{issue_753::case_9_declare_indexed_operand_binds_nothing, upstream_suite_slice_snapshots} 与 master 同 job 逐字一致 → 基线;
    • census:38 个失败 stem = master@969230b9 基线 36 + {issue2771_posix_function_tempenv_conversion、issue1902_fd_alias_fact_regressions} 两个 flake stem。归因:本 PR 为纯钉(产品代码=master 逐字节),不可能引入产品回归;issue2771 为 SECONDS 计时抖动(姊妹 PR fix(executor): dup-save procsub spawn-plan fds before the plan bind retires them (Refs #3732) #3741 CI 同 stem 不同格红,WSL 本 head 复跑 5/5 绿);issue1902 fd-alias 为负载敏感 flake。
  • GNU 对照:WSL /usr/local/bin/bash = GNU Bash 5.3.20(1)-release(第1轮已核,见上)。

F 点:无(产品面缺陷已由 #3728 修复,本 PR 无产品代码可判)。

队长裁定项(维持第3轮三选一,本轮已执行 a 的机械部分):

续作记录:

  • 7ef7850 merge: origin/master into fix/3726-net-bind-fd-restore — downgrade to pins-only:builtin_redirects.rs、fd_table.rs 冲突全取 master 侧,仅保留钉文件;新 head: 7ef7850(gh 复核 OPEN/draft/MERGEABLE)。

未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3740 第5轮(o2)— 判词:FAIL(纯钉非判别,不合入;产品面已由 master 覆盖)

head: 7ef7850 base(merge-base): 969230b 文件: 1 个 +319(仅 tests/issue3726_net_bind_fd_restore.rs)
关联 issue: #3726(本轮按 WSL 原生证据处置,见下)

判别力复核(本轮的裁决腿):

产品面(WSL Linux 原生 master 复核,非 Windows 仿真):

#3726 处置:issue 描述的全部形状(read bind 的 fd0/3/8、多 numbered、<> readwrite 两格、udp)在 master 上已修复,fix 由 #3728 + #3655 落地 → 关闭(证据见 issue 评论)。
F 点:上轮唯一 F1(<> 双连断言)已按选项 (a) 移除并另立 #3798,本轮无新增 F 点。

未跑/VOID:Windows 判腿(fd 族 Windows 面为 #[cfg(unix)] no-op,假绿无信息;本轮全部结论取自 WSL 原生构建);head CI(本 PR 不再合入,无意义)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【o2 判词】PASS(纯钉,fd 族 WSL Linux 原生复核)。head 本轮 b8f863211(合 master 后);diff vs master:仅 tests/issue3726_net_bind_fd_restore.rs +319,src/ 0 行。

腿(全部 WSL Linux 原生,非 Windows 仿真 fd table):

CI 归因(本 PR 无 src 改动):Rust tests = master 同一 UTF-8 hygiene 红(src/executor/strmatch.rs:135/136/158/284/285 5 处 byte→char widening,master 文件同文);GNU regression goldens = 基线 issue_753::case_9 + upstream_suite_slice_snapshots + 与本钉无关的 golden_issue1230_exec_redirect_undo;Regression pin stem census = 基线 ~36 stem。niubash builds against this tree 已随 #3837/#1046 配对收尾转绿。

关联 #3726 已 CLOSED(产品面在 master),本钉不关单。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 07:14
@caomengxuan666
caomengxuan666 merged commit b2e488e into master Nov 17, 2026
17 of 24 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