Skip to content

executor: port GNU async-stdin verdict to pipe-fed pipeline elements - #3734

Merged
caomengxuan666 merged 3 commits into
masterfrom
devin/pipe-stdin-redir
Nov 16, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
devin/pipe-stdin-redir

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Refs #3605 — exec13.sub's pipe-fed compound elements (echo x | ( cat & wait ), | { cat & wait; }, function elements) lost their piped lines: the element's async cat & spawned with /dev/null stdin because the element clone never inherited the "my stdin is a pipe" verdict. Upstream gate run-execscript shows the x1..x3b family blank; the GNU golden expects all eight lines.

GNU anchors (vendored bash-5.3 tree):

  • execute_cmd.c:4570 — the forked element child ORs the verdict: stdin_redir |= (pipe_in != NO_PIPE).
  • execute_cmd.c:1733 — execute_subshell_command ASSIGNS stdin_redir = (own fd-0 redirects) || (pipe_in != NO_PIPE); a nested ( ) clears an enclosing verdict and the element's own < file replaces the pipe via do_redirections.
  • execute_cmd.c:1759-1764 — the :826 assign for control structures runs after do_redirections consumed the element's fd-0 redirect, so it must not re-set the flag.
  • eval.c:181 — only reader_loop clears stdin_redir between commands; evalstring.c's parse_and_execute does not.

Port

  • execute_pipeline_stage gains a stdin_is_pipe parameter; the stage entry ORs it into shell_state.stdin_redir and the epilogue restores it. Real-OS-pipe stage call sites pass true; producer/stage-0 and the timed-read followup pass false. GNU's lastpipe quirk (prev = NO_PIPE before dispatching the last element) means the lastpipe path needs no marker — verified: shopt -s lastpipe; echo x9 | ( cat & wait ) prints nothing.
  • New one-shot element_pipe_stdin marker (Cell<bool> on ShellState): set on the compound-stage clone, consumed at the clone's own compound dispatch — the ( ) :1733 assign (own redirects || marker) and the :826 assigns in command_execute/execute_brace_group_pipeline, which GNU skips for pipe-connected elements.
  • ast_exec's per-command reader-loop clear now only fires at subshell_depth == 0 (eval.c:181); a stage clone's body walk is not a reader loop.
  • bind_command_fd0_input drops the FUNCTION_STDIN(+_OFFSET) mirror after binding a real fd-0 endpoint (file / procsub / net): GNU's do_redirections replaces fd 0 wholesale, so a stale pipe mirror must not shadow the bound file for inherited_async_stdin.

Verification

  • Byte-identical to GNU bash 5.3.20 (script files, stdout+stderr+rc): exec13 shapes (x1/x1a/x2/x2a/x2b/x3/x3a/x3b incl. GNU's empty x2/x2a and x3b-sourced + lastpipe forms), and a 13-form probe matrix — pipe-fed ( )/{ }/if/while/for/case/select/coproc elements, element-own-< file override, nested-clear assign semantics, stage-scoped flag death.
  • niubash gate slice run-execscript: the x1..x3b family now matches exec.right. Remaining diffs are the already-tracked E2BIG (fix(executor): report E2BIG wording for an oversize command line (Refs unixwin/niubash#993 S4) #3712), exec -a prolog, coproc and job-control noise.
  • tests/issue3605_pipeline_async_stdin.rs pins the four shapes (4 tests, all green); stdin/lastpipe regression slice green (6 files).

Refs #3605

exec13.sub's pipe-fed compound elements (`echo x | ( cat & wait )`,
`| { cat & wait; }`, function elements) lost their lines: the async
child spawned with /dev/null stdin because the element clone never
inherited the "my stdin is a pipe" verdict.

GNU anchors (bash-5.3 vendored tree):

- execute_cmd.c:4570 — the forked element child does
  `stdin_redir |= (pipe_in != NO_PIPE)`; a pipe-connected element's
  async spawns read the pipe, not /dev/null.
- execute_cmd.c:1733 — `execute_subshell_command` ASSIGNS
  `stdin_redir = (own fd-0 redirects) || (pipe_in != NO_PIPE)`; a
  nested `( )` clears an enclosing verdict, and the element's own
  `< file` replaces the pipe via do_redirections.
- execute_cmd.c:1759-1764 — the :826 `stdin_redir = redirects` assign
  for shell control structures runs AFTER do_redirections consumed
  the element's fd-0 redirect, so it must not re-set the flag.
- eval.c:181 — only reader_loop clears stdin_redir between commands;
  evalstring.c's parse_and_execute does not, so a clone replaying a
  pre-parsed element list must not clear either.
- jobs.c:1743/nojobs.c child_setup — the async child consults the
  verdict when picking /dev/null vs inherited fd 0.

Port:

- `execute_pipeline_stage` gains a `stdin_is_pipe` parameter; the
  stage entry ORs it into `shell_state.stdin_redir` and the epilogue
  restores the caller's flag. Real-OS-pipe stage call sites (loop
  stages index > 0, streaming tail/mid) pass true; producer/stage-0
  and the timed-read followup pass false. GNU's lastpipe quirk
  (prev = NO_PIPE before dispatching the last element) means the
  lastpipe path needs no marker.
- New one-shot `element_pipe_stdin` Cell on ShellState: set on the
  compound-stage clone, consumed at the clone's own compound
  dispatch — the `( )` :1733-style assign
  (`own fd-0 redirects || marker`) and the :826-style assigns in
  `command_execute`/`execute_brace_group_pipeline`, both of which
  GNU skips for pipe-connected elements.
- ast_exec's per-command reader-loop clear now only fires at
  subshell_depth == 0 (eval.c:181); a stage clone's body walk is not
  a reader loop.
- `bind_command_fd0_input` drops the FUNCTION_STDIN(+OFFSET) mirror
  after binding a real fd-0 endpoint (file, procsub, net): GNU's
  do_redirections replaces fd 0 wholesale, so a stale pipe mirror
  must not shadow the bound file for `inherited_async_stdin`.

Verification vs GNU bash 5.3.20 (script files, byte-identical
stdout/stderr/rc): exec13.sub shapes x1/x1a/x2b/x3/x3a incl. GNU's
empty x2/x2a, x3b + lastpipe forms, and a 13-form probe matrix
(pipe-fed `( )`/`{ }`/if/while/for/case/select/coproc elements,
element-own-`< file` override, nested-clear assign semantics,
stage-scoped flag death). niubash gate slice run-execscript: the
x1..x3b family now matches exec.right; remaining diffs are the
already-tracked E2BIG/#3712, exec -a prolog, coproc and job-control
noise. tests/issue3605_pipeline_async_stdin.rs pins the four shapes
(4 tests).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

Dedup note from a parallel exec13 session (built + matrix-verified this branch's head 6fd4838a before abandoning a duplicate implementation on #3755): this branch passes exec13.sub (x1..x3b) and the full async-stdin matrix byte-for-byte vs GNU 5.3.20.

Extra cases verified on this branch that the current pin file doesn't cover, if you want them as added pins:

  • { cat & wait; } / ( cat & wait ) at top level with ambient stdin -> prints nothing (stdin_redir cleared per reader_loop command, eval.c:181).
  • { ( cat & wait ); } fed by echo x | -> nothing (nested ( ) resets, execute_cmd.c:1733 assignment semantics) — covers the group-element nesting, not just ( )-in-( ).
  • { cat & wait; } <<EOT -> heredoc text (element's own fd-0 redirect sets the flag).
  • exec <file; cat & wait -> nothing (exec rebinds fd0 via redirect_builtin, never touches stdin_redir).

No action needed — the family is owned here; #3755 dropped its duplicate and carries only the unrelated main.rs script-arg prolog commit.

Written by Devin

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3734 第1轮 — 建议:PASS
head: bd060f8(= merge 2ae1e13 + 钉修复 bd060f8;原 head 6fd4838 + origin/master 5d253cf)base(merge-base): 0587028 文件: 9 个(gh 口径)
关联 issue: #3605(伞目录,保持 OPEN)

机制核查:

  • ShellState::element_pipe_stdin 一次性标记 + pipeline stage helpers 透传 stdin_is_pipe;元素自有 fd0 重定向优先、嵌套 subshell 清标记、verdict 随 stage 消亡——与 GNU execute_cmd.c:4570(stdin_redir |= (pipe_in != NO_PIPE))、:1733(user subshell ASSIGN)、:1759-1764(do_redirections 先于 :826 控制结构 assign)、eval.c:181(仅 reader_loop 清位)一致。
  • pipeline_exec.rs merge 冲突已解(保留 master stdin_redir 语义 + 分支 element_pipe_stdin 分支);diff 无顺手改无关面。

腿:

  • 钉自跑:issue3605_pipeline_async_stdin — Windows head 4/4、Linux head(WSL rustup 1.99.0)4/4。
  • fail-first:merge-base 0587028 上 Windows 0/4、Linux 0/4 全红,红名 = 全部 4 钉(exec13_pipe_fed_element_async_stdin / element_own_fd0_redirect_wins_over_pipe / nested_subshell_clears_element_flag / element_stdin_verdict_dies_with_stage);Linux 基线红为语义红(nested_subshell 基线左值 "line1\nline2" vs 应然 "",非路径伪影)。
  • lib 双边(Windows cargo test --lib):head {issue2084} / base(5d253cf) {issue2084} / 差集 ∅(head 另有 7 个 executor::path 瞬时红,串行复跑 91/91 绿=负载抖动)。
  • -D warnings:cargo check --locked 通过,0 警告。
  • 卫生:rustfmt 涉及 9 文件全 CLEAN;marker-lint OK;utf8 无新增。
  • CI:head bd060f8 主 CI run 43915722367 仍 pending(niu-canary 两 job pass);CI 按名核查 VOID,待队长复核。
  • GNU 对照:WSL GNU bash 5.3.20(1)-release;probe-3605-pipe.sh P1-P4(exec13 五形状 / 自有 fd0 重定向 / 嵌套 subshell 清位 / verdict 随 stage 死)+ p2/probe-p2.sh(group/subshell/for 三形状自有重定向)head 输出与 GNU 逐字节一致。

F 点:无。

续作记录:

  • 2ae1e13 Merge remote-tracking branch 'origin/master' into devin/pipe-stdin-redir(pipeline_exec.rs 冲突解);bd060f876 fix(tests): quote fixture path in pipe-stdin pins for Windows (Refs macOS: GNU upstream gate 48/86 — catalog of remaining divergences #3605)——钉内 {f} 未加引号,Windows 下 C:\Users\... 反斜杠被 bash 当转义吃掉导致重定向落空(element_own_fd0 格假红、nested_subshell 格假绿);改为 '{f}' 后 Windows/Linux 双端 4/4 绿且基线语义红恢复。新 head: bd060f8

未跑/VOID 的腿:CI 全腿(run pending);lib 双边仅 Windows 侧(Linux lib 未跑,fd/管道语义差异风险低但注明)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 17:36
@caomengxuan666
caomengxuan666 merged commit 308e97f into master Nov 16, 2026
2 checks passed
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