Repository navigation
fix(fd): materialize varredir fds as real process descriptors - #3473
Merged
Merged
Conversation
…#3213) GNU opens the file, then fcntl(fd, F_DUPFD, SHELL_FD_BASE) dups it to the first free number >= 10 (redir.c:963-968) and binds the variable to that number (redir.c:1470-1482 redir_varassign), closing the low original (redir.c:1040-1047) — so /proc/$$/fd shows the number and readlink resolves the target. rubash kept the fd virtual (an fd-table entry whose FileFd raw handle sat at some other low number), materialized into children only at spawn through the pin pass: the shell's own /proc/$$/fd lacked the number while exec-form children saw it. Materialize read/write endpoints at their logical number (>= 10) with dup2 at open_input/open_output/duplicate time; the replacement FileFd owns the materialized descriptor so existing close paths close it. Never clobber a live descriptor: when the number is already occupied in this process the endpoint stays virtual. Regression nails: tests/issue3213_varredir_proc_fd_visibility.rs (7 tests; expectations verified against GNU bash 5.3.20).
Member
Author
|
【WIN-AGENT-AUDIT】 机制核查:
腿(逐项给数字与名字):
F 点:
未跑/VOID 的腿:
|
…point (Refs #3213) GNU basis: third_party/bash/redir.c:1239-1271 r_close_this — the close arm resolves the varredir variable, registers undo state, then close_buffered_fd() closes the real kernel descriptor at the close point; add_undo_redirect/add_undo_close_redirect (redir.c:1296-1390) keep undo state as dup'd savefd copies, so the original number always dies immediately. The varredir descriptors come from fcntl(F_DUPFD, SHELL_FD_BASE) (redir.c:963-983). Rubash only dropped the table arm at a close, but snapshot stores (fd_var_apply_snapshot.binds, pending-close/undo stacks) hold cloned FdEntry values whose shared Rc<FileFd> kept the materialized descriptor open until process teardown — /proc/$$/fd still listed the number after `exec {v}>&-` and the varredir_close command-end drain, and the delayed Drop could re-close a recycled number. FileFd now carries an idempotent close_now() (a Cell flag; Drop only closes a live descriptor). The table's arm-drop points — the open_input/open_output/dup_entry arm replacements and close/close_input/close_output — retire the outgoing FileFd's descriptor immediately, unless another live table arm still shares the same Rc (a virtual dup is one open file description, so retiring through a sibling would dead-end the shared binding). Unix-only: off /proc platforms the virtual table is the whole model. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Member
Author
|
【WIN-AGENT-REWORK】round 2 — head F 点逐条回应
跑腿结果
|
caomengxuan666
marked this pull request as ready for review
November 15, 2026 05:06
This was referenced Nov 15, 2026
caomengxuan666
added a commit
that referenced
this pull request
Nov 15, 2026
…3213) fix(build): add missing `closed` field to two FileFd literals (Refs #3213) ## 根因 #3473 给 `fd_table::FileFd` 加了 `closed: Cell<bool>` 字段(`src/executor/fd_table.rs:77-91`,#3213 面),全树其它字面量都补了,唯独漏了 `src/executor/external_setup.rs` 的两处 `FileFd { handle, path: ... }`(:2727 与 :2769)。`cargo build` 因此报两处 `error[E0063]: missing field 'closed'`,尖自 #3473 合入(2026-11-15 13:06 CST)起不可编译。 ## 本 PR 两处各加一行 `closed: std::cell::Cell::new(false),`,与同一文件另 8 处字面量的写法逐字同形。仅此两行,无行为变化。 ## 本 PR 删除了什么 什么也没删除。仅新增两行字段初始化;无文件、无代码、无测试删除。 ## 验证(合并形头 afcdea3,尖 e527da1) - cargo check --locked --lib --bins 干净 RC=0 - RUSTFLAGS="-D warnings" cargo check 干净 RC=0 - cargo build --locked -j1 --bin rubash 成功 RC=0 - #3213 钉 stem tests/issue3213_varredir_proc_fd_visibility.rs 7/7 全绿 - smoke:exec {v}>f.txt 后 v=10、/proc/$$/fd 可见、readlink 指向 f.txt - rustfmt --check 干净;全树扫无第三处缺 closed 的 FileFd 字面量 Refs #3213
caomengxuan666
added a commit
that referenced
this pull request
Nov 15, 2026
caomengxuan666
added a commit
that referenced
this pull request
Nov 15, 2026
… bind (#3601) * fix: sever BASH_ALIASES/BASH_CMDS dynamic link on unset or whole-name bind (Refs #3573) GNU variables.c:3839 unbind_variable severs a dynamic variable for the rest of the shell's lifetime: after `unset BASH_ALIASES` the name is an ordinary variable — `declare -p` reports not found, the alias table no longer resurrects it, and a later `NAME=([k]=v)` bind builds a fresh INDEXED array (do_compound_assignment subst.c:3441) instead of the assoc the stale attribute implied. A direct whole-name bind severs it the same way: scalar `NAME=v` on the live name binds a plain `declare --` scalar that the owner table stops owning. Verified byte-identical against vendored GNU bash 5.3.20 for the full matrix: unset -> not found, unset+compound -> declare -a, live scalar -> declare --, element assign on severed -> declare -a, redeclare -A -> assoc, unsevered -> assoc view still tracks the owner tables. A compound `NAME=(...)` on the still-live name merges into the owner table (keys become real aliases/hashes) — a separate write-through mechanism, noted at the sever door. Pins: tests/issue3573_dynamic_name_severing.rs (6 tests). Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * merge: take master's FileFd.closed call sites in external_setup (#3473 side) * fix(executor): bind live BASH_ALIASES/BASH_CMDS scalar assigns to element [0] (Refs #3573) GNU variables.c:3128-3144 bind_variable_internal dispatches a scalar whole-name bind on a LIVE dynamic assoc through the variable's assign_func with key "0" — assign_aliasvar / assign_hashcmd write the value into the owner alias/hash table as element [0], so `BASH_ALIASES=hello` yields `declare -A BASH_ALIASES=([r] [0]="hello")` with the dynamic link intact (5.3.20 oracle). Rubash severed the name to a plain scalar instead. Route the live scalar arm to the owner-table element-0 bind; unset remains the only sever (variables.c:3839), and compound `NAME=(...)` still excludes via the `(` guard. The previous test expectation asserted the severed `declare --` form; it is corrected to GNU's own hash-bucket render order. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This was referenced Nov 15, 2026
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.
根因
GNU 打开文件后
fcntl(fd, F_DUPFD, SHELL_FD_BASE)把它 dup 到首个 ≥10 的空闲编号(redir.c:963-968),再把变量绑到该编号(redir.c:1470-1482 redir_varassign),并关闭低位原 fd(redir.c:1040-1047)——因此/proc/$$/fd里有该编号,readlink 解析到目标文件。rubash 只在 fd 表里记虚拟条目(FileFd 的 raw handle 落在别的低位编号),只在 spawn 边界经 pin pass 物化进子进程:shell 自身的/proc/$$/fd缺该编号,而 exec 形子进程却能看到它。修法
在
open_input/open_output/duplicate时把 ≥10 的读写 endpoint 用 dup2 物化到其逻辑编号上;替换后的 FileFd 拥有该物化描述符,现有 close 路径随 endpoint drop 关闭它,不新增 bookkeeping。编号已被本进程占用(其他条目的 raw handle、继承的描述符、临时 spawn 管道)或 dup 失败时 endpoint 保持虚拟,绝不覆盖已有描述符。旧 endpoint 先 drop 再物化:dup2 到 fd 会关闭占该编号的描述符,事后再 drop 旧 FileFd 会二次关闭新描述符。GNU 锚点
SHELL_FD_BASE=10fcntl(fd, F_DUPFD, SHELL_FD_BASE)redir_varassign回归钉
tests/issue3213_varredir_proc_fd_visibility.rs(7 格,Linux-only):输出/输入/dup 形 varredir 在 shell 自身/proc/$$/fd可见且 readlink 命中目标;exec {v}>&-关闭后编号消失;{v}>形选项在命令结束时关闭;exec 形子进程继承不变;固定高位 fd 可见。期望逐格对 GNU bash 5.3.20 实测复核通过。本 PR 删除了什么
无删除。仅新增物化路径与 1 个钉文件;
src/executor/fd_table.rs+134/−2,tests/issue3213_varredir_proc_fd_visibility.rs新增 159 行。状态
UNVERIFIED-BY-CAPTAIN — 构建/测试腿(fail-first + 全量 lib +
-D warningscheck)待构建槽可用后跑;X299 腿留复核轮。