Skip to content

WIP: #2492 procsub producer inherits ambient std-stream fds on every platform (exec 8<&0 + <(... <&8)) - #2504

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2492-procsub-std-endpoint-fd
Nov 2, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2492-procsub-std-endpoint-fd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: #2492 — procsub producer delivers ambient std-stream-endpoint fds on every platform. Do not merge (captain harvests).

Refs #2492 #2471 #2480

Root cause (one sentence)

spawn_input_process_substitution_child (src/executor/trap_exec.rs) had its ambient __RUBASH_FD_* delivery pass behind #[cfg(unix)], so the Windows streamed <(...) producer respawn carried NO ambient fd at all — and since exec 8<&0 binds the symbolic FdReadEndpoint::InheritedProcessStdin, fd 8 never reached the body, which then reported 8: Bad file descriptor at both read <&8 and exec 0<&8.

This is the residual half (a) of #2471 on Windows: the std-endpoint materialization (ambient_procsub_spawn_fd -> standard_backing dup, external_setup.rs) and the consumer-side ambient loop landed there, but the producer-side pass stayed Unix-only. issue2212 pin 08 only exercises the collected comsub route (body runs in-process), so the streamed Windows spawn was never covered.

GNU basis (C source citations)

  • redir.c:1192-1193 do_redirection_internal() dup branch: dup2 (redir_fd, redirector) — exec 8<&0 leaves fd 8 on the process stdin's open file description.
  • subst.c:6362 process_substitute(), make_child at subst.c:6412 — a pure fork: the child inherits the whole fd table, reads through fd 8 consume the same description, and exec 0<&8 inside the child is a legal dup.

Fix

The producer's ambient pass now runs on every platform (trap_exec.rs): the env-stamp loop drops its #[cfg(unix)] (only the Unix pre_exec dup2 pins stay platform-gated). On Windows, rustc 1.99 std's plain Command::spawn sets no PROC_THREAD_ATTRIBUTE_HANDLE_LIST (verified in the toolchain source — STARTF_USESTDHANDLES with bInheritHandles=TRUE), so the stamped inheritable dups cross at their own handle values — exactly what the child's init already trusts verbatim (init.rs, "Windows handle values transfer verbatim across CreateProcess inheritance"), the same transport the output form's plain spawn (external_setup.rs:3333) already uses. Std-stream endpoints (8<&0 / 8<&1 / 8>&2) arrive materialized through ambient_procsub_spawn_fd's standard_backing dup (#2212), so they ride as Files. No new transport, no new guards — the existing design extended to the platform it skipped.

Matrix (WSL GNU Bash 5.3.x vs rubash, script files, stdin fed on BOTH sides — the fd is a dup of stdin, an unfed run blocks GNU in the child's read <&8)

Ambient fd Procsub form Child face Verdict
exec 8<&0 <(...) producer read <&8 + exec 0<&8 byte-identical (stdout)
exec 8<&1 <(...) producer read <&8 (fails rc 1) + exec 0<&8 (ok) byte-identical
exec 8>&2 <(...) producer write >&8 + exec 2>&8 byte-identical (stdout AND stderr, rc 1)
exec 8<&0 >(...) consumer read <&8 byte-identical (side-file channel)
exec 8<&1 >(...) consumer read <&8 (fails rc 1) byte-identical
exec 8>&2 >(...) consumer write >&8 byte-identical (side-file + stderr)

The issue's repro is now byte-identical: parent got: [child sees fd8: [DATA-LINE]] / parent got: [ child exec 0<&8 ok]. Raw artifacts: target/issue-suites/results/issue2492/ (cases/, out/, run_matrix.sh).

Adjacent pre-existing gap observed while matrixing (NOT this PR's scope, fd-independent — reproduced with no ambient fd at all): a single echo start > >( read payload; echo ... ) body's STDOUT never surfaces under rubash (payload:[start] on GNU); the body runs, its side files and stderr writes land (pinned by the consumer cells above). Same family as the known-red issue2195 Windows faces; likely for the #2234 live-pipes cluster.

Verification

  • repro byte-identical (both streams, both shells, stdin fed).
  • new pins tests/issue2492_procsub_std_endpoint_fds.rs: 6/6 (stdin fed explicitly in the harness).
  • family: issue2212 14/14, issue2292 18/18 (issue1626 is #![cfg(unix)], no-op on Windows).
  • issue2195: 7 reds verified identical on clean master (pre-existing Windows consumer-face gaps, not regressions).
  • cargo test --lib parity with clean master, on BOTH bases: old base 37eef11 and current master 2f566a0 — fix tree 1124 passed / 2 failed / 3 ignored vs master 1124/2/3, same 2 pre-existing reds (api::consumer_tests::issue2084_session_end_restores_host_process_state, executor::tests::unit_tests::shell_identity_is_used_for_default_dollar_zero).
  • rebase note: master moved (37eef11 -> 2f566a0, 12 commits touching trap_exec.rs etc.) while this was in flight; the fix cherry-picks cleanly onto 2f566a0 and every number above is from the re-verified 2f566a0 tree.

Disclosures

  • rustfmt hook / os error 206: repo-wide cargo fmt --check fails with os error 206 on this machine (reproduced in the pristine main checkout — the rustfmt argument line exceeds the Windows limit with this many test targets), so the pre-commit hook cannot execute. Both files in this PR were verified with per-file rustfmt --edition 2021 --check: clean, except one PRE-EXISTING master-side diff in trap_exec.rs at line 387 (present in committed master, untouched here). Local commit therefore used --no-verify with this disclosure.
  • transport note: github.com:443 is TCP-blocked from this machine right now (api.github.com and codeload.github.com reachable), so git push cannot run. The remote branch fix/2492-procsub-std-endpoint-fd was built through the GitHub Git Data API (blobs + tree + commit directly on parent 2f566a0); its content is byte-identical to the locally verified tree (blobs uploaded from git show of the verified commit). If the network recovers, the captain can force-push the local history over it without content change.
  • shared-stash incident (crew note): during verification, a git stash pop in this worktree popped ANOTHER session's stash entry (the stash stack is shared across the 20+ worktrees). The foreign 4-file WIP (fix/procsub-fd-arg: function_env.rs, ast_print.rs, nodes.rs, token_actions.rs) was restored UNTOUCHED to the shared stack as stash@{0} with message "recovered untouched: fix/procsub-fd-arg 4-file WIP...", and a patch copy sits at the wt-2492 machine's /tmp/wt2492_foreign_4file.patch. Owners may want a per-worktree stash discipline.

…m (rubash#2492)

GNU basis: `exec 8<&0` is the dup branch of do_redirection_internal
(redir.c:1192 — `dup2 (redir_fd, redirector)`), leaving fd 8 on the
process stdin's open file description, and process_substitute
(subst.c:6362, make_child at subst.c:6412) is a pure fork: the child
inherits that descriptor whole, reads through it consume the same
description, and `exec 0<&8` inside the child is a legal dup.

Residual of #2471's half (a): spawn_input_process_substitution_child
(executor/trap_exec.rs) had its ambient `__RUBASH_FD_*` pass behind
#[cfg(unix)] — the Windows streamed producer spawn delivered NO
ambient fd at all, std-stream or File, so after `exec 8<&0` a
`<( ... <&8 ... )` body failed with `8: Bad file descriptor`
(the modernish _Msh_loopgen `exec 0<&8` face). issue2212 pin 08
covered the collected comsub route (body runs in-process), so the
Windows streamed spawn was never exercised.

Fix: the pass now runs on every platform. On Unix nothing changes
(the pins still dup2 each dup onto its slot in pre_exec); on
Windows std's plain Command::spawn sets no
PROC_THREAD_ATTRIBUTE_HANDLE_LIST (verified in the rustc 1.99
toolchain source — STARTF_USESTDHANDLES with bInheritHandles=TRUE),
so the stamped inheritable dups cross at their own handle values,
exactly what the child's init already trusts verbatim (init.rs,
"Windows handle values transfer verbatim across CreateProcess
inheritance") — the same transport the output form's plain spawn
(external_setup.rs) uses. Std-stream endpoints arrive materialized
through ambient_procsub_spawn_fd's standard_backing dup (#2212),
so `8<&0` / `8<&1` / `8>&2` all ride as Files.

Verification (WSL GNU Bash 5.3.x script files, stdin fed both sides
— the fd is a dup of stdin, an unfed run blocks GNU in the child's
read <&8):
- repro byte-identical (parent got: [child sees fd8: [DATA-LINE]] /
  [  child exec 0<&8 ok]).
- matrix {8<&0, 8<&1, 8>&2} x {<(...), >(...)} x {read / write /
  exec dup}: 6 semantic cells byte-identical on stdout/stderr or the
  side-file channel; artifacts under
  target/issue-suites/results/issue2492/.
- new pins tests/issue2492_procsub_std_endpoint_fds.rs: 6/6.
- issue2212 14/14, issue2292 18/18; issue2195 reds (7) verified
  identical on clean master 37eef11 (pre-existing Windows
  consumer-face gaps); cargo test --lib parity with clean master:
  1124 passed / 2 failed / 3 ignored on BOTH trees (same 2
  pre-existing reds).

Hook note: committed with --no-verify because the repo-wide
`cargo fmt --check` fails with os error 206 on this machine in the
pristine main checkout too (argument line over the Windows limit
with this many test targets) — the hook cannot execute here.
Both staged files verified with per-file `rustfmt --edition 2021
--check`: clean, except one PRE-EXISTING master-side diff in
trap_exec.rs at line 387 (present on committed master, untouched).

Refs #2492 #2471 #2480
@caomengxuan666
caomengxuan666 force-pushed the fix/2492-procsub-std-endpoint-fd branch from 0d0da49 to 58567ad Compare November 2, 2026 00:41
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 00:41
@caomengxuan666
caomengxuan666 merged commit 6e06070 into master Nov 2, 2026
11 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2504 PASS — new pin 6/6 x2 @--test-threads=4; issue #2492 repro (stdin fed on both sides) byte-identical to WSL GNU Bash 5.3.20: parent got: [child sees fd8: [DATA-LINE]] / parent got: [ child exec 0<&8 ok], stdout/stderr 0 diff lines; families issue2212 14/14, issue2292 18/18 green, issue1626 0-run (unix-gated as documented).

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 6e06070 (= this PR's merge step), GNU oracle = wsl bash 5.3.20 script-file comparison. Probe: D:/repo/win-accept/probes/p2492.sh (the issue's verbatim repro), stdin fed via pipe on both sides since fd8 is a dup of stdin.

This is the cfg(windows)-face PR (the #[cfg(unix)] drop on the producer's ambient __RUBASH_FD_* pass in trap_exec.rs) — verified on the real Windows face: the streamed <(...) producer child now reads and dups the ambient fd 8 exactly like GNU's fork-inherits-fd-table behavior (subst.c:6362 process_substitute).

The PR body's adjacent pre-existing gap (single echo start > >( read payload; ... ) body stdout never surfacing) was noted as out-of-scope/#2234-cluster — not re-litigated here.

No BLOCKER.

@caomengxuan666
caomengxuan666 deleted the fix/2492-procsub-std-endpoint-fd branch November 3, 2026 09:05
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