Skip to content

fix(executor): dup-save function-call fd 0 before stdin binds (Refs #3727) - #3739

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3727-function-call-stdin-fd-restore
Nov 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3727-function-call-stdin-fd-restore

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

根因

function_call_stdin(src/executor/function_calls.rs)在七处以裸 Rc clone 保存被顶掉的 fd 0 表项(:1599 text/heredoc、:1663 fd-alias dup、:1703 /dev/tcp、:1764 <> device、:1778 < device、:1811 <> file、:1825 < file),随后紧跟的绑定(open_input / dup_input / open_output)经 retire_read_arm / retire_write_arm(#3213)退役被顶掉的描述符;恢复(restore_function_call_fd0 经 FunctionCallStdinBinding)于是把一个已退役表项插回 fd 0,调用返回后首次从 fd 0 读即 EBADF。与 #3706(group redirect)同一 hazard,发生在函数调用 stdin 面。

GNU 锚点

  • 函数重定向可 undo:execute_cmd.c:854 do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE)(由 execute_function 应用)。
  • undo 保存是内核 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 失败则整个重定向失败(redir.c REDIRECTION_ERROR);本修法把 saved_entry 的 io 错误以 ExecuteError::IoError 上抛,同义。

修法

七处一律改为 FdTable::saved_entry(0)?(内核 dup 快照,FdEntry::dup_file_endpoints),绝不用裸 Rc clone;恢复路径不动(插回的已是 dup 出的活描述符,与 GNU undo 同义; <> 对的两臂仍共享同一 dup,单向关闭仍退役共享描述符)。

注意:FdTable::saved_entry / FdEntry::dup_file_endpoints / FileFd::duplicate 是从尚未合入的草稿 PR #3723(#3706 修法,本单的姊妹单)逐字移植的——新增行与 #3723 的 fd_table.rs 增量 byte-identical,哪支先合,另一支在 fd_table.rs 上的 rebase 即 no-op。

回归钉

tests/issue3727_function_call_stdin_fd_restore.rs,12 cell:七个绑定面各一( < file、 <> file、< /dev/null、 <> /dev/null、heredoc、here-string、 <&3、/dev/fd/3 拼写、/dev/tcp——九个面 cell),另加 while-read 循环逐轮恢复、嵌套调用、无重定向对照(继承路径不变)。除对照外每 cell 在未修 base 上 fail-first(调用返回后读 fd 0 得 rc=1 空行)。

本 PR 删除了什么

什么也没删除。只改保存形态:七处 entries.get(&0).cloned() 改为内核 dup 快照;新增 fd_table.rs 的 dup 机制(#3723 移植)与一枚新钉文件;无行为删除、无公开面增减(docs/api-ledger.md 未动)。

UNVERIFIED-BY-CAPTAIN

UNVERIFIED-BY-CAPTAIN:本轮无 cargo 构建路线(Simular box 钱包空 hibernated、X299 与老服务器同断、本 VM 禁本地构建),以下腿全部 OWED,待队长/Win-agent 在路线恢复后补跑:钉 fail-first(base 上红)与修后绿、全量 lib 与 base 逐数对齐、邻茎、RUSTFLAGS="-D warnings" cargo check --locked --lib --bins、GNU 对照。本地已跑通过:逐文件 rustfmt --check(clean)、git diff --check(clean)、scripts/check-marker-literals.py(OK)、scripts/check-utf8-boundary-hygiene.sh(clean)。

Refs #3727

…3727)

Root cause: function_call_stdin saved the displaced fd-0 entry with a
bare Rc clone at seven sites (function_calls.rs :1599 text/heredoc,
:1663 fd-alias dup, :1703 /dev/tcp, :1764 <> device, :1778 < device,
:1811 <> file, :1825 < file). The bind that follows retires the
displaced descriptor (retire_read_arm / retire_write_arm, #3213), so
the restore (restore_function_call_fd0 via FunctionCallStdinBinding)
re-inserted a retired entry and the next read off fd 0 failed EBADF.
Same hazard as #3706 (group redirects), on the function-call stdin
face.

Fix: snapshot fd 0 with FdTable::saved_entry (kernel dup,
FdEntry::dup_file_endpoints) at all seven sites, never a bare clone.
GNU: a function's redirections are undoable (execute_cmd.c:854
do_redirections RX_ACTIVE|RX_UNDOABLE, applied by execute_function)
and the undo save IS a kernel duplicate (fcntl (fd, F_DUPFD,
SHELL_FD_BASE), redir.c:965/1082/1144, SHELL_FD_BASE 10 at redir.c:86).
A dup failure fails the call as ExecuteError::IoError, as GNU fails
the whole redirect (redir.c REDIRECTION_ERROR on a failed F_DUPFD
save).

Note: FdTable::saved_entry / FdEntry::dup_file_endpoints /
FileFd::duplicate are ported verbatim from draft PR #3723 (the #3706
fix, unmerged at this base); the added lines are byte-identical, so
whichever PR lands first, the other rebases to a no-op on fd_table.rs.

Tests: tests/issue3727_function_call_stdin_fd_restore.rs, 12 cells
(one per bind face, plus loop / nested / no-redirect control), each
fail-first on the unfixed base.

UNVERIFIED-BY-CAPTAIN: all build/test legs OWED — no cargo build route
is live this round (see REPORT ~/workspace/fix-3727/REPORT.md).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 06:13
@caomengxuan666
caomengxuan666 merged commit e1e3d7b into master Nov 16, 2026
7 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
… (Refs #3731)

Refs #3731.

## 根因

`execute_persistent_output_process_substitution`(src/executor/trap_exec.rs:7357,base f56716b;修后位于 7360)以裸 Rc clone 保存 fd 0:`let old_fd0 = self.fd_table.entries.get(&0).cloned();`。随后的 `set_fd_input_text(0, ...)` 经 `open_input` retire 被顶掉的 File 描述符(retire_read_arm / retire_write_arm,#3213;`#[cfg(unix)]`),restore 又把已 retire 的 entry 插回去——下一次从 fd 0 读便失败 EBADF。与 #3706(组重定向)、#3727(函数调用 stdin)同 hazard,落在 procsub 体解析面。唯一调用方是 `close_persistent_output_fd`(:7174);体在 fd 关闭时运行,不在命令结束时运行——钉须显式 `exec 3>&-` 触发,否则读在体前、空过。

## GNU 锚点

- redir.c:965/1082/1144:undo save 是 kernel dup(`fcntl (fd, F_DUPFD, SHELL_FD_BASE)`)。
- execute_cmd.c:854:`do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE)`。
- Oracle(本地 bash 5.3.20):primitive 驱动 `r1=0 l1=[o1] r2=0 l2=[o2] r3=0 l3=[o3]` rc=0;body-reads / cat / readwrite / loop 均与 GNU 一致。

## 修法

一行(+3 行注释),沿 #3723 同款:`let old_fd0 = self.fd_table.saved_entry(0)?;`(`FdEntry::dup_file_endpoints`,#3739 已带入 master;unix 下 `fcntl(handle, F_DUPFD_CLOEXEC, 0)`)。dup 失败时 `?` 返回 `ExecuteError::IoError`,体不运行、fd 0 不动;无裸 clone 回退。restore 保持直接 insert 不变(同 `restore_command_fd0_input` 的形态)。

## 本 PR 删除了什么

无删除:不删除任何行为或公开面,仅把 1 行裸 clone 保存换成 kernel-dup 快照,并新增回归钉文件 1 个(tests/issue3731_procsub_body_fd0_restore.rs,11 组)。不碰 `docs/api-ledger.md`、`src/lexer/continuation.rs`。

## 验证(实测)

钉:11 组(primitive、body-reads、cat、compound、twice、readwrite、loop、nested、devnull、control、implicit-doc)。compound 用 `>(echo a; echo b)`;`>({ ... })` 群组体是另一 parser 缺口(见下)。

- **Linux fail-first(新服务器,base f56716b + 钉 v3)**:pin 2 passed / 9 failed(PIN_RC=101)。9 红皆 EBADF:`driver.sh: line 7: read: 0: read error: Bad file descriptor`。lib 1260 passed / 0 failed / 4 ignored(LIB_RC=0)。日志 `~/leg-3731-linux-base.log`(服务器)。
- **Linux fixed(新服务器,fix + 钉 v4)**:pin 11/11(PIN_RC=0),lib 1260 passed / 0 failed / 4 ignored(LIB_RC=0),`RUSTFLAGS="-D warnings" cargo check` rc=0。日志 `~/leg-3731-linux-fixed.log`(服务器)。lib 双边与 base 完全一致。
- **X299 fixed(Windows,fix + 钉 v4)**:pin 11/11(PIN_RC=0),lib 1362 passed / 1 failed / 4 ignored——唯一红与 base 相同(`api::consumer_tests::issue2084_session_end_restores_host_process_state`,既存基线),`RUSTFLAGS="-D warnings" cargo check` rc=0。日志 `C:\Users\WWW\leg-3731-fixed-launch.log`。注:retire 是 `#[cfg(unix)]`,Windows 上钉在 base 即按构造通过,本腿仅作无回归验证。
- **X299 base 旧钉(已废弃)**:pin v1 8/2(触发空洞:读在体前)、v2 10/1(compound 一格为驱动语法错,非 hazard)——均已被 Linux fail-first 取代。
- **rustfmt**:`rustfmt --edition 2021 --check`,trap_exec.rs(61a80f15)rc=0,钉 v4 rc=0。

队长复核(2026-11-17 02:0x CST):CI run 43922615482:goldens 恰 2 基线格(issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots),census 79 格、issue3731 钉全绿,macOS/darwin 全绿,其余 7 项作业全绿、1 项跳过;4 格 head-only(assoc_member_and_bare_at_a_forms_stay_one_word、assoc_star_at_a_quoted_prints_full_assignment_body、elf_operand_is_refused_not_run_as_script、issue3575_posix_cd_out_after_failure_restores_pwd)为 census 抖动——assoc/at 族已知同树翻动(86→93),issue3575 同现于 #3807 printf 头,fd0 保存修法不可达该面;另 15 格父集红格在头上翻绿(含组重定向 fd 恢复面);漂移 f56716b...fcc1de5 与本单两文件零交集 → 按零交集例外合入。

## 残差

- `>({ echo a; echo b; })` 群组体:GNU 合法(实测,rc=0),rubash 报 `ambiguous redirect`——独立 parser 缺口,不属本单,已记 REPORT §4,待队长立案。
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
#3729)

## Summary

Fixes the stdio-marker reinstate hazard in `close_persistent_output_fd` (rubash#3729): the face took a bare `Rc` clone of the fd's entry, then `open_output(fd, Stdout/Stderr marker)` retired the displaced descriptor (`retire_read_arm`, rubash#3213), and the restore re-inserted the retired entry — the same hazard as #3706. The snapshot is now a kernel duplicate via `FdTable::saved_entry` (the #3723/#3739 pattern), exactly like GNU's `fcntl (fd, F_DUPFD, SHELL_FD_BASE)` save (redir.c:965/1082/1144). A dup failure propagates (GNU fails the redirect the same way).

## 本 PR 删除了什么

无删除。仅将一行裸 clone 改为内核 dup 快照,并新增回归钉文件 `tests/issue3729_stdio_marker_fd_restore.rs`(7 格)。

## Reachability note (read before reviewing the pin)

The face needs a File-backed read arm AND the output-procsub registration to coexist on fd 1/2 at close/flush time. They cannot: every input bind calls `clear_output_fd_mirrors` (rubash#3655) and drops the registration, and every output bind retires any read arm. A full trigger battery against GNU bash 5.3.20 and the base binary (f56716b) confirms it — `exec 2> >(...)` + `exec 2<input.txt` + close/rebind/dup-to-closed/failed-rebind, group variants, and the `{var}`-close form all agree with GNU on the read result. So there is NO shell-level fail-first cell for the face itself: the 7 pin cells are GUARDS on the face's code path (stdio-marker reinstate + fd state after close/rebind), written so a future change that makes the face reachable cannot silently reintroduce the hazard. All 7 pass on the base and on the fixed head. Full battery + evidence in REPORT.md (workdir `~/workspace/fix-3729/REPORT.md`).

A separate real divergence in the same area — the deferred procsub body is dropped after an input rebind (GNU runs it at close, rubash does not) — is documented as a residual in the REPORT for the captain to file as its own issue; it is deliberately NOT pinned here (it fails on the fixed head too).

## Legs (队长复核)

- X299 base pin stem (`issue3729_stdio_marker_fd_restore`): **7 passed / 0 failed** (base build RC=0, 45.10s).
- X299 head pin stem: **7 passed / 0 failed** (head build RC=0, 14.02s).
- X299 lib bilateral: head **1362 passed / 1 failed** vs base **1362 passed / 1 failed** — sole fail on both sides is the known `api::consumer_tests::issue2084_session_end_restores_host_process_state` (no new red).
- `RUSTFLAGS="-D warnings" cargo check --locked --all-targets`: **RC=0**, zero diagnostics (39.83s).
- `rustfmt --edition 2021 --check` on the 2 changed files: **RC=0**.
- marker-lint: **OK** (118 registry consts, 268 allowlisted sites).
- CI(更新头 6fd24c2,run 43926778677,已 update-branch 到尖 52ffeba):goldens 恰 2 基线格(issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots);census 78 格、issue3729 钉全绿;其余 8 项作业全绿、1 项跳过。3 格 head-only(elf_operand_is_refused_not_run_as_script、issue1902_stdout_stderr_names_follow_their_bindings、issue3575_posix_cd_out_after_failure_restores_pwd)皆为已备案 census 抖动格(issue1902 为 #3799 复核裁定的抖动格;elf/issue3575 同现于 #3801/#3807 头;修法面已证不可达,钉为 guard 钉)。旧头 census 重跑已证同头两次跑 4 格翻动。

队长复核(2026-11-17 02:2x CST):diff 已读——trap_exec.rs 1 行裸 clone 改 kernel-dup 快照 + 7 格 guard 钉;reachability §3.2 要点已亲核(clear_output_fd_mirrors 删双键、三处 set_fd_input_* 尾调用、open_output 替换双臂);X299 腿钉 7/7 双边、lib 双边同名同数、-D warnings rc=0、fmt 干净、marker-lint OK。
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
fd_table.rs helpers already landed via #3739; take master's version.
Picks up #3723's saved_entry conversion of the compound numbered-input
save, removing the stale-FileFd producer that turned this PR's
saved_entry dup into an EBADF command failure on Linux.
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
fd_table.rs helpers already landed via #3739; take master's version
(the PR's hunk was byte-identical modulo doc comments).
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