Skip to content

WIP: fix numbered-fd heredoc delivery to external children (#2270) - #2271

Merged
caomengxuan666 merged 7 commits into
masterfrom
fix/2270-ext-fd-heredoc
Oct 30, 2026
Merged

caomengxuan666 merged 7 commits into
masterfrom
fix/2270-ext-fd-heredoc

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: Refs #2270

Problem

./reader.sh 3<<EOF left the external child's fd 3 unbound — reads
failed EBADF — while the same body on fd 0 and a plain 3<file both
worked. In spawn_external_process's own-redirects loop, a
HereDoc / HereString entry fell into the write bucket (its kind is
in none of the input exclusions), so the loop "opened" the
delimiter word as an output file in the cwd and delivered that
descriptor instead of the body.

Fix (one installation contract, by mechanism)

GNU's contract is fd-number-agnostic: do_redirection_internal's
r_reading_until / r_deblank_reading_until / r_reading_string arm
calls here_document_to_fd (redir.c:423), which places the body in
a temporary file, and dup2's the resulting descriptor onto the
redirector (redir.c:1065-1072).

The loop now special-cases only the SOURCE of the binding: a
heredoc / here-string entry materializes the canonical body text
(external_fd_heredoc_input — the ladder the /dev/fd operand and
<&N source faces already use) into a carrier file via
write_process_substitution_temp (registered as an adhoc carrier,
the route seam's precedent), and the ordinary input arm opens it
and delivers it through the same __RUBASH_FD_* env keys and Unix
pre_exec pins as a file input redirect. No parallel numbered-fd
implementation; fd 0 keeps its existing
apply_external_stdin_redirect path; in-process faces (builtins /
functions) keep binding the same body as an fd-table text entry.
Last-wins across repeated redirects on one fd follows from the
loop's list order (do_redirections, redir.c:237).

Pins

tests/issue2270_external_numbered_fd_heredoc_regressions.rs,
10 pins: << and <<- on fd 3, fd 4, here-string, last-wins,
quoted-delimiter literal body, child exit code after reading the
body to EOF, no stray delimiter file, plus fd 0 and plain
3<file controls. Expectations byte-verified against GNU bash
5.3.20 via probes/probe-2270.sh (cluster workdir).

Verification split (compile rationing)

Refs #2270

`./reader.sh 3<<EOF` left the child's fd 3 unbound (reads failed
EBADF): in spawn_external_process's own-redirects loop a HereDoc /
HereString entry fell into the write bucket (its kind is in none of
the input exclusions), so the loop "opened" the delimiter word as
an output file in the cwd and delivered THAT descriptor instead of
the body.

GNU's contract is fd-number-agnostic: do_redirection_internal's
r_reading_until / r_deblank_reading_until / r_reading_string arm
calls here_document_to_fd (redir.c:423), which places the body in
a temporary file, and dup2's the resulting descriptor onto the
redirector (redir.c:1065-1072) — the same installation for fd 0,
fd 3, or any other number.

Mirror that contract in the one spawn-side loop: a heredoc /
here-string entry now materializes the canonical body text
(external_fd_heredoc_input — the ladder the /dev/fd operand and
<&N source faces already use) into a carrier file via
write_process_substitution_temp (registered as an adhoc carrier,
the route seam's precedent), and the ordinary input arm below
opens it and delivers it through the same __RUBASH_FD_* env keys
and Unix pre_exec pins as a file input redirect. fd 0 keeps its
existing apply_external_stdin_redirect path; no parallel
numbered-fd implementation is added. Last-wins ordering across
repeated redirects on one fd follows from the loop's list order
(do_redirections, redir.c:237).

Regression pins in tests/issue2270_external_numbered_fd_heredoc_
regressions.rs (10 pins): << and <<- on fd 3, fd 4, here-string,
last-wins, quoted-delimiter literal body, the child's exit code
after reading the body to EOF, no stray delimiter file, plus fd 0
and plain 3<file controls. Expectations byte-verified against GNU
bash 5.3.20 via probes/probe-2270.sh. The <<- pin also needs the
numbered-fd strip-tabs lexer fix (PR #2269 / #2265) and stays red
for that reason alone on a tree without it.

Local verification: cargo check only (compile rationing); pins
are pending the collector's batch run.
caomengxuan666 and others added 5 commits October 30, 2026 18:15
Resolve external_inner.rs against #2253/#2263: keep the #2235 stash lookup ahead of the #2270 heredoc arm (a heredoc entry still classifies as write at lookup time, so it can never claim a stash — matching the dup replay, which kind-skips heredocs; a same-fd stash belongs to an earlier plain entry and the heredoc's later stamp preserves last-wins). Semantic check: diff vs master is exactly this PR's change set.

Refs #2270
@caomengxuan666
caomengxuan666 merged commit c17160e into master Oct 30, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2270-ext-fd-heredoc branch November 2, 2026 01:09
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