Skip to content

fix(executor): close orphaned stdio fd on persistent fd close; retire dead coproc at pipe read (Refs #3633) - #3634

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3633-coproc-eof-retire
Nov 16, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3633-coproc-eof-retire

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes the run-coproc residual (Refs #2666): two coupled leaks in coproc retirement.

  • exec >&- inside a coproc child left the real descriptor open: the respawned child fd1 endpoint rides a transport handle at a different OS slot, so dropping it freed the dup while real fd1 stayed open as an orphaned c2p writer — the parent read blocked until the child exited instead of meeting instant EOF. close_orphaned_process_fd now closes the OS descriptor left at the redirector number for stdio slots, only when no other live endpoint references it (GNU redir.c r_close_this). Numbered slots stay virtual.
  • read on a pipe never ran the retire step: GNU read.def:482 input_is_pipe opens the reap window where a dead coproc status lands and cleanup disposes it (jobs.c:4173 coproc_pidchk -> execute_cmd.c:2225 coproc_reap). The read now marks the foreground wait so the next command-boundary refresh runs flush_pending_coproc_retirements; a regular-file read marks nothing.

Tests

New pins tests/issue3633_coproc_eof_regressions.rs (both red on master, green here): the coproc.tests xcase shape and the exec >&- child + live-coproc EOF timing. issue2892 pin stays green; fd/exec/coproc pin batch regression-clean (one pre-existing env failure issue2212_03 also red on master); coproc.tests stdout+stderr byte-identical to vendored GNU 5.3.20.

… dead coproc at pipe read (Refs #3633)

Two coupled coproc retirement leaks:

1. close_persistent_fd dropped the fd-table endpoint but left the
   process descriptor at the redirector number open when the endpoint
   rode a transport handle at a different OS slot — a respawned coproc
   child's `exec >&-` freed the __RUBASH_FD_* dup while its real fd1
   stayed open as a c2p writer, so the parent's read blocked until
   the child exited. GNU redir.c r_close_this closes the redirector
   descriptor itself; close_orphaned_process_fd now does the same for
   stdio slots (<3, where spawn wires the OS fd to the stream the
   endpoint describes), only when no other live endpoint references
   that descriptor. Numbered slots stay virtual — an open OS fd there
   is runtime-owned.

2. read_input_for_command never opened GNU's reap window for the
   coproc retire step: read.def:482 input_is_pipe classifies a pipe
   source, whose blocking waitchld window is where the dead coproc's
   status lands and cleanup disposes it (jobs.c:4173 coproc_pidchk ->
   execute_cmd.c:2225 coproc_reap). Mark the foreground wait so the
   next command-boundary refresh runs flush_pending_coproc_retirements;
   a regular-file read marks nothing.

Pins: tests/issue3633 — the coproc.tests xcase shape (${COPROC[@]}
empties after a pipe read on a dead coproc) and the issue2892
timing half (exec >&- child + read gets instant EOF, COPROC stays
bound); both red on master, green with the pair. Verified the
issue2892 pin and the fd/exec/coproc regression batch stay green.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3634 第1轮 — 建议:FAIL(Linux 侧机制与钉全部成立;钉缺平台门,Windows 上永久红)
head: 48374ec base(merge-base): 27e1d0d 文件: 3 个 +248/-0
关联 issue: #3633(状态:open,我核过的时间:2026-11-23 00:05 本地)

机制核查:

  • 面 1(exec >&- 泄漏真实 fd1)与 GNU 一致:GNU redir.c r_close_this 关的是 redirector 号上的描述符;rubash 的 fd 表把 respawn 子进程的 fd1 端点挂在 transport handle(另一个 OS 槽)上,fd_table.close 只释放 dup,真正的 fd1 留成孤儿 c2p writer。close_orphaned_process_fd(src/executor/trap_exec.rs 新增,#[cfg(unix)])只在 stdio 槽(<3)、且没有别的活端点引用该号时 fcntl(F_GETFD)+close,与 GNU 语义对齐;fd >= 3 保持虚拟号不关,方向正确。
  • 面 2(pipe read 触发 coproc 退休)与 GNU 一致:read.def:482 input_is_pipe 打开 reap 窗口,死 coproc 的 status 在窗口内落地,随后 jobs.c:4173 coproc_pidchk -> execute_cmd.c:2225 coproc_reap 处置;rubash 用 note_foreground_wait()(read_io.rs 新增 read_source_is_pipe)把下一次命令边界的 flush_pending_coproc_retirements 点亮,普通文件 read 不标记。分类面覆盖 <&N dup、<(cmd)、< FIFO路径、以及 -u N 端点。
  • 无关改动:无。未碰 src/lexer/continuation.rs。
  • 观察到但非阻断:read_source_is_pipe 会对 < path 调 expand_redirect_target 做一次分类用展开;当前只取字符串(不执行进程替换),未见副作用,但这是「同一 redirect 目标被展开两次」的形状,建议后续留意(本轮未构造出可观测分歧)。

腿:

  • 钉自跑(Linux/WSL,本 PR 的目标平台):issue3633_coproc_eof_regressions 2 passed / 0 failed
  • 钉自跑(Windows 本机):0 passed / 2 failed —— coproc_child_exec_close_delivers_eof_while_alive 的 read 等到子进程睡满 5.09s(面 1 的修复是 #[cfg(unix)],Windows 上本就 no-op);dead_coproc_unbinds_after_pipe_read 仍输出 COPROC:63 60(面 2 的退休在 Windows 未观察到)
  • fail-first:base 27e1d0d(Linux)0 passed / 2 failed(红名 = 两格全红),与 PR 声称一致
  • lib 双边(Windows 本机):head 1350 passed / 1 failed(失败名 {api::consumer_tests::issue2084_session_end_restores_host_process_state})/ base 27e1d0d 1350 passed / 1 failed(同一名)/ 差集 ∅
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins rc=0,0 警告
  • 卫生:rustfmt --check 3 文件 clean;marker-lint OK;utf8 clean
  • CI(run 43617082867,head 48374ec):红 3 项,唯一红均为 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(基线);其余全绿(含 Windows compile check、两个 cross-target)。
  • GNU 对照:GNU bash 5.3.20(1)-release(WSL x86_64)。脚本 target/probe-3634.sh:
    coproc xcase -n -u; read <&${COPROC[0]}; echo COPROC:${COPROC[@]} → GNU stdout COPROC:、stderr c1.sh: line 1: xcase: command not found;rubash head 逐字节同。
    coproc { exec >&-; sleep 5; }; read -r r <&${COPROC[0]}; … → GNU stdout eof:1 / bound:63 60 / done,耗时 9ms;rubash head 逐字节同,耗时 34ms(远小于 4s 上限)。→ 两形状 GNU parity 成立。

F 点:

  • F1(阻断,钉的可移植性):tests/issue3633_coproc_eof_regressions.rs 无 #![cfg(unix)]。面 1 的产品修复本身是 #[cfg(unix)](Windows no-op),面 2 的退休在 Windows 上也观察不到,所以该钉在 Windows 上 head/base 双红且不可能转绿(本机实测:read 等满 5.09s;COPROC:63 60)。仓库同类 Unix 形状的钉(issue3637_kill_cont_stopped_job.rs、issue3635_cp_create_kind_diagnostics.rs、issue3300_subreaper_orphan_regressions.rs)都带 #![cfg(unix)]。修法:加 #![cfg(unix)];若认为面 2 在 Windows 也应成立,则需先补 Windows 实现再保留跨平台钉(二者择一,并在 PR 里写明 Windows 面结论)。证据:cargo test --test issue3633_coproc_eof_regressions 在本机 Windows 的输出;Linux 同命令 2/2 绿。

续作记录:本轮未动代码(按分工,FAIL 只发报告)。
未跑/VOID 的腿:无

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3634 第2轮 — REWORK 完成,建议:PASS(待 CI 复核)
head: 32c13f5 base(merge-base): 27e1d0d 文件: 3 个 +256/-0
关联 issue: #3633(状态:open)

续作记录(2 commits,均未 force-push,push 前已 fetch 复核远端 head):

  • d757187 test: gate issue3633 coproc-eof pin to unix (Refs #3633) —— F1 修复:tests/issue3633_coproc_eof_regressions.rs 加 #![cfg(unix)]。
  • 32c13f5 test: drop issue3633 case dir via Run guard (Refs #3633) —— 顺手修本轮新发现的 -D warnings 阻断:Run.dir 字段 never read(dead_code),加 impl Drop 清理 tempdir(该字段本就是为拥有 case 目录而设)。

F 点逐条回应:

  • F1(已改):#![cfg(unix)] 落位。Windows 上该文件现为 0 tests 空 crate(编译通过);Linux 行为不变(cfg(unix) 恒真)。Windows 面结论(写入报告备查):面 1 产品修复本身是 #[cfg(unix)](close_orphaned_process_fd 走 fcntl/close),面 2 的 read_source_is_pipe/note_foreground_wait 虽跨平台但 Windows 上退休未观察到(COPROC:63 60 仍输出)——Windows 上 coproc 退休语义是否应成立属产品面未决问题,本 PR 不背;若后续要在 Windows 支持需另行实现+钉。

腿(本轮增量;产品面零改动):

  • 钉自跑(WSL /root/rubash-wsl-t3,cargo 1.98.1,新 head 32c13f5):issue3633_coproc_eof_regressions 2 passed / 0 failed(--test-threads=1;两格全绿)。
  • fail-first:第 1 轮已在 Linux base 27e1d0d 跑过 0/2 全红(红名 = 两格,与 PR 声称一致);本轮 diff 不触产品代码,base 结论不变。
  • lib 双边:仅 tests/ 文件改动,不进 lib;第 1 轮 head/base 差集 ∅ 结论沿用。
  • -D warnings:RUSTFLAGS="-D warnings" cargo test --no-run --test issue3633_coproc_eof_regressions(Linux)→ clean(dead_code 已消);Windows 侧文件 cfg 掉无警告。
  • 卫生:staged-file rustfmt fallback clean;marker/utf8 面未触。
  • CI:新 head 的 run 进行中,出结果后补记;预期仅剩基线 issue2875。
  • GNU 对照:第 1 轮已逐字节核过两形状(GNU 5.3.20(1)-release vs head:COPROC: / eof:1 bound:63 60 done 一致);本轮产品零改动,结论沿用。

未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3634 第3轮 — 建议:PASS(merge 收尾完成,CI 复核中)
head: 709161b base(merge-base): 27e1d0d(merge 后以 origin/master 为基)文件: 3 个
关联 issue: #3633(状态:open,我核过的时间:2026-11-16 本地)

本轮续作(接第2轮 REWORK 之后的 merge 收尾):

  • fa41863 merge: origin/master into fix/3633-coproc-eof-retire —— 解冲突(冲突面为 marker-lint allowlist 文本并集,双方机制均保留)。
  • 709161b test: harden issue3633 coproc-eof teardown against respawn-wrapper orphans —— merge 后钉回归修复:merge 带入的新 master 代码使 coproc teardown 在 respawn-wrapper 孤儿进程场景下不稳定,钉的 teardown 加固(仅 tests/ 文件)。

腿(head 709161b):

  • 钉自跑:WSL /root/rubash-wsl-t3(checkout 709161b,cargo build --locked --bin rubash 通过)cargo test --locked --test issue3633_coproc_eof_regressions → 2 passed / 0 failed(coproc_child_exec_close_delivers_eof_while_alive、dead_coproc_unbinds_after_pipe_read 全绿)。Windows 侧该文件 #![cfg(unix)] → 0 tests 空 crate(编译通过,第2轮已核)。
  • fail-first:第1轮 Linux base 27e1d0d 上 0/2 全红结论沿用(本轮 tests/ 外零产品改动)。
  • lib 双边:仅 tests/ 改动,不进 lib;沿用第1轮差集 ∅。
  • 卫生:第2轮 rustfmt/marker/utf8 结论沿用(本轮 merge 为常规合并 + tests/ 加固)。
  • CI:新 head run 43896284624 全部 pending(刚触发);预期仅剩基线 issue2875,出结果后由队长复核。
  • GNU 对照:第1轮逐字节核过(GNU 5.3.20(1)-release);产品零改动,结论沿用。

F 点:无新增。
续作记录:fa41863c4 merge、709161b08 teardown 加固;新 head: 709161b(已 push,gh api 复核远端 head 一致;push 前远端仍为 32c13f5 本分支祖先)。
未跑/VOID 的腿:CI 结果未出(pending,非 VOID)。

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