Skip to content

WIP: coproc/procsub residual reconciliation (wave 3) - #1866

Merged
caomengxuan666 merged 9 commits into
masterfrom
fix/w3-coproc-recon
Oct 27, 2026
Merged

caomengxuan666 merged 9 commits into
masterfrom
fix/w3-coproc-recon

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

WIP: coproc/procsub residual reconciliation (wave-3 cluster w3-coproc-recon). Gate campaign COMPLETE (see cluster REPORT.md) — ready for main-session adjudication; do not merge on this PR's word.

Gates (artifact-verified): build rc=0; lib 773/1/1 with failure set identical to baseline (ownership_unary_operators_check_existing_file only); parser_tests green (incl. both #1859 pre-existing reds); parser_coproc_tests green; new pin targets all green; executor_tests process_substitution failure set identical to pristine f78396f (7 pre-existing #1638-family reds); diffcorpus 306/306; rustfmt clean on touched files; CR=0.

Fixes #1623
Fixes #1624
Fixes #1625
Fixes #1632
Fixes #1859
Refs #1584 (recon only — deferred to the direct-exec design line)
Refs #1638 (data-loss falsified on master; residual = deferred-harvest visibility family, harvest-line disposition)
Refs #1646 (data-loss falsified on master; same disposition as #1638)
Refs #1790 (falsified vs oracle — adjudication pin only, recommend close with evidence)

Full evidence: ~/workspace/issue-clearance/clusters/w3-coproc-recon/REPORT.md

The `-u N` coproc serve in read_input_for_command_inner re-derived
the read.def:949 EOF flag from the TRIMMED record text
(`!record_contains_delimiter(output, ...)`). trim_read_input has
already removed the delimiter, so every successful record — and
every -n/-N limit stop — scored as EOF-without-delimiter and the
builtin returned 1 despite serving the line.

read_coproc_stdout owns that accounting itself: it sets
read_eof_no_delimiter only when the pipe ends before the delimiter
arrives. Drop the caller's re-derivation (the `<&N` alias call site
already returns the record directly).

GNU bash 5.3.20 oracle (script-file probes): `read rc=0 line=[hello]`,
`read -n 3` rc=0. Regression pins: tests/issue1623_coproc_read_eof_regressions.rs.

Fixes #1623
…ndpoints

Two coupled defects kept a dead coproc's NAME array and NAME_PID
bound forever (`fds=[63 -1] pid=[<dead pid>]`):

1. retire_completed_coproc recognized a reaped pid as a coproc
   only when a CoprocStdout/CoprocStdin endpoint was still in the
   fd table. GNU coproc_reap (execute_cmd.c) walks the coproc list
   and runs coproc_unsetvars (execute_cmd.c:2450) unconditionally;
   rubash's registry is coproc_names, so recognition is now keyed
   on it, with the endpoint scan kept as a fallback. The leak
   triggered whenever both ends had left the table before the
   reap: the write end closed by `exec {NAME[1]}>&-` and the read
   end closed by an EOF read's endpoint close.

2. read_coproc_stdout closed the read endpoint on ANY loop exit
   (`ended`), including a deadline break and a SIGINT abort. Only
   a true pipe EOF (empty read or read error) may close it — GNU
   keeps the descriptor across builtin calls, so a timed-out
   `read -t` (rc 142) leaves the coproc fully usable.

GNU bash 5.3.20 oracle: issue seed settles to `fds=[] pid=[]` and
the re-read fails as `invalid file descriptor specification`;
timeout-then-reuse round-trips. Pins:
tests/issue1624_coproc_family_retire_regressions.rs.

Fixes #1624
…body parses

The seed forms were a whole-script syntax error (rc 2, `syntax
error near unexpected token '}'`) although the identical coproc
works at top level. matching_brace_group_end's
word_command_open_brace validates a `coproc NAME {` opener by
walking left over the prefix chain, but any KEYWORD predecessor
outside the four prefix keywords returned false — including the
enclosing group's own `{`, which is exactly the command-position
boundary that proves the coproc starts a command. The coproc's
`}` then closed the OUTER group in the depth scan and the rest
of the line was garbage.

The falsifying predecessor is a WORD (`echo coproc a {` keeps
`{` a plain argument — that arm is unchanged); a keyword
predecessor after a consumed prefix chain (`{`, `then`, `do`,
`else`, ...) confirms command position. The walk now returns
what it accumulated (parse.y:1125-1175 coproc productions).

GNU bash 5.3.20 oracle: seed prints grouped-ok/func-ok, rc 0;
a coproc spawned inside a group round-trips under its name.
Pins: tests/issue1625_coproc_nested_brace_regressions.rs.

Fixes #1625
…c pipe

`coproc RC { cat; } >file` broke the coproc: apply_coproc_redirects
replaced the child's stdout Stdio and dropped the pipe writer
before the spawn, so the pipe's only write end vanished and the
parent's read end reported immediate EOF (rc 1); the file check
then raced the never-fed child.

GNU applies the redirect in the forked child (execute_cmd.c),
where the displaced pipe end survives in a redirect save slot for
the child's whole life: the read end sees no EOF while the coproc
lives (read -t 1 times out, rc 142) and the data lands in the
file. apply_coproc_redirects now hands the displaced writer back
instead of dropping it: on Unix it rides into the child via
pre_exec (dup to the first free fd in the save-slot range
10..64; dup2 clears CLOEXEC so it survives the exec); elsewhere
the parent holds it in coproc_displaced_stdout_writers until
retire_completed_coproc drops it. Dup targets (`>&2`) displace
nothing and keep the old path — that seam is #1695's, untouched.

GNU bash 5.3.20 oracle: seed gives `read rc=142`, `file: abc`
for both `>` and `>>`. Pins:
tests/issue1632_coproc_redirected_stdout_regressions.rs.

Fixes #1632
…ody end

The #1024 newline fold in process_substitution_source fired for
EVERY mid-body line-break semicolon, rewriting the two canonical
reprints the #373/#381/#390 adjudication pins own:
`case esac in ; esac` became "case esac in\nesac" and a trailing
separator in `echo bye ;` became a newline — the two parser_tests
reds of #1859.

The fold is owed only between two commands (its purpose is body
diagnostic line numbering). A break before a closer/continuation
keyword (esac/fi/done/then/else/elif/do/}/)) or at the body's end
separates nothing, so it now falls through to the generic `;`
spelling; Branch A's after-`;` newline push is gated the same
way. `in` is deliberately NOT in the set: `case x ; in` is a GNU
syntax error (oracle-verified), so a break before `in` keeps the
verbatim newline. `(echo a ; )` is oracle-valid, so `)` is safe
in the set.

Regression pins (pre-existing, were red):
test_process_substitution_keeps_case_pattern_starting_with_esac,
test_process_substitution_keeps_command_after_heredoc_body
(tests/parser_redirection_tests.rs).

Fixes #1859
Re-measured on baseline master f78396f in the deterministic
shape (fd saved before the child ends, settle delay): both of
#1790's claims fail to reproduce — the second read DOES print
`read: 63: invalid file descriptor: Bad file descriptor` and the
target variable keeps its value, byte-identical to GNU bash
5.3.20, 3/3 on both sides. The shape's only real divergence was
the first read's status, which is #1623. The un-saved-fd form is
racy in GNU itself. Pin locks the GNU behavior; see the cluster
REPORT.md for the full evidence table.

Refs #1790
…ndoff

The Executor fork-copy literal in command_substitution.rs
enumerates every field and needed the new
coproc_displaced_stdout_writers entry (E0063), and
apply_coproc_redirects' displaced-writer return tripped a
conditional-move error (E0382): the writer now leaves through a
single if/else handoff into the return value.

Refs #1632
The token-level root of #1625: tokenize() of the seed line folded
the OUTER group as the single keyword token "{ coproc FC2 { cat; }"
— skip_brace's depth scan treated the coproc body's `{` as word
text (its predecessor word FC2 is not close-acceptable) while the
body's `}` still counted as a closer, so the fold ended one brace
early and the enclosing group's real `}` was left stray.

skip_brace already carries a `function NAME` chain for the
identical grammar shape (parse.y:1056 function_def, rubash#465);
the coproc productions (parse.y:1125-1175 `COPROC [WORD]
shell_command`) had no mirror. Add coproc_chain (0/1/2) plus
word_began_acceptable: stage 1 arms only when the `coproc` word
began at a close-acceptable (command) position, because GNU
produces COPROC only where a reserved word is acceptable
(parse.y:5899) — `echo coproc FC2 {` keeps `{` as word text and
still errors exactly like GNU. The chain rides the same reset
points as function_chain and is carried through the brace-scan
resume cache.

The companion parser-scan change (word_command_open_brace) from
the previous #1625 commit covers the unfolded token route with
the same invariant.

GNU bash 5.3.20 oracle: seed prints grouped-ok/func-ok rc 0;
guard shapes (echo function/coproc forms) unchanged. Pins:
tests/issue1625_coproc_nested_brace_regressions.rs.

Fixes #1625
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 27, 2026 12:14
@caomengxuan666
caomengxuan666 merged commit 3543ed3 into master Oct 27, 2026
8 of 9 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 27, 2026
…sh#1869)

Adjudication from reproduction on current master (bb360a9),
against GNU bash 5.3.20 script-file probes:

- Claim 1 (group route steals the first simple-command word as the
  coproc NAME): the parse is already unified - parse_coproc_command
  implements the parse.y:1125-1175 rule route-independently (a WORD
  is a NAME only before a compound/shell command). With a settle
  window the group, function-body and top-level forms are
  byte-identical: FC2 stays unbound, fds bind as COPROC, the child
  reports 'FC2: command not found'. What the recon observation saw
  as silence is a stderr-lifetime mechanism (coproc child stderr
  rides a pipe + forwarder thread whose read end dies with the
  parent), not a binding difference - recorded with deterministic
  probes in the cluster REPORT as a separate residual.
- Claim 2 (multi-line group, coproc first line, reports 'coproc:
  command not found' + stray '}'): the brace-fold defect family
  fixed for #1625 and merged in PR #1866 (03f7271 + bbf3b53) -
  the fold ending one brace early is exactly the stray-'}'
  mechanism. Broad context matrices on current master (pipeline,
  subshell, comsub, case, loops, if-condition, time, negation)
  show no reproduction.

These pins lock both shapes so the routes cannot drift apart
again. No product change.
caomengxuan666 added a commit that referenced this pull request Oct 27, 2026
* fix(jobs): flush pending signal notices at end of stream (rubash#1853)

A signal death in the stream's last command never printed its job
notice: report_pending_signal_notices only ran at the next top-level
command boundary in execute_ast, and at EOF there is no next command.
GNU prints without a following command - wait_for calls
notify_and_cleanup for the dead foreground job as the wait completes
(jobs.c:3385), and maybe_print_job_notifications prints whenever the
shell is not interactive (jobs.c:4723-4728), background deaths
included.

finish_shell (the script-file/stdin/-c drivers' shared finish path)
now replays the reap + report half of the boundary step once at
stream end, before run_exit_trap_with_status, matching GNU's order
(notice precedes the EXIT trap). The exec-replacement -c final unit
never reaches finish_shell, so that silent shape stays silent.

Pins: tests/issue1853_last_line_signal_notice_regressions.rs -
script last line, stdin last line, background death during the last
command, EXIT-trap ordering, followed-line exactly-once control,
-c brace-group final unit. 4 of 6 fail on pristine master; all pass
here. GNU expectations verified against bash 5.3.20 script-file
probes.

* test(coproc): pin the unified coproc NAME grammar across routes (rubash#1869)

Adjudication from reproduction on current master (bb360a9),
against GNU bash 5.3.20 script-file probes:

- Claim 1 (group route steals the first simple-command word as the
  coproc NAME): the parse is already unified - parse_coproc_command
  implements the parse.y:1125-1175 rule route-independently (a WORD
  is a NAME only before a compound/shell command). With a settle
  window the group, function-body and top-level forms are
  byte-identical: FC2 stays unbound, fds bind as COPROC, the child
  reports 'FC2: command not found'. What the recon observation saw
  as silence is a stderr-lifetime mechanism (coproc child stderr
  rides a pipe + forwarder thread whose read end dies with the
  parent), not a binding difference - recorded with deterministic
  probes in the cluster REPORT as a separate residual.
- Claim 2 (multi-line group, coproc first line, reports 'coproc:
  command not found' + stray '}'): the brace-fold defect family
  fixed for #1625 and merged in PR #1866 (03f7271 + bbf3b53) -
  the fold ending one brace early is exactly the stray-'}'
  mechanism. Broad context matrices on current master (pipeline,
  subshell, comsub, case, loops, if-condition, time, negation)
  show no reproduction.

These pins lock both shapes so the routes cannot drift apart
again. No product change.

---------

Co-authored-by: 曹梦轩 <[email protected]>
@caomengxuan666
caomengxuan666 deleted the fix/w3-coproc-recon branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment