Skip to content

WIP: async/procsub materialization faces (#2388, #2409, #2410) - #2419

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/async-procsub-materialization
Nov 1, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/async-procsub-materialization

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

WIP — async-subshell / process-substitution materialization cluster (three issues, one commit each; main session batch-verifies the pins under scrubbed fds before this leaves draft).

#2410 — procsub VALUE leak across commands

GNU recognizes a process substitution only while scanning the word's own characters at expansion time (subst.c:11365-11385 expand_word_internal, via subst.c:1311 extract_process_subst / subst.c:6362 process_substitute); a value arriving through parameter expansion is data and is never re-scanned. The post-expansion materializer re-scanned expanded word text whenever word metadata had been dropped, so x='<(echo hi)'; echo "$x" printed a /dev/fd/N path and >(...) values executed their bodies. Fix: the standalone arm now requires parse provenance from the node's substitution aggregate (cloned through expansion), and alias-introduced words (re-lexed source in GNU, parse.y push_string) get WordMetadata provenance recorded at alias expansion.

#2409 — async stdin materialization temp-file leak

GNU's here_document_to_fd (redir.c:423) makes the heredoc temp "really temporary" (redir.c:548-551): re-opened by name for reading, then unlinked before use. Both compound_exec.rs materialization faces (materialize_async_stdin_body for a background command's own heredoc/here-string; materialize_virtual_stdin_fd0 for an inherited virtual fd 0) created named rubash-process-subst-*.tmp carriers and never unlinked them — the #2395 leak class on this face. Both are now anonymous from birth (unlink after the read re-open on Unix, FILE_FLAG_DELETE_ON_CLOSE on Windows, mirroring the #2395 fix). Same-wave residual found by the pin: the legacy here-string fallback in materialize_async_stdin_body dropped the trailing newline GNU appends (redir.c:396-397); fixed in the same commit.

#2388 — heredoc body lost in async subshell respawn

The respawn text renderer (bash_command_source_text / bash_command_sequence_text) never emitted heredocs at all — headers live only in cmd.redirects as RedirectKind::HereDoc, no mirror carries them — so the respawned child re-parsed a heredoc-less command and the body vanished. The renderer now follows print_cmd.c: headers inline in the redirect list, bodies deferred past the whole command line (past |, &&/||, &) via a deferred-bodies queue the connector arms hold, newline separators after delimiter lines, and the same correlation helpers as the parser-side printer (heredoc_redirect_info / heredoc_header / heredoc_body_text, extracted to pub(crate)). The #2332 pin that documented the loss is updated to the full pre/body/post expectation.

Local verification (this session)

New pins, run against the locally built binary: issue2388 9/9, issue2409 5/5, issue2410 6/6. Neighbor pins: issue2332 12/12, issue2202 10/10, issue2058 12/12, issue2292 18/18, issue2270 10/10, issue2395 7/7, issue1862 6/6, issue1872 11/11, issue2237 1/1. Note: all three areas are fd/materialization-shaped — the new pins belong in the main session's scrubbed-fd batch. Known adjacent residuals NOT in this PR: heredoc as the last command of a case clause inside a respawned body (the ;; terminator placement), and the pre-existing tee <(echo t1) <(echo t2) hang on master.

Refs #2410
Refs #2409
Refs #2388

…nance

GNU recognizes a process substitution only while scanning the word's
own characters at expansion time: expand_word_internal's '<'/'>'
cases (subst.c:11365-11385) hand the in-word text to
extract_process_subst (subst.c:1311) / process_substitute
(subst.c:6362). A value arriving through parameter expansion is
appended as data and never re-scanned, so x='<(echo hi)'; echo "$x"
prints the literal text in any session state.

Rubash's post-expansion materializer re-scanned the EXPANDED word
text whenever word metadata had been dropped (expand_command_words
preserves metadata only for a small command set). A data word whose
value spells <(...) therefore materialized as if it were source
syntax: echo/cat printed a /dev/fd/N path, and >( ... ) values
actually executed their bodies with side effects.

Fix by mechanism, two halves:

- command_with_process_substitution_files: the no-metadata
  standalone arm now requires parse provenance — the node's
  process_substitutions aggregate (cloned through expansion) must
  record a word-position substitution (redirect_fd none) at this
  word index or with this exact source word as target. Data words
  have no record and stay literal.
- apply_alias_expansion_after_word_expansion: alias VALUE text is
  re-lexed by GNU at parse time (parse.y push_string), so its
  substitutions ARE source but arrived with no parse record.
  Record WordMetadata provenance for alias-introduced leading words
  carrying a substitution, so they materialize through the
  recorded-substitution arm.

Regression pins: tests/issue2410_procsub_value_leak_regressions.rs
(literal survival in all word positions, no execution of >(...)
values, cat-of-value is a filename, real word/redirect/diff/read/
for/alias/nested faces still materialize). GNU bash 5.3.20
script-file expectations.

Refs #2410
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
A respawned async child re-parses the parent's re-rendered command
text (bash_command_sequence_text / bash_command_source_text). GNU's
fork-shaped children execute the parsed tree instead, so a
here-document stays attached to its redirected command
(execute_cmd.c; redir.c:423 here_document_to_fd). The text renderer
never emitted heredocs at all — headers live only in cmd.redirects
as RedirectKind::HereDoc, no mirror field carries them, and neither
render path printed them — so the child re-parsed a heredoc-less
command and the body was silently lost (documented as the residual
in the #2332 pins).

The renderer now follows print_cmd.c:

- Headers print inline in the redirect list
  (print_redirection_list); bodies are deferred past the whole
  command line — past `|', `&&'/`||' connectors and the `&'
  terminator (print_heredoc_bodies / print_deferred_heredocs) — via
  a deferred-bodies queue the connector arms hold while rendering
  their elements, flushed by the outermost render or the list
  separator logic.
- A part ending with a delimiter line takes the newline separator
  (the delimiter admits nothing else on its line), and the
  separator/closer logic (push_command_separator, subshell / brace
  group / function closers) consults the same ended-with-heredoc
  fact.
- Header/body correlation reuses the parser printer's
  heredoc_redirect_info / heredoc_header / heredoc_body_text
  (extracted to pub(crate) free functions in ast_print.rs), so both
  printers agree on delimiter spelling, quoting and `<<-'.
- append_source_redirects renders a compound node's own heredocs
  (`( ... ) <<EOF', `done <<EOF'); its list-only loop no longer
  double-renders a numbered-fd heredoc as a filename target.

Regression pins: tests/issue2388_asyncsub_heredoc_body_regressions.rs
(the #2332 seed shape with the full pre/body/post expectation, plus
top-level async, unquoted-body expansion in the child, <<-,
two-heredoc sequences, heredoc in a pipeline, subshell-own redirect,
backgrounded heredoc inside a function body, and a sync control).
GNU bash 5.3.20 script-file expectations.

Refs #2388
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 1, 2026 03:23
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