Skip to content

fix(coproc): dead coprocs unbind NAME/NAME_PID at the command boundary - #1198

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/1023-coproc-unbind
Oct 23, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/1023-coproc-unbind

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1023

Summary

GNU bash tears a dead coproc down at the first reap point after its exit: cleanup_dead_jobs -> coproc_reap (jobs.c:1342) disposes every COPROC_DEAD entry via coproc_dispose -> coproc_unsetvars (execute_cmd.c:2166, :2450), unbinding <name> and <name>_PID. rubash's per-command refresh skipped exactly the coprocs the next command's redirects referenced (protected_coprocs), so the issue's two simultaneous named coprocs kept both var sets alive.

The carve-out is removed: a finished coproc is disposed at the command boundary regardless of what the next command references — matching GNU, which consults nothing about upcoming words. Live coprocs are unaffected (only observed-dead children are retired), so coproc.tests-shaped read/write roundtrips still work.

Before / after (vs WSL GNU bash 5.3.20)

coproc A1 { echo one; }
coproc A2 { echo two; }
sleep 1            # let both children exit (the issue's race made deterministic)
cat <&${A1[0]}
cat <&${A2[0]}
echo "done rc=$?"

GNU:

line 4: ${A1[0]}: ambiguous redirect
line 5: ${A2[0]}: ambiguous redirect
done rc=1

rubash before: one, two, done rc=0 — both var sets kept.
rubash after: byte-identical to GNU.

Without the sleep the outcome is timing-dependent in GNU itself (the issue notes "GNU's victim varies run to run"); the mechanism — not the race winner — is what now matches.

Test plan

  • tests/issue1023_coproc_unbind.rs: two dead coprocs → both ambiguous; dead coproc referenced by next redirect → unbound; live coproc → vars bound + echo roundtrip works
  • cargo test --test regression — 78/78 green
  • cargo fmt --check clean

Generated with Devin

…y (rubash#1023)

The per-command background-refresh spared finished coprocs whose name
the upcoming command's redirects referenced (coprocs_referenced_by_command /
protected_coprocs), so two simultaneous named coprocs kept both arrays
forever. GNU has no such protection: cleanup_dead_jobs -> coproc_reap
(jobs.c:1342, reached from notify_and_cleanup at each input line and
from stop_pipeline after every spawned pipeline) disposes each
COPROC_DEAD entry via coproc_dispose -> coproc_unsetvars
(execute_cmd.c:2166, :2450), unbinding <name> and <name>_PID, so
`cat <&${A[0]}` on a reaped coproc reports `ambiguous redirect`.

GNU anchors:
- jobs.c:1342 cleanup_dead_jobs -> coproc_reap -> cpl_reap
- execute_cmd.c:2166 coproc_dispose -> :2450 coproc_unsetvars
- execute_cmd.c:2579 execute_coproc -> stop_pipeline -> cleanup_dead_jobs

Verified against WSL GNU bash 5.3.20: two exited coprocs + sleep +
`cat <&${A[0]}` on each prints `ambiguous redirect` for both, rc=1 —
byte-identical after this change. Live coprocs (coproc.tests shape:
read/write while the child runs) are untouched — only COPROC_DEAD
entries are ever disposed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit 0ca16d0 into master Oct 23, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1023-coproc-unbind branch October 23, 2026 08:54
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.

[compat] Two simultaneous named coprocs: GNU tears one down, niu keeps both

1 participant