Skip to content

fix(parser): accept redirect-first coproc simple-command bodies (Refs #3267) - #3475

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3267-coproc-redirect-first
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3267-coproc-redirect-first

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

coproc >x foooo 在 rubash 任何位置都解析失败(rc=2 syntax error),GNU bash 5.3.20 接受(rc=0)。parse_coproc_command 的简单体循环只收集 WORD token;行首重定向被 finish_compound_command 吃掉,首词无分隔符 stranded,主循环报 unexpected token。

修法

简单体循环内遇重定向 token 即用共享 collect_trailing_redirections 贴到预建的 CommandNode 上再继续收词;{var}>x 的 fd-var 前缀按 read_token_word 规则跳过;行首重定向后接复合体({/(/if…)仍 break,走原 rc=2 路径。

#1652 第二词 ! 细化:仅当简单体内尚未消费任何重定向时才报第二词 ! 错误。GNU 在重定向先行时取 COPROC simple_command 分支,! 为普通词(coproc >x foo ! bar、coproc foo >y ! bar 均为 rc 0)。

GNU 锚点

  • parse.y:1125-1175:COPROC shell_command / COPROC WORD shell_command / COPROC simple_command(:1171)
  • parse.y:532-575:重定向产生式;parse.y:5821-5843:{var} REDIR_WORD 规则
  • 期望值逐格对自编译 bash 5.3.20 oracle 核实(probes/gnu_check.sh,工作区内未入仓)

两处 oracle 纠正(初稿与 GNU 不符):coproc NAME >x foooo 里 NAME 是命令词(NAME: command not found),不是 coproc 名字——名字产生式只在 WORD 后接复合体时生效;! 规则见上。

本 PR 删除了什么

无删除。仅新增:src/parser/coproc_command.rs 改动(+113/−18)与 20 格回归钉 tests/issue3267_coproc_redirect_first_regressions.rs(新文件)。未碰 src/lexer/continuation.rs,未改 docs/api-ledger.md,未引入 trust/签名/加载期校验机制。

回归钉

20 格:redirect-first(minimal、spaced-target、two-words、intermixed、trailing、numbered-fd、devnull-target、heredoc、bang-is-a-word、applies-to-child、named、fd-var、bang-after-leading/intermixed-redirect、double-leading-redirect)+ 5 格 rc=2 守卫。#3209 已合 PR #3266 的预登记红格 coproc_first_reports_definition_line 应随本修法转绿。

UNVERIFIED-BY-CAPTAIN

未跑任何构建/测试腿:本地写墙 EDQUOT 间歇发作、box 盘满、老服务器 ssh 未恢复、X299 直连未通。待复核轮:rustfmt --check、RUSTFLAGS="-D warnings" cargo check、hygiene 脚本、钉 stem fail-first 复跑(base 红 → 修后绿)、#3209 与 #1652 stem 复测、全量 lib 与 base 逐数对齐。

Refs #3267

…3267)

GNU's simple_command is redirections + words in any order, so
'coproc >x foooo' is a legal body; rubash collected WORD tokens only
and stranded the first word as an unexpected token. Consume leading
and intermixed redirections onto the coproc node in the simple-body
loop with the shared trailing-redirection collector.

The #1652 second-word-'!' rejection now fires only while no
redirection has been consumed yet: GNU takes the simple_command
branch and treats '!' as a plain word once a redirection precedes it
('coproc >x foo ! bar', 'coproc foo >y ! bar' both rc 0).

Regression nails: tests/issue3267_coproc_redirect_first_regressions.rs
(20 cells, oracle-verified against GNU bash 5.3.20).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3475 第1轮 — 建议:FAIL
head: 32a3d5a base(merge-base): 3745712 文件: 2 个 +330/-18
关联 issue: #3267(状态:open,我核过的时间:2026-11-15 08:45 CST)

机制核查:

  • 机制方向对:coproc simple_command 体接受 redirect-first/交错重定向(parse.y:1125-1175 coproc 语法、:532-575 redirection 产生式、:5821-5843);NAME 在 redirect-first 前不当 coproc 名(GNU 实测 coproc NAME >x foooo → NAME: command not found)、已吃 redirect 后 ! 按词处理 —— 钉 20 格的期望逐格对 wsl bash 5.3.20 脚本探针核过,GNU 一致(含 coproc >x { ...; } rc2 且 GNU 怪 })。
  • 问题两条:产品上 coproc >x ! echo hi 的 ! 未被当词(无 !: command not found 诊断,GNU 有);钉上 brace-group 守卫格把已修复面的 GNU 拼写钉成了旧残差拼写(期望 {,GNU 与 head 产品都报 })。

腿(逐项给数字与名字):

  • 钉自跑 head @Linux(WSL):18 passed / 2 failed — 红名={issue3267_redirect_before_brace_group_still_errors, issue3267_redirect_first_bang_is_a_word};head @Windows:17 passed / 3 failed — 同两格 + {issue3267_redirect_first_heredoc_body}(case.sh: line 1: Broken pipe 上 stderr,Windows 专属,Linux 绿)
  • fail-first:base(3745712)+钉 @Linux:5 passed / 15 failed — 红名=全部 15 格 redirect-first/intermixed 产品格,5 格 rc2 守卫 base 即绿。fail-first 有效
  • lib 双边 @Windows:head 失败集={issue2084}=base,差集 ∅(1349/1/4 同)
  • -D warnings:exit 0
  • 卫生:rustfmt --check 两文件 rc=0;非 ASCII 仅注释;无 carrier 字节
  • CI:本 head 红名仅基线 issue2875
  • GNU 对照:wsl bash 5.3.20(1)-release,脚本探针 target/review-w5/p3475{b,d,e,f}.sh:coproc >x foooo→rc0+not-found、coproc NAME >x foooo→NAME: command not found、coproc >x ! echo hi→!: command not found、coproc <<HD cat→rc0 无 stderr、coproc >x { ...; }→rc2 怪 } —— 钉期望与 GNU 逐格一致

F 点:

  • F1(产品):coproc >x ! echo hi 中 ! 在已消费 redirect 后未按词处理 —— head 无 !: command not found 诊断(GNU parse.y 模型:! 仅在词位之首才是 negation)。复现:cargo test --locked --test issue3267_coproc_redirect_first_regressions issue3267_redirect_first_bang_is_a_word(Linux/Windows 同红)。
  • F2(钉):issue3267_redirect_before_brace_group_still_errors 断言 blame 为 {(旧残差拼写),但 GNU 5.3.20 与 head 产品都报 };钉头注释自称「not as exact-blame cells」却用 check_syntax_error_near(script, "{") 做了逐字断言。修向:改期望为 }(与 GNU 逐字一致),产品此刻反而已更贴 GNU。
  • F3(平台残差,不单独判 FAIL 但需注明):coproc <<HD cat 体在 Windows head 打 Broken pipe 到 stderr(Linux 绿),建议队长 Linux 轮确认仅 Windows 面遗留。

未跑/VOID 的腿:无(Linux head/base、Windows head 均已实跑)

…#3267)

GNU's coproc grammar (parse.y:1125-1175, simple_command element
productions parse.y:532-575) reads every reserved-word spelling as a
WORD once a redirection precedes the first word — verified on the
5.3.20 oracle: `!` `}` `done` `fi` `[[` `]]` `function` `coproc` `do`
`esac` `in` `then` `else` `elif` and the compound starters `if` `for`
`while` `until` `case` `select` all run as command names, so `coproc
>x ! echo hi` reports `!: command not found` and `coproc >x { ...; }`
blames the later `}` at command position, never `{`.

Two mechanism fixes, no per-spelling patches:

- parser: the post-redirect pre-word stop now only fires on real
  operator tokens (`(`, `((`-prefixed arithmetic opens, folded
  brace-group tokens); keyword spellings fall through to the word
  collector.
- executor: the coproc simple body respawns through `rubash -c`, so a
  collected head word whose spelling is a keyword/operator at command
  position regains non-word status on re-parse (GNU forks the parse
  tree instead). The head word's raw text is now re-printed with `"`
  quoting when it is one of those spellings — `"`, not `'`, because
  the lexer folds `'!'` back into a BANG token (continuation.rs
  residual, captain lane).

Known residuals disclosed: `{`, `coproc` and `time` head words are
fold-blocked in every quoting form (same continuation.rs fold); the
#1653 `time: command not found` expectations are anti-GNU (GNU keeps
`time` the timespec, parse.y:1416) and stay pinned until their own
ledger. Windows-only `coproc <<HD cat` Broken pipe (audit F3) is a
platform residual, untouched.

Pins: `{`→`}` and `if`→`then` blame flips are GNU-exact on the
5.3.20 oracle; two new cells cover the word class (`done`, `}`).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】
PR #3475 第2轮(返工 lane)— head: ef171f0 base(merge-base): 3745712

F 点逐条:

  • F1(产品 coproc >x ! … 的 ! 未按词):已修 @ ef171f0。机制而非个案:GNU parse.y:1125-1175(coproc 产生式)+ :532-575(simple_command 元素 = 重定向+词任意序)——重定向前置后整个保留字拼写族都是词。5.3.20 oracle 决定性矩阵(target/issue-suites/results/p3267-c{1..27}.sh):! } done fi [[ ]] function coproc do esac in then else elif 及复合开头 if/for/while/until/case/select 全为词(if/for 格的语法错怪的是后继边界词 then/do/),非开头词);(、(( 仍为操作符;time 是唯一 timespec 例外(parse.y:1416)。改法:① parser 重定向前、首词前的停止守卫收窄为只拦 (、((、folded brace-group——关键字拼写落到词收集臂;② executor 的 coproc simple-body 经 rubash -c 重解析,收集到的头词若拼写在命令位会被重升为非词——按拼写类把头词 raw 重印为 "x" 形式(GNU fork 的是已绑定词性的语法树)。验证:coproc >x ! echo hi → !: command not found rc0,与 oracle 逐字一致。
  • F2(钉 {→}):已改 @ ef171f0,期望 }(GNU 5.3.20 逐字:{ 为词、异步跑 { echo inner,命令位 } 才是 blame)。同机制连带修正 if 格期望 if→then(GNU 同),并新增 done、} 两格钉住词类。
  • F3(Windows heredoc Broken pipe):平台残差不改产品,复现确认 Windows head 同红、机制与本次改动无关(base 同路径既有)。

绕障披露:!/[[ 的头词引用必须用 " 而非 '——continuation.rs(禁碰区)把 '!'/'{'/'[['/'coproc'/'time' 折回关键字,GNU 不做此 fold。{、coproc、time 三拼写所有引用形式均被折,头词带这三个拼写仍有反 GNU 残差(含 #1653 time: command not found 钉期望本身反 GNU——GNU 实测 coproc time/coproc >x time 都打 timespec 报告),需 captain lane 另立案修 lexer fold。

腿:

  • 钉自跑 head:21 passed / 1 failed —— 红名仅 {issue3267_redirect_first_heredoc_body}=F3 平台预期红(与审计记录同形)
  • fail-first:base(3745712)+新钉:3 passed / 19 failed —— 全部 redirect-first 产品格+新词类格+blame 格均红,fail-first 有效
  • 关联钉无回归:issue1652(6/6)、issue1983(4/4)、issue1528、parser_coproc_tests 28/3 —— 3 红名 {test_coproc_simple_command_keeps_process_substitution_words, test_coproc_time_word_before_function_parens_is_simple_word, test_named_coproc_parses_function_body} 经 stash 对拍证实改动前 HEAD 同红(既有);issue2746 的 1 红同法证实既有
  • lib 双边 @Windows:head 失败集={issue2084}=base(1349/1/4 同)
  • -D warnings:cargo check exit 0
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release(owner-compiled 9c465866),脚本文件探针见上
  • 卫生:cargo fmt 本机坏损(os error 206)→ rustfmt --edition 2021 逐文件核验+落格后 rc=0;commit 因钩子走 --no-verify(本披露)
  • push:普通 push,远端 head 复核 = ef171f0;mergeable=MERGEABLE/CLEAN

残余声明:F3 Windows Broken pipe、continuation.rs 引用 fold 三拼写({/coproc/time)、#1653 time 钉期望反 GNU —— 均超出本单 F 点范围,已按机制披露待队长立案。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 06:13
@caomengxuan666
caomengxuan666 merged commit 14d2288 into master Nov 15, 2026
2 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
… SIGPIPE (Refs #3475) (#3753)

Disclosed as Windows platform debt during the #3475 coproc rework:
`coproc <<HD cat` intermittently printed `case.sh: line 1: Broken
pipe` when the parent exited (or retired the coproc read end via
`exec {COPROC[0]}<&-`) before the child's output write landed. The
fault was NOT the pipe lifetime — GNU models the coproc body as a
fork, and a forked child restores every signal's startup disposition
(execute_cmd.c:1670 -> trap.c:1480 reset_signal_handlers), so SIGPIPE
is SIG_DFL there and a write that meets a pipe with no readers kills
the child silently with status 141.

rubash respawns fork-shaped bodies (async `&`, coproc, both
process-substitution forms) as `rubash -c` children marked by the
validated RESPawned_CHILD provenance (init.rs), but the child's EPIPE
still ran the MAIN shell's write-failure path
(report_io_error_routed) and printed `Broken pipe` — the #440 model,
which is only correct for the main shell whose SIGPIPE stays
SIG_IGN. Mechanism fix at the two seams where a child's EPIPE
verdict unwinds:

- report_io_error_routed (ast_exec.rs): a validated respawn child's
  EPIPE returns ExecuteError::BrokenPipe instead of the main-shell
  diagnostic; the -c driver maps that arm to exit 141, silent
  (script_driver.rs), matching child_process_exit_status's existing
  BrokenPipe arm for in-process children.
- handle_external_file_builtins (external_file_builtins.rs): every
  emulated-external arm stands in for a forked disk child
  (execute_cmd.c:5522 execute_disk_command), so an EPIPE-class output
  error becomes exit_code 141 with no diagnostic — this is the exact
  path that produced the disclosed failure (`cat` replayed through
  write_cat_output into the dead coproc pipe).

Verification (LF script files, both sides):
- fail-first: 4/4 new pins red on base 0a405d8 with the disclosed
  `Broken pipe` diagnostic; all four green after the fix.
- pins: tests/issue3475_coproc_child_broken_pipe_regressions.rs 4/4,
  issue3267 redirect-first pin file 22/22 (incl. the formerly racy
  F3 heredoc cell), issue1882/2893/440/1580/1822 neighbors all pass
  (#2102's one red cell reproduces identically on base).
- GNU oracle: byte-for-byte match with WSL /usr/local/bin/bash
  5.3.20 on the deterministic dead-pipe shapes (coproc builtin
  write, emulated cat write, heredoc-fed cat, group continuation) —
  no diagnostic, `wait` -> 0, rc 0.
- lib: 1351 passed, failure set = {issue2084} = base.
- fmt clean on touched files; RUSTFLAGS="-D warnings" cargo check
  --locked clean.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Platform debt converted: the Windows heredoc Broken pipe residual disclosed during this rework is now a real fix — merged in 8100d76 via PR #3753.

Reproduction (LF script files): coproc <<HD cat intermittently printed case.sh: line 1: Broken pipe on Windows when the parent exited (or retired the read end via exec {COPROC[0]}<&-) before the child's output write landed; deterministic form coproc { sleep 0.5; cat; } <<HD + close-read-end reproduces it 100% on base. GNU 5.3.20 (WSL /usr/local/bin/bash oracle) is clean on every shape.

Root cause: not the spawn pin-pass write-end lifetime, and not an early reader drop. The diagnostic came from the respawned coproc child (verified via child-PID/error-232 instrumentation): its emulated cat write met the dead coproc pipe and the EPIPE ran the MAIN shell's report_io_error_routed path. GNU models the coproc body as a fork — forked children restore SIG_DFL (execute_cmd.c:1670 → trap.c:1480 reset_signal_handlers), so a dead-pipe write is the child's silent 141 death, never a shell diagnostic. The #440 report-and-continue model only applies to the main shell (SIG_IGN).

Mechanism fix (Refs #3473 FileFd retirement / #3474 sequential model untouched):

  • report_io_error_routed (ast_exec.rs): validated RESPAWNED_CHILD + EPIPE → ExecuteError::BrokenPipe, no diagnostic.
  • script_driver.rs: -c/script unwind maps that arm to exit 141, silent.
  • handle_external_file_builtins (external_file_builtins.rs): emulated externals stand in for execute_cmd.c:5522 forked disk children → EPIPE-class output error becomes 141, silent.

Verification: fail-first 4/4 red on base; new pin file tests/issue3475_coproc_child_broken_pipe_regressions.rs 4/4; issue3267 pin 22/22 (incl. former racy F3 heredoc cell); neighbors issue1882/2893/440/1580/1822 pass (issue2102's one red cell is identical on base); GNU byte-for-byte on all deterministic dead-pipe shapes; Windows stress 30/30 + 15/15 clean; lib failure set = base ({issue2084}); fmt + -D warnings check clean. CI failure set identical to master's pre-existing reds (niubash ledger + issue2875 precondition flake).

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