Skip to content

WIP: coproc reap/disposal split — retire only at GNU disposal points - #2122

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/coproc-races-2053-2057
Oct 29, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/coproc-races-2053-2057

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: coproc reap/disposal split — the retirement half of a coproc completion now runs only at GNU's disposal points, so a wait $C_PID can no longer lose its operand to a boundary retirement (and exec {C[1]}>&- keeps its array until a real disposal point passes).

Fixes #2053
Fixes #2057

Root cause (one state machine, both issues)

GNU keeps the reap and the disposal in separate hands: waitchld records the status asynchronously (jobs.c:4173 → execute_cmd.c:2326 coproc_setstatus, which explicitly defers disposal out of the handler context), and coproc_reap (execute_cmd.c:2225) disposes only an already-reaped coproc, from cleanup_dead_jobs (jobs.c:1303) at specific disposal points:

  • the notify_and_cleanup inside a foreground wait_for (jobs.c:3385)
  • stop_pipeline after a command that forked (execute_cmd.c:963)
  • the REAP() after each loop iteration (execute_cmd.c:2975-2983; call sites :3105/:3267/:3606/:3838)
  • the wait/jobs builtins, and stream end

Rubash's boundary refresh instead discovered a coproc death with its own try_wait and retired the coproc (unbind <name>/<name>_PID, close endpoints) in the same motion, at any command boundary — including the boundary immediately before a wait expanded its operand (operand stolen → bare wait 0 / spurious 127) or before an exec {C[1]}>&- (array gone → ambiguous redirect).

Changes

  • apply_job_completion only records a coproc completion now and queues the pid in pending_coproc_retirements; the force-apply carve-out for coprocs at unobserved boundaries is removed.
  • flush_pending_coproc_retirements performs the retirement only at disposal points: a refresh that observed a foreground wait (force, or the foreground-wait count moved), the loop REAP point, the jobs listing, stream end.
  • Whole-command emulations (external file builtins, host handler, same-shell script, /bin/echo stand-in) note the foreground wait they stand in for — in GNU each is a forked child whose wait_for completion is a disposal point (the emulated sleep fast path already did this; without the mark a death during an emulated cat never reached a disposal point).
  • reap_dead_jobs_after_loop_body gains the coproc arm of REAP; the AST while/until executor gains the REAP call itself (it never had one — execute_cmd.c:3838), which also fixes dead-job reaping for while loops on that path.

Evidence (this VM, debug builds, per-round fresh processes)

Baseline → fixed, 200-round batteries unless noted:

shape baseline fixed GNU 5.3.20
status-7 wait battery (m5) BOUND→0 in ~5% (forbidden class) BOUND→0 = 0 (BOUND→7 / UNSET→0 only) same invariant; BOUND→0 never observed
100-round in-script invariant battery 18 VIOLATION (BOUND rc=0) 0 violations (×3 runs) 0 violations
issue #2053 plain shape rc=0 23/200 vs recorded-status class mismatch classes always agree classes always agree
#2057 seed (exec {C[1]}>&- right after exit) not reproduced on this VM (0/200; reporter's environment ~1/5) 200/200 clean 50/50 clean (no disposal point before the exec)
loop disposal (pinA/W1 forms) vars survived builtin loops on the AST while path disposed per iteration 96–100% disposed

Shapes probed and deliberately not pinned: a cat-to-EOF command between the death and the exec — its completion is a GNU disposal point, and GNU itself reports the ambiguous redirect there (90/100); the post-disposal unbound close is ambiguous in both shells (verified 30/30 on both).

Gates

  • Local (build slot): coproc pin family (19 targets) + wait/jobs/loop targets all green; new pins 6/6. parser_coproc_tests shows 1 failure that is byte-identical on the pristine baseline (failure-set identity verified by stash rebuild) — pre-existing, untouched.
  • cargo check with RUSTFLAGS=-D warnings clean; per-file rustfmt on changed files; no CR.
  • Full suite adjudication left to CI on this PR.

… recorded status (rubash#2053)

The boundary refresh discovered a coproc death with its own
try_wait and retired the coproc in the same motion — unbinding
<name>/<name>_PID and closing the endpoints — at ANY command
boundary, including the boundary immediately before a `wait`
expanded its operand. The operand vanished mid-flight: the wait
degenerated to a bare wait (status 0) or reported 127 for a pid
whose recorded status was never consulted. Probe evidence on the
baseline (200-round batteries): the status-7 shape produced the
forbidden BOUND->0 class in ~5% of rounds, and a 100-round
in-script battery caught 18 stolen waits.

GNU keeps the two steps in separate hands: waitchld records the
status asynchronously (jobs.c:4173 -> execute_cmd.c:2326
coproc_setstatus, which explicitly defers disposal out of the
handler context), and coproc_reap (execute_cmd.c:2225) disposes
only an already-reaped coproc, from cleanup_dead_jobs (jobs.c:1303)
at specific disposal points — the notify_and_cleanup inside a
foreground wait_for (jobs.c:3385), stop_pipeline after a command
that forked (execute_cmd.c:963), the REAP() after each loop
iteration (execute_cmd.c:2975-2983; :3105/:3267/:3606/:3838), the
wait/jobs builtins, and stream end.

Mirror that split in the executor:

- apply_job_completion now only RECORDS a coproc completion (the
  status lands in completed_statuses like any job) and queues the
  pid in pending_coproc_retirements; the old carve-out that
  force-applied coproc completions at unobserved boundaries is
  gone.
- flush_pending_coproc_retirements performs the retirement
  (coproc_unset_vars + endpoint close) and runs only at disposal
  points: a refresh that observed a foreground wait
  (force_observe, or foreground_wait_count moved), the loop REAP
  point, the jobs listing, and stream end.
- Whole-command emulations (external file builtins, host handler,
  same-shell script, /bin/echo stand-in) now note the foreground
  wait they stand in for — in GNU each is a forked child whose
  wait_for completion is a disposal point (the emulated sleep
  fast path already did this). Without the mark, a death during
  an emulated `cat` never reached a disposal point.
- reap_dead_jobs_after_loop_body gains the coproc arm of GNU's
  REAP (refresh + apply + retire under the same
  non-interactive/non-job-control gate), and the AST while/until
  executor (compound_exec.rs) gains the REAP call itself — it
  never had one (execute_cmd.c:3838), so a coproc dying during a
  while loop kept its variables for the whole loop.

Post-fix batteries (same scripts, 200 rounds): the forbidden
class is gone everywhere — BOUND always yields the recorded
status, UNSET (disposed at a real disposal point) yields the
bare-wait 0, matching the GNU invariant; GNU's own distribution
over the same shapes was verified first (including the shapes
where GNU disposes aggressively: loop iterations 96-100%
disposed, cat-completion ~97% disposed).

Regression pins in tests/issue2053_coproc_wait_race_regressions.rs:
a 100-round in-script class-invariant battery, the
no-disposal-point wait form, and the loop/foreground-wait
disposal guards (#1023 cadence).
…(rubash#2057)

The exec-close face of the same reap/disposal race fixed in the
previous commit (rubash#2053): when the boundary retirement landed
between a coproc child's exit and a following `exec {C[1]}>&-`,
the array was already unbound in a window where GNU has no
disposal point, and the close reported an ambiguous redirect
(~1 in 5 rounds in the reporter's environment). With retirement
deferred to GNU's disposal points, the array is still bound at
the exec whenever no disposal point has passed.

Pins (tests/issue2057_coproc_exec_close_race_regressions.rs),
expectations verified against GNU bash 5.3.20:

- the issue's seed verbatim, 50 rounds in isolated subshells —
  no disposal point exists between the spawn and the exec in
  either shell, so every round must close cleanly;
- the deterministic form: the child dies during a blocking
  builtin (not a disposal point), the array must still be bound
  and the close must succeed (GNU 20/20).

The pin file also records the shape that must NOT be pinned
clean: an intervening external command read to EOF (its
completion is a disposal point) — GNU itself reports the
ambiguous redirect there in 90 of 100 rounds, because after
disposal the unbound-variable close is ambiguous in both shells.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 29, 2026 14:35
@caomengxuan666
caomengxuan666 merged commit e524774 into master Oct 29, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/coproc-races-2053-2057 branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant