Skip to content

WIP: fix exec with piped stdin — temp carrier leak + dropped redirection (#2395) - #2400

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/r41-exec-stdin
Oct 31, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/r41-exec-stdin

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2395

exec with a piped stdin: the stdin materialization temp carrier (rubash-process-subst-*.tmp) is left behind in $TMPDIR and the exec command's own redirections (e.g. 2>/dev/null) are dropped. Fixing at the exec/respawn boundary. WIP scaffold commit; fix + pins to follow.

…ws the armed capture (Refs #2395)

GNU contract: execute_cmd.c:4244/4268 performs a command's
redirections, then exec_builtin (builtins/exec.def:231) hands the
image the shell's descriptors as bound via shell_execve
(execute_cmd.c:6126). There is no named stdin materialization and
no second stderr channel.

1) Carrier leak. exec_inherited_stdin writes virtual fd-0 text
(pipeline-stage payload, heredoc, here-string) to a temp file via
materialize_exec_stdin_bytes, which did fs::write + File::open and
dropped the path: every such exec left a
rubash-process-subst-*.tmp in $TMPDIR forever. The carrier is now
anonymous from birth — one read/write handle, unlinked after open
on Unix (the redir.c:423 here_document_to_fd shape), opened with
FILE_FLAG_DELETE_ON_CLOSE on Windows (the
synthetic_proc_write_sink discipline) — so it dies with the last
descriptor in every spawn arm, including true image replacement.

2) Dropped stage redirection. A pipeline stage's clone strips its
fd-1/fd-2 redirects (the driver owns them) and arms stderr_capture
for the stage's fd-2 bytes; the exec spawn wrote the child's piped
stderr and its own diagnostics straight to the process stderr, so
`printf '' | exec bash -c 'echo e >&2' 2>/dev/null` printed `e` to
the terminal and `2>err.txt` routed nothing. The spawn's stderr
destination now follows the executor's armed capture
(ExecDiagStderr / with_exec_diag_stderr in trap_exec.rs, applied
at every execute_exec_spawn arm that used the raw process
stderr); with no capture armed the destination is unchanged.

Pins: tests/issue2395_exec_piped_stdin_regressions.rs (7 cases:
pipeline stdin residue, 2>/dev/null, 2>file routing, heredoc and
here-string residue, file-stdin control, failure-diagnostic
routing) — byte-verified against GNU bash 5.3.20 script-file
probes. Neighbor exec targets (1102/1175/1412/1012/1021/1132/
1274) and the brief-named 2292/2308 all still green.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 31, 2026 18:46
@caomengxuan666
caomengxuan666 merged commit 99eacf2 into master Oct 31, 2026
caomengxuan666 added a commit that referenced this pull request Nov 1, 2026
GNU's here_document_to_fd (redir.c:423) makes the heredoc temp
"really temporary" (redir.c:548-551): the scratch file is written
through one handle, re-opened by name for reading, and the name is
unlinked before the descriptor is ever used — the temp has no
directory entry for its whole working life (small bodies ride an
anonymous pipe instead, redir.c:452-510).

Both compound_exec.rs materialization faces in that class created
their carrier with fs::write + open and never unlinked it, leaking
a named rubash-process-subst-*.tmp into $TMPDIR for every command —
the #2395 leak class on this face (the exec face was fixed in
PR #2400):

- materialize_async_stdin_body: a background command carrying its
  own heredoc / here-string redirect (cat <<EOF &, { cat; } <<EOF &,
  ( cat ) <<EOF &). One caller discarded the returned path outright;
  the other keeps it only as the FileFd name record.
- materialize_virtual_stdin_fd0: an async child inheriting a
  virtual fd 0 (an enclosing compound's staged heredoc text channel
  / FUNCTION_STDIN, or a Text / ProcessSubstitution fd-0 endpoint).

Both now mirror GNU and the #2395 fix: scratch write handle, read
re-open while it is alive, immediate unlink on Unix;
FILE_FLAG_DELETE_ON_CLOSE carries the same lifetime on Windows
(the re-opened handle keeps the delete pending until the
descriptor itself closes). The returned path is a name record
only; FileFd::reopens_fresh already falls back to a dup when the
name no longer resolves, which matches GNU's unnamed-fd shape.

Same-wave residual found by the pin: the legacy here-string
fallback in materialize_async_stdin_body returned the bare expanded
word, dropping the trailing newline GNU appends for
r_reading_string (redir.c:396-397 heredoc_expand; the correlated
path appends it in expand_heredoc_redirect_text), so every async
here-string (cat <<<word &) lost its newline. The fallback now
appends it.

Regression pins: tests/issue2409_async_stdin_temp_residue_regressions.rs
(isolated TMPDIR listing unchanged after simple/brace-group/
subshell async heredocs, an async here-string, and the inherited
virtual-stdin shape; bodies delivered byte-identical). GNU bash
5.3.20 script-file expectations.

Refs #2409
@caomengxuan666
caomengxuan666 deleted the fix/r41-exec-stdin branch November 2, 2026 01:11
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