Repository navigation
fix(executor): snapshot displaced fd 0 with a kernel dup before read binds it (Refs #3728) - #3738
Merged
Merged
Conversation
…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.
Member
Author
|
队长 FAIL:钉文件编译不过。CI run 43747702011 上 |
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.
…save port (Refs #3728)
Member
Author
|
【WIN-AGENT-AUDIT】 机制核查:
腿(逐项给数字与名字):
F 点:无。 续作记录:
未跑/VOID 的腿:
|
caomengxuan666
marked this pull request as ready for review
November 16, 2026 11:06
Member
Author
|
【WIN-AGENT-BATCH-SUMMARY】 本批处置(编号从小到大):
清单外但本 lane 一并处理的:
阻塞项 / 需要队长裁断:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
根因
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) 关闭整个描述符一致)。entries.get(&0).cloned()→saved_entry(0);dup 失败则该次 read 失败(GNU 在 F_DUPFD 保存失败时报 REDIRECTION_ERROR)。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共享面、numberedread -u 3 ... 3</dev/tcp/...、read x < /dev/nulldevice 臂。预期输出已用 vendored GNU bash 5.3(loopback listener)逐字核过。Windows-only fifo 臂无 Linux 钉,以同一机制覆盖。本 PR 删除了什么
无。只改保存/恢复路径,不删任何行为、测试或公开面。
UNVERIFIED-BY-CAPTAIN
构建路线当前全断(Simular box 钱包空 hibernated、X299 与老服务器同断、本 VM 禁本地构建),以下腿全部 OWED,由队长/Win-agent 在路线恢复后补跑:
cargo test --locked --test issue3728_read_tcp_fd_restore,stem 已核对)RUSTFLAGS="-D warnings" cargo check --locked --lib --bins本地已跑绿:
cargo fmt --check(4 文件)、git diff --check、scripts/check-marker-literals.py、scripts/check-utf8-boundary-hygiene.sh。