Skip to content

fix(#575): exec'd child keeps the shell's stdin channel in every context - #583

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/575-child-stdin
Oct 13, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/575-child-stdin

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

exec cmd inside 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.rs had no child-stdin parameter on any spawn path. The capture path spawned via Command::output(), whose std default is Stdio::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-level exec cat worked.

GNU contract: 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 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 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.
  • compound_stdin_open_failure: <&N dups are descriptor operations, not path opens — probing &N as 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 e6b1d28b is byte-identical to the 12b077ea fix 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, output tr -d '\0'), byte-compared.

  1. Issue minimal repro (p.sh):
    • 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)"; }
    • master (7f18b44): got=l1 rest=[] / noread=[] — the bug
    • GNU oracle: got=l1 rest=[l2\nl3] / noread=[p\nq]
    • this branch: identical to GNU, byte-for-byte
  2. Extended 8-shape matrix (top comsub, <&0 dup, file redirect, pipeline element, herestring, fd9 dup, <> var, read-then-exec): 8/8 identical to GNU.
  3. modernish stdin-script: printf 'echo 1\necho 2\necho 3\necho 4\n' | rubash bin/modernish -> master: only 1; branch: 1 2 3 4 (GNU: 1 2 3 4).
  4. cargo check clean; RUSTFLAGS="-D warnings" cargo check clean; cargo fmt --check clean.
  5. Full 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 is upstream_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 no exec at 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.
  6. New regression suite issue575_exec_child_stdin_regressions green (12 cases).

P1 fix agent and others added 2 commits October 12, 2026 18:41
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.
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