Skip to content

WIP: procsub: inherit ambient exec fds in the <(...) producer fork (Refs #2466) - #2472

Closed
caomengxuan666 wants to merge 1 commit into
masterfrom
fix/2466-procsub-ambient-fd
Closed

caomengxuan666 wants to merge 1 commit into
masterfrom
fix/2466-procsub-ambient-fd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

<(...) process-substitution producer children did not inherit ambient exec-opened descriptors: the #2466 reproducer left ps.txt empty and the body failed with 9: Bad file descriptor. This gives the producer fork the same ambient fd-table transport the >(...) consumer face has had since #2212, and fixes a latent binding-rotation in that shared transport.

GNU C source basis (third_party/bash, vendored tree)

  • subst.c:6362 process_substitute() is one function for BOTH <(...) and >(...); at subst.c:6412 it reaches make_child((char *)NULL, FORK_ASYNC|FORK_PROCSUB) — a plain fork that copies the shell's whole fd table.
  • The child-side teardown closes only the substitution pipe ends and the pending command's fd_bitmap entries (subst.c:6557-6581: dup2 of the pipe end onto fd 0/1, close_fd_bitmap(current_fds_to_close), close(parent_pipe_fd)); an exec 9>file binding is not in that set, so it survives at its number.
  • jobs.c:2265 make_child() does signal/pgrp work only — no fd scrubbing of its own.

So both fork faces of process_substitute must see the ambient fd table. Rubash had diverged: the >(...) consumer spawn (external_setup.rs, #2212) carried it, the <(...) producer spawn (spawn_input_process_substitution_child, trap_exec.rs) did not.

Changes

  1. Producer transport (the [bug] process substitution children do not inherit ambient exec fds (Bad file descriptor) #2466 root cause) — src/executor/trap_exec.rs: spawn_input_process_substitution_child now stamps the same __RUBASH_FD_* env channel (restored into the child's fd table by init.rs) with parent-side inheritable dups closed after spawn.
  2. Shared transport helper — src/executor/external_setup.rs: extracted ambient_procsub_spawn_fds() + procsub_fd_transport() used by BOTH spawn sites (plan entries still win over ambient entries on the consumer face).
  3. Slot-pin pass (pre-existing latent bug, both procsub faces) — the raw respawns delivered each carrier at its duplicate's number, but the child-side restore prefers the descriptor AT the slot number (init.rs dup_adopted_fd(fd), rubash#2333). When a dup number collided with another slot's number the bindings rotated: exec 7<a; exec 8<b; exec 9>c + >(...) consumer gave fd7->b, fd8->c (verified red on master). Both procsub spawns now dup2 each carrier onto its slot between fork and exec via pin_ambient_fds_in_child (external_inner.rs, the same pre_exec pass foreign-child spawns use). Env stamps remain as fallback for guard-skipped targets and non-unix.

Verification

1. Reproducer + class matrix — WSL GNU Bash 5.3.20 script-file comparison, byte-identical stdout/stderr/rc (all 0)

exec 9>ps.txt; cat <(/usr/bin/bash -c 'echo p >&9; echo viaps'); echo "ps=[$(cat ps.txt)]" — both sides print exactly:

viaps
ps=[p]

Full class matrix (script file run on both shells, diff empty for stdout AND stderr):

  • read-direction exec 8<din.txt + cat <&8 inside <(...) → DATAviar
  • two ambient write fds (>&7 + >&9) with shared-offset append → seven=[seven] ps2=[p\nnine]
  • override-safety: command's own 9>own.txt never writes the ambient file → amb=[]
  • <(...) nested in $(...) → nest=nest ps3=[p\nnine]
  • multi-fd mapping probes: <(...) producer and >(...) consumer with exec 7<a; exec 8<b; exec 9>c — fd7/8/9 now point at a/b/c exactly like GNU (pre-fix the consumer rotated).

2. Pinned regression target

cargo test --test issue1626_exec_fd_child_visibility_regressions
test result: ok. 12 passed; 0 failed; (11 passed / 1 failed on master:
  issue1626_exec_fd_visible_inside_process_substitution was the red pin)

3. Neighboring regression census

  • procsub/fd family, 43 targets: green except five targets red IDENTICALLY on pristine origin/master (verified by stash/rebuild/rerun): issue1184_f4 (2), issue1219 (exit_in_write_procsub_after_body_output ordering), issue1959 (abandoned_producer_not_waited_at_boundary), issue438 (argv_word_stays_gnu_dev_fd_form), issue1025..redirect_fd lane (2 × issue_1967) — all pre-existing, untouched.
  • cargo test --lib (WSL): 1029 passed, 0 failed.
  • Windows MSVC cargo check --lib --bins: clean.

Machine info

  • Windows 10 10.0.19045 x64, Git Bash; WSL GNU Bash 5.3.20 (/usr/local/bin/bash, vendored tree 9c465866); rustc 1.98.1 (WSL) / MSVC toolchain (Windows check).

Notes

  • Push performed through a temporary local CONNECT proxy pinning github.com to a reachable edge IP (the usual 127.0.0.1:65532 proxy was down); no repo config was changed.
  • Known pre-existing (not addressed here): the same dup/slot rotation exists on the cmd & respawn face (spawn_whitelisted pins extras at their own numbers, not at their slots) — reproducible with exec 7<a; exec 8<b; { ls -l /proc/self/fd/7 /proc/self/fd/8; } &; worth its own issue.

Refs #2466

GNU process_substitute (subst.c:6362) reaches make_child with
FORK_ASYNC|FORK_PROCSUB (subst.c:6412) — a plain fork that copies the
shell's whole fd table. The procsub child-side teardown closes only the
substitution pipe ends and the pending command's fd_bitmap entries
(subst.c:6557-6581), so an ambient `exec 9>file` binding survives into
the process-substitution child at its number, and the body's own
children inherit it in turn (make_child, jobs.c:2265, does no fd
scrubbing of its own).

The `>(...)` consumer spawn carried the ambient fd table since
rubash#2212 (external_setup.rs); the `<(...)` producer spawn
(spawn_input_process_substitution_child, trap_exec.rs) never did, so a
producer body writing `>&9` failed EBADF — the #2466 reproducer
(`exec 9>ps.txt; cat <(/usr/bin/bash -c 'echo p >&9; echo viaps')`
printed ps=[]).

Root cause, both halves:

1. The producer spawn omitted the ambient fd transport entirely. Both
   fork faces of subst.c:6412 now share one transport:
   ambient_procsub_spawn_fds + procsub_fd_transport (external_setup.rs)
   stamp the __RUBASH_FD_* env channel the child's init restores
   (init.rs), with parent-side dups closed after spawn.

2. The transport itself delivered each carrier at its duplicate's
   number, but the child-side restore prefers the descriptor AT the
   slot number (init.rs:1249 dup_adopted_fd, rubash#2333) — when a
   duplicate number collided with another slot's number the bindings
   rotated (observed pre-existing on the `>(...)` consumer face:
   `exec 7<a; exec 8<b; exec 9>c` gave 7->b, 8->c). Both procsub spawns
   now dup2 each carrier onto its slot between fork and exec via
   pin_ambient_fds_in_child (external_inner.rs), the same pre_exec pass
   the foreign-child spawns use; the env stamps remain as the fallback
   for guard-skipped targets and non-unix platforms.

Verification (WSL GNU Bash 5.3.20 script-file comparison, both sides
byte-identical stdout/stderr/rc): the #2466 reproducer plus the class
matrix — read-direction (<&8), two ambient fds with shared-offset
append, the command-redirect-never-writes-ambient invariant, procsub
nested in comsub, and the >(...) consumer.

- tests/issue1626_exec_fd_child_visibility_regressions: 12/12 (was
  11/12 on master; issue1626_exec_fd_visible_inside_process_substitution
  was the red pin).
- procsub regression family (43 targets) green except five targets red
  identically on pristine origin/master (issue1184_f4 x2, issue1219
  ordering, issue1959 abandoned-producer, issue438 dev-fd word form,
  issue1025.. lane x2 issue_1967) — pre-existing, not touched.
- cargo test --lib: 1029 passed, 0 failed (WSL).
- Windows cargo check clean.

Refs #2466
@caomengxuan666

Copy link
Copy Markdown
Member Author

感谢这份独立修复——根因定位(subst.c:6362 一个函数管 <(...) 与 >(...) 两面、fork 传整张 fd 表)和三段修法都成立。关闭本 PR 的原因是它已被合入的 PR #2471 完全覆盖,按仓库的外部 PR 处置规矩(时间戳 + 硬证据 + 本说明)走 superseded 关闭,特此留证:

时间戳:#2471 创建于 2026-11-01T14:28:41Z,本 PR 创建于 14:31:33Z——#2471 在先(其簇工作在当晚更早已派发,只是未推送,致两边互不可见,这正是本规矩要防的撞车形态)。

覆盖证据(主会话在 #2471 合入树 e6ccbf14 上亲验,GNU bash 5.3.20 对照):

  1. [bug] process substitution children do not inherit ambient exec fds (Bad file descriptor) #2466 原复现:exec 9>ps.txt; cat <(/usr/bin/bash -c 'echo p >&9; echo viaps'); echo "ps=[$(cat ps.txt)]" → viaps / ps=[p],与 GNU 逐字节一致(master:ps=[] + Bad file descriptor)。
  2. 本 PR 第 3 段的槽位轮转形状:exec 7<fa; exec 8<fb; exec 9<fc 后以 >(...) 消费子进程读三 fd——master 输出 7=[B] 8=[C] 9=[](轮转,与本 PR 正文所述一致),WIP: #2212 bats cells — prelude heredoc closer, ambient fd delivery, EXIT-trap once-only, DEBUG silence in EXIT action #2471 树输出 7=[A] 8=[B] 9=[C],与 GNU 一致。WIP: #2212 bats cells — prelude heredoc closer, ambient fd delivery, EXIT-trap once-only, DEBUG silence in EXIT action #2471 的 F2 修复在 trap_exec 的输入 procsub spawn 补齐了环境传递(rubash 子进程 env stamps + 外来子进程 pin_ambient_fds_in_child pre_exec 钉,与本 PR 采用的是同一 [bug] External commands cannot see fds opened persistently with exec (EBADF; missing from child fd table) #1626 通道),轮转随之消除。
  3. WIP: #2212 bats cells — prelude heredoc closer, ambient fd delivery, EXIT-trap once-only, DEBUG silence in EXIT action #2471 的回归钉 tests/issue2212_bats_remainder_regressions.rs(issue2212_08.._11)覆盖本族形状,主会话 scrubbed 复跑 15/15。

本 PR 无测试文件,故无独有测试形状需要吸收;若复跑后发现任何 #2471 未覆盖的形状,请直接在本 PR 或 #2466 上留言,主会话立即接手处理。#2466 已随 #2471 关单。

caomengxuan666 added a commit that referenced this pull request Nov 2, 2026
…e fifo-marker route (#2473)

GNU assembles multi-operand process substitutions per occurrence:
expand_word_internal calls process_substitute for EVERY `<(...)` word
(subst.c:11364-11385), each with its own pipe (subst.c:6381) under its
own /dev/fd name (move_to_high_fd + make_dev_fd_filename,
subst.c:6392-6394, add_fifo_list 6343) — the contract for every
operand being that whatever consumer opens the name receives the
producer's bytes.

rubash#2225 gated the Windows fifo-marker streaming route on binary
identity ("the consumer IS the WinuxCmd runtime, so its reads bridge
markers"). Measured against the installed WinuxCmd 1.1.20 the
prediction is false on the READ side: paste/wc/od/comm/diff read the
7 `!<fifo>` marker bytes plainly (`paste <(echo 1) <(echo x)` printed
`!<fifo>\t!<fifo>`, `wc -c <(printf hello)` counted 7), and WinuxCmd's
own runtime does not rendezvous with its own mkfifo marker — its `od`
reads the marker bytes immediately while the writer blocks in the
pipe open (unixwin/WinuxCmd#1493). A wrong identity prediction here is
silent operand byte corruption, the exact harm #2225 was written to
prevent, so identity is no longer treated as proof.

The delivery gate now inverts admission: every consumer takes the
byte-correct drain (the pre-#2156 shape) unless the machine-level
RUBASH_PROCSUB_FIFO_MARKER=1 key asserts the installed runtime bridges
marker reads — the #1959 streaming route stays armed behind that key
(pinned end-to-end: with the key a non-bridging reader sees the marker
path while rubash's own bridging cat still streams the bytes through
the rendezvous). The identity classification itself is unchanged
(path::winuxcmd_fifo_marker_aware and its pins stand).

Pins (tests/issue2473_procsub_operand_marker_regressions.rs,
#![cfg(windows)]): deterministic end-to-end tests build an aware-class
stand-in consumer by hard-linking the rubash binary under a fake
WinuxCmd tree (winuxcmd.exe + paste.exe) and invoking it as
`paste -c 'cat "$0"'` — one/two/three operands carry their own bytes,
the proof key flips the delivery, the key never reaches non-runtime
consumers, and the single-operand internal face is unchanged; plus
real-WinuxCmd pins (paste 2/3-operand, wc byte count, comm columns,
diff actually seeing the difference) that skip when the runtime is
absent. Unit pin updated: dispatcher-class consumer drains without the
key, marker-routes with it.

Verification: WSL GNU Bash 5.3.20 vs the fixed Windows binary,
script-file byte-compare — stdout/stderr/rc identical for the issue
reproducer plus paste 3-operand, wc count, comm, diff, single-operand
cat (artifacts under target/issue-suites/results/issue2473/).
cargo test --lib: WSL 1029/0; Windows 1123 passed with 2 failures
both identical on pristine origin/master (environment-bound).
Neighboring procsub family green except the master-reds already
documented in #2472 (issue1959 abandoned-producer et al).

Refs #2473
See-also: unixwin/WinuxCmd#1493
@caomengxuan666
caomengxuan666 deleted the fix/2466-procsub-ambient-fd branch November 2, 2026 01:15
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