Skip to content

fix(procsub): run >(body) consumers as detached async children - #1293

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/1220-procsub-async-consumer
Oct 23, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/1220-procsub-async-consumer

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1220

GNU subst.c:6362 process_substitute() forks the write-side >(...) consumer with FORK_ASYNC|FORK_PROCSUB: it reads the substitution pipe concurrently and the parent never blocks on it — the child is reaped only by wait/SIGCHLD (jobs.c procsub_add). Rubash ran the body inline at finish_process_substitutions, so a slow consumer stalled the next command.

execute_output_process_substitution now spawns the body as a detached rubash -c child whose fd 0 is an OS pipe streaming the temp-carrier bytes — real pacing, output races in late like GNU's fork. Reuses the cmd & respawn transport (function prelude, env whitelist, trap cancellation, subshell-depth/BASHPID markers, __RUBASH_CMD_FILE fallback) and registers the pid in background_children + $! so wait $!/bare wait reap it; stays off jobs like GNU's separate procsub list.

Before:

$ time rubash s.sh   # echo hello > >(sleep 1; echo AFTER); echo next
AFTER
next     # ~1.0s

After (== GNU 5.3.20):

next     # ~0.05s

Also fixes the consumer-side symptoms noted in the issue: >(while read...) no longer double-feeds (single real fd-0 channel — the FUNCTION_STDIN duplicate is gone), >(exit 3) is child-local, and body variable assignments no longer leak into the parent.

Test plan

  • cargo test --test issue1220_procsub_write_async — 2 tests green (non-blocking + output still produced)
  • cargo fmt --check clean

Generated with Devin

GNU subst.c:6362 process_substitute() forks the write-side `>(...)`
consumer with FORK_ASYNC|FORK_PROCSUB: it reads the substitution pipe
concurrently and the parent never blocks on it — the child is reaped
only by wait/SIGCHLD (jobs.c procsub_add). Rubash ran the body inline at
finish_process_substitutions, so `echo hi > >(sleep 1; echo AFTER);
echo next` printed AFTER before next and took the full second, and a
`while read` consumer double-fed its input through the staged
FUNCTION_STDIN mirror (rubash#1221).

execute_output_process_substitution now spawns the body as a detached
`rubash -c` child whose fd 0 is an OS pipe streaming the temp-carrier
bytes from a writer thread — real pacing, and the consumer's output
races in late exactly like GNU's forked child. The spawn reuses the
`cmd &` respawn transport (function prelude, env whitelist, trap table
cancellation, subshell-depth/BASHPID markers, __RUBASH_CMD_FILE
fallback) and registers the pid in background_children + $! so
`wait $!` and bare `wait` reap it; procsub children stay off `jobs`
like GNU's separate procsub list.

Verified vs WSL GNU Bash 5.3.20 (script-file probes):
  echo hello > >(sleep 1; echo AFTER); echo next  -> next, ~0.05s
  echo hello > >(while read -r l; do echo C:$l; done) -> C:hello once
  echo hi > >(exit 3); echo rc=$?                 -> rc=0 (child-local)
  echo hi > >(read l; y=got_$l); echo [$y]        -> [] (no leak)

Generated with [Devin](https://devin.ai)
@caomengxuan666
caomengxuan666 force-pushed the fix/1220-procsub-async-consumer branch from 7183072 to 3b005cc Compare October 23, 2026 07:53
@caomengxuan666
caomengxuan666 merged commit 5a52639 into master Oct 23, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1220-procsub-async-consumer 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.

[bug] >(slow-consumer) blocks the next command until the consumer exits (GNU runs it async)

2 participants