Repository navigation
fix(#575): exec'd child keeps the shell's stdin channel in every context - #583
Merged
Merged
Conversation
exec.rs had no child-stdin parameter on any spawn path: the capture path spawned via Command::output() whose std default is Stdio::null(), so an exec'd program inside a command substitution read EOF - $(exec cat) returned empty where GNU reads the outer stdin, and modernish's stdin-script mode (tail: eval "$(exec cat)") ran only its first line. GNU ground truth (vendored bash 5.3-16 + WSL 5.3.0 byte matrix): - subst.c command_substitute dups ONLY fd 1 for the capture pipe (subst.c:7143, :7320-7324); fd 0 stays inherited in the subshell. - exec.def exec_builtin -> shell_execve replaces the shell image, so the exec'd program keeps fd 0 exactly as do_redirections bound it (redir.c). Fix: - builtins/exec.rs: execute_with_io/execute_with_child_stdio carry an explicit child_stdin; both spawn helpers (.output() and .status()) set it - no spawn site relies on std defaults anymore. - executor/trap_exec.rs: exec_inherited_stdin() resolves endpoint 0 as the read-side twin of exec_inherited_stdio(1)/(2): File/Coproc dup, Text/ProcessSubstitution remainder materialized to a temp file, InheritedProcessStdin -> inherit, closed -> null; every spawn arm in execute_exec passes it explicitly. - execute_exec binds the command node's fd-0 input redirects before the spawn and unwinds afterwards (bind_command_fd0_input / restore_command_fd0_input, extracted from with_command_input_redirects_inner; open_compound_numbered_input_redirects now also covers the exec node so '<&N' dups reach the child). GNU applies node redirects before execve, so 'exec cat < f' and 'exec cat <<< s' now feed the child instead of silently reading the shell's own stdin. - compound_stdin_open_failure: '<&N' dups are descriptor operations, not path opens - probing "&N" used to abort with a bogus ENOENT. tests/issue575_exec_child_stdin_regressions.rs: 12 cases, byte-compared against WSL bash 5.3.0 (9 fail on master, 3 guards already green). Matrix, GNU vs fixed rubash: pipe/read-then-exec/noread comsub, top comsub, <&0 dup, file redirect, pipeline element, herestring, fd9 dup, <> var - 11/11 identical.
This was referenced Oct 13, 2026
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.
Problem
exec cmdinside a command substitution loses inherited stdin:$(exec cat)sees EOF where GNU reads the outer stdin (issue #575, P1). Downstream, modernish's stdin-script mode (tail: eval "... \$(PATH=\$DEFPATH exec cat)"after the hashbang preload consumed line 1) ran only the first line of a piped script.Root cause:
src/builtins/exec.rshad no child-stdin parameter on any spawn path. The capture path spawned viaCommand::output(), whose std default isStdio::null()when.stdin()is never set, so the exec'd program read EOF immediately. Paths spawning via.status()inherited by default, which is why top-levelexec catworked.GNU contract:
subst.ccommand_substitutedups ONLY fd 1 for the capture pipe (subst.c:7143, :7320-7324) — fd 0 stays inherited in the subshell;exec.defexec_builtin->shell_execvereplaces the shell image, so the exec'd program keeps fd 0 exactly asdo_redirectionsbound it (redir.c).Fix
builtins/exec.rs:execute_with_io/execute_with_child_stdiocarry an explicitchild_stdin; both spawn helpers (.output()and.status()) set it — no exec spawn site relies on std defaults anymore.executor/trap_exec.rs:exec_inherited_stdin()resolves fd-table endpoint 0 as the read-side twin ofexec_inherited_stdio(1)/(2): File/Coproc dup, Text/ProcessSubstitution remainder materialized to a temp file,InheritedProcessStdin-> inherit, closed -> null. Every spawn arm inexecute_execpasses it explicitly.execute_execbinds the command node's fd-0 input redirects before the spawn and unwinds afterwards (bind_command_fd0_input/restore_command_fd0_input, extracted fromwith_command_input_redirects_inner;open_compound_numbered_input_redirectsnow also covers the exec node so<&Ndups reach the child). GNU applies node redirects before execve, soexec cat < fandexec cat <<< snow feed the child.compound_stdin_open_failure:<&Ndups are descriptor operations, not path opens — probing&Nas a file used to abort with a bogus ENOENT.tests/issue575_exec_child_stdin_regressions.rs: 12 cases (9 fail on master, 3 guards already green).No GPL C code involved; semantics anchored to the GNU behavioral contract + file:line references above.
Merge
Branch merged origin/master (7f18b44, #579/#577) — clean merge e6b1d28, zero conflicts (disjoint file sets: the fix touches exec.rs / command_input_scope.rs / trap_exec.rs; #579 touched command_prepare.rs, #577 lexer files). Verified
git diff 7f18b44b e6b1d28bis byte-identical to the12b077eafix diff, i.e. the branch is exactly master + the fix.Verification
GNU oracle: WSL bash 5.3.0 via stdin method (
cat script | wsl.exe bash -s, outputtr -d '\0'), byte-compared.printf 'l1\nl2\nl3\n' | { read -r x; printf 'got=%s rest=[%s]\n' "$x" "$(PATH=/usr/bin exec cat)"; }printf 'p\nq\n' | { printf 'noread=[%s]\n' "$(PATH=/usr/bin exec cat)"; }got=l1 rest=[]/noread=[]— the buggot=l1 rest=[l2\nl3]/noread=[p\nq]<&0dup, file redirect, pipeline element, herestring, fd9 dup,<>var, read-then-exec): 8/8 identical to GNU.printf 'echo 1\necho 2\necho 3\necho 4\n' | rubash bin/modernish-> master: only1; branch:1 2 3 4(GNU: 1 2 3 4).cargo checkclean;RUSTFLAGS="-D warnings" cargo checkclean;cargo fmt --checkclean.cargo test --no-fail-fast(RUST_TEST_THREADS=1) vs fresh-master 7f18b44 on the same machine/environment: branch failure set is a strict subset of the master baseline (43 vs 44, comm one-way empty). The single flip isupstream_suite_slice_snapshots(a known local snapshot-drift failure on master): red on master -> green on branch. Noted: the slice suites involved (comsub/heredoc/nquote) contain noexecat all and the fix touches only exec spawn paths, while the same baseline binary produces different suite output under slightly different environments — the local drift is harness environment sensitivity, not fix semantics. Branch reruns of the test are stable green.issue575_exec_child_stdin_regressionsgreen (12 cases).