Repository navigation
WIP: procsub: inherit ambient exec fds in the <(...) producer fork (Refs #2466) - #2472
Closed
caomengxuan666 wants to merge 1 commit into
Closed
caomengxuan666 wants to merge 1 commit into
caomengxuan666 wants to merge 1 commit into
Conversation
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
Member
Author
|
感谢这份独立修复——根因定位( 时间戳:#2471 创建于 2026-11-01T14:28:41Z,本 PR 创建于 14:31:33Z——#2471 在先(其簇工作在当晚更早已派发,只是未推送,致两边互不可见,这正是本规矩要防的撞车形态)。 覆盖证据(主会话在 #2471 合入树
本 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
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.
Summary
<(...)process-substitution producer children did not inherit ambientexec-opened descriptors: the #2466 reproducer leftps.txtempty and the body failed with9: 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>(...); atsubst.c:6412it reachesmake_child((char *)NULL, FORK_ASYNC|FORK_PROCSUB)— a plain fork that copies the shell's whole fd table.fd_bitmapentries (subst.c:6557-6581: dup2 of the pipe end onto fd 0/1,close_fd_bitmap(current_fds_to_close),close(parent_pipe_fd)); anexec 9>filebinding 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_substitutemust 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
src/executor/trap_exec.rs:spawn_input_process_substitution_childnow 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.src/executor/external_setup.rs: extractedambient_procsub_spawn_fds()+procsub_fd_transport()used by BOTH spawn sites (plan entries still win over ambient entries on the consumer face).init.rsdup_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 viapin_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:Full class matrix (script file run on both shells,
diffempty for stdout AND stderr):exec 8<din.txt+cat <&8inside<(...)→DATAviar>&7+>&9) with shared-offset append →seven=[seven] ps2=[p\nnine]9>own.txtnever writes the ambient file →amb=[]<(...)nested in$(...)→nest=nest ps3=[p\nnine]<(...)producer and>(...)consumer withexec 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
3. Neighboring regression census
exit_in_write_procsub_after_body_outputordering), 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.cargo check --lib --bins: clean.Machine info
/usr/local/bin/bash, vendored tree 9c465866); rustc 1.98.1 (WSL) / MSVC toolchain (Windows check).Notes
cmd &respawn face (spawn_whitelistedpins extras at their own numbers, not at their slots) — reproducible withexec 7<a; exec 8<b; { ls -l /proc/self/fd/7 /proc/self/fd/8; } &; worth its own issue.Refs #2466