Skip to content

fix(executor): coproc honors here-string/heredoc stdin redirects (rubash#1651) - #1694

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix-1651-coproc-heredoc-stdin
Oct 26, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix-1651-coproc-heredoc-stdin

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Fixes #1651.

GNU redir.c do_redirections binds fd 0 to the redirect before the coproc child runs, so the coproc consumes the body and exits. Rubash's apply_coproc_redirects only handled the single-field redirects (redirect_in/out/append/err), and a memory-backed body never lands in redirect_in — a here-string lives in cmd.redirects + cmd.here_string, a heredoc in cmd.redirects + cmd.heredoc_redirects — so the child stayed on the coproc pipe and coproc cat <<< hi blocked forever (while < file worked).

The fix resolves the winning fd-0 body (same last-wins ordering as stdin_string_for_command_mut, whose here-string expansion also strips quotes segment-wise — the read-only fallback left them, feeding "hello" with quotes) and feeds it to the child through a pre-filled pipe (writer thread, so a body larger than the pipe buffer cannot block the spawn; the parent's coproc write end stays registered as ${COPROC[1]}, useless but present, like GNU's rebound pipe end). Only HereDoc/HereString winners take this path: < file keeps the real-file path, and <&N dups keep the pipe (streaming a dup through a string buffer would change blocking behavior).

Verification (local, master 499a5e2 + this fix, vs GNU bash 5.3.20):

  • coproc cat <<< "hello" / heredoc form: child exits on its own (DEAD, wait_rc 0) instead of blocking; content, quoting, and variable expansion all match GNU; compound body + herestring works; last fd-0 redirect wins in both directions; 500-line and 200KB heredocs transfer without deadlock.
  • New regression file tests/issue1651_coproc_heredoc_stdin_regressions.rs: 10/10 passed; existing coproc suites (parser_coproc_tests, issue352, issue387, issue1528) still pass.

Known pre-existing divergence, unchanged here and unrelated to this issue: reading a coproc fd inside a pipeline stage (cat <&${COPROC[0]} | wc -l) yields 0 lines on rubash (master included) while GNU reports 'Bad file descriptor' — pipeline-stage coproc-fd handling needs its own issue.

…ash#1651)

GNU redir.c do_redirections binds fd 0 to the redirect
before the coproc child runs, so the coproc consumes the
body and exits. Rubash's apply_coproc_redirects only
handled the single-field redirects (redirect_in/out/
append/err), and a memory-backed body never lands in
redirect_in — a here-string lives in cmd.redirects +
cmd.here_string, a heredoc in cmd.redirects +
cmd.heredoc_redirects — so the child stayed on the coproc
pipe and 'coproc cat <<< hi' blocked forever (while
'< file' worked).

The fix resolves the winning fd-0 body (same last-wins
ordering as stdin_string_for_command_mut, whose
here-string expansion also strips quotes segment-wise —
the read-only fallback left them, feeding '"hello"'
with quotes) and feeds it to the child through a
pre-filled pipe (writer thread, so a body larger than
the pipe buffer cannot block the spawn; the parent's
coproc write end stays registered as ${COPROC[1]},
useless but present, like GNU's rebound pipe end). Only
HereDoc/HereString winners take this path: '< file'
keeps the real-file path, and '<&N' dups keep the pipe
(streaming a dup through a string buffer would change
blocking behavior).

Regression tests:
tests/issue1651_coproc_heredoc_stdin_regressions.rs (10 tests:
herestring/heredoc consumed + exit, content + expansion +
quotes, file control, last-wins both directions, compound
body, 500-line heredoc, numbered/empty herestring).
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.

[bug] coproc ignores herestring/heredoc stdin redir; coproc blocks on pipe instead (GNU consumes it and exits)

2 participants