Repository navigation
WIP: coproc reap/disposal split — retire only at GNU disposal points - #2122
Merged
Merged
Conversation
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WIP: coproc reap/disposal split — the retirement half of a coproc completion now runs only at GNU's disposal points, so a
wait $C_PIDcan no longer lose its operand to a boundary retirement (andexec {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:
waitchldrecords the status asynchronously (jobs.c:4173 → execute_cmd.c:2326coproc_setstatus, which explicitly defers disposal out of the handler context), andcoproc_reap(execute_cmd.c:2225) disposes only an already-reaped coproc, fromcleanup_dead_jobs(jobs.c:1303) at specific disposal points:notify_and_cleanupinside a foregroundwait_for(jobs.c:3385)stop_pipelineafter a command that forked (execute_cmd.c:963)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 awaitexpanded its operand (operand stolen → bare wait 0 / spurious 127) or before anexec {C[1]}>&-(array gone → ambiguous redirect).Changes
apply_job_completiononly records a coproc completion now and queues the pid inpending_coproc_retirements; the force-apply carve-out for coprocs at unobserved boundaries is removed.flush_pending_coproc_retirementsperforms the retirement only at disposal points: a refresh that observed a foreground wait (force, or the foreground-wait count moved), the loop REAP point, thejobslisting, stream end.wait_forcompletion is a disposal point (the emulatedsleepfast path already did this; without the mark a death during an emulatedcatnever reached a disposal point).reap_dead_jobs_after_loop_bodygains 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:
exec {C[1]}>&-right after exit)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
parser_coproc_testsshows 1 failure that is byte-identical on the pristine baseline (failure-set identity verified by stash rebuild) — pre-existing, untouched.cargo checkwithRUSTFLAGS=-D warningsclean; per-file rustfmt on changed files; no CR.