Skip to content

fix(procsub,redir): separate BrokenPipe from EBADF and inherit runtime SIG_IGN across procsub respawn (Fixes #4255) - #4260

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4255-procsub-broken-pipe-write-error
Nov 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4255-procsub-broken-pipe-write-error

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Stop misclassifying a readerless process-substitution pipe's BrokenPipe / Windows ERROR_NO_DATA (232) as Bad file descriptor (EBADF), and propagate runtime-ignored signal dispositions (trap '' PIPE) across process-substitution respawns to match GNU Bash 5.3.20 (Fixes #4255).

GNU C Specification

  • third_party/bash/subst.c:6362 (process_substitute()), :6412 (make_child()), :6426 (restore_original_signals()).
  • third_party/bash/trap.c:1433-1452 (reset_or_restore_signal_handlers()): resets caught traps in the forked child while preserving SIG_IGN entries (visible to trap -p and re-trappable).
  • third_party/bash/builtins/common.c:320-334 (sh_chkwrite()): a write to a genuinely closed stdout (>&-) fails with EBADF (write error: Bad file descriptor); a write to a broken pipe with default SIGPIPE kills the producer silently (status 141), whereas under trap '' PIPE it fails EPIPE (write error: Broken pipe).

Root Cause & Changes

  1. src/executor/redirection.rs:
    • Split is_broken_pipe_output_error (ErrorKind::BrokenPipe / Windows ERROR_NO_DATA (232) / ERROR_BROKEN_PIPE (109)) from is_closed_output_error (ERROR_INVALID_HANDLE (6) on Windows / EBADF on Unix).
    • Previously, is_closed_output_error matched BrokenPipe / 232, causing buffered builtins like printf inside an unread <(printf 'hi\n') on Windows to emit printf: write error: Bad file descriptor.
    • Under default SIGPIPE disposition in a respawned child (respawned_child_broken_pipe_dies()), return Err(ExecuteError::BrokenPipe) so the child exits silently like Unix SIGPIPE (141); under trap '' PIPE, fall through to sh_chkwrite (write error: Broken pipe).
    • Replaced the synthetic BrokenPipe error for a closed coprocess input with bad_file_descriptor_io_error().
  2. src/builtins/trap.rs + src/executor/compound_exec/transport.rs + src/executor/init.rs + src/executor/shell_options.rs + src/executor/ast_exec.rs:
    • Added FORK_IGNORED_SIGNALS_TRANSPORT (__RUBASH_FORK_IGNORED_SIGNALS, stripped in FORK_TRANSPORT_KEYS) carrying non-hard-ignored runtime SIG_IGN entries across respawn and installed via install_fork_inherited_ignore() in init.rs.
    • Added Executor::respawned_child_broken_pipe_dies(&self) so ast_exec.rs and redirection.rs share one SIGPIPE-vs-EPIPE verdict.
  3. tests/issues_4200_4299.rs + tests/issues_4200_4299/issue4255_procsub_broken_pipe_write_error.rs:
    • Added 6 integration pins covering unread <(printf 'hi\n') / <(echo hi) / <(pwd) silence under default SIGPIPE, printf: write error: Broken pipe (and no Bad file descriptor) under trap '' PIPE, trap -p PIPE visibility & re-trapping across <(...), and genuinely closed stdout (>&-) still reporting Bad file descriptor.

What this PR deletes

  • Deletes the broken conflation of ErrorKind::BrokenPipe / Windows ERROR_NO_DATA (232) with Bad file descriptor inside is_closed_output_error (src/executor/redirection.rs).
  • Deletes the synthetic ErrorKind::BrokenPipe error returned for closed coprocess input in OutputFdState (replaced with real EBADF / ERROR_INVALID_HANDLE).

Verification

  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash):
    • Unread <(printf 'hi\n') / <(echo hi) / <(pwd): silent (rc=0).
    • trap '' PIPE + unread <(printf 'hi\n'): printf: write error: Broken pipe (never Bad file descriptor).
    • trap '' PIPE; cat <(trap -p PIPE) -> trap -- '' SIGPIPE; re-trapping inside <(...) succeeds (rc=0, trap -- 'echo inner' SIGPIPE).
    • printf 'hi\n' >&- / echo hi >&- / pwd >&-: write error: Bad file descriptor (rc=1).
  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --lib env_namespace_census_is_complete --locked: passed.
    • cargo test --test issues_4200_4299 --locked: 8/8 passed.
  • Windows:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked (after rebase onto 8407b40ad): clean (0 warnings).
    • cargo test --test issues_4200_4299 --locked: 8/8 passed.
    • cargo test --test issues_0400_0599 issue510 --locked: 5/5 passed.
    • cargo test --test issues_0600_0999 issue699 --locked: 7/7 passed.
    • cargo test --test issues_3900_4199 issue4028 --locked: 5/5 passed.
    • cargo test --lib env_namespace_census_is_complete --locked: passed.
    • python scripts/check-rustfmt.py: ok (226 targets).
    • python scripts/check-marker-literals.py: OK.
    • python scripts/check-api-ledger.py: OK (298 symbols).

Fixes #4255

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — passes the gate, merge after rebase.

Checked against GNU bash 5.3.20 source, line by line: subst.c:6426 (restore_original_signals() in the procsub fork) and trap.c:1433-1453 (IGNORE_SIG stays SIG_IGN, trapped signals reset) both back the model exactly — runtime-ignored signals become ordinary visible empty trap entries in the respawned child (re-trappable, shown by trap -p), while startup hard-ignores stay on the TRAP_ORIG_IGNORES path. The BrokenPipe / EPIPE-report / EBADF three-way split matches GNU's SIGPIPE-death / EPIPE-error / EBADF-error split, including the trap '' PIPE report path and the >&- EBADF path. The new FORK_IGNORED_SIGNALS_TRANSPORT key extends the existing FORK_* family — one mechanism, not a second one.

CI: census 7 FAIL, all on the known rotator/stable list, 0 SIGABRT, goldens only the 2 standing snapshots. New pins 6/6 actually executed (sync-barrier repro so the unread-procsub-pipe case isn't schedule-dependent), and the fail-first direction is sound — the default-silent pin must print printf: write error on the old code, which is exactly the #4255 symptom.

One ask before merging: the base (8407b40a) is behind master now (#4258 and #4261 have landed since). Please rebase onto current master and confirm mergeable + fresh CI; if the census is still the same 7 names, I'll merge it.

Non-blocking suggestion: after merging, I'll re-run the #4255 minimal repro (echo <(echo hi) silent) against the cloud GNU 5.3.20 oracle as closing evidence.

@caomengxuan666
caomengxuan666 force-pushed the fix/4255-procsub-broken-pipe-write-error branch from 2f368a6 to 7148e03 Compare November 28, 2026 05:55
@caomengxuan666

Copy link
Copy Markdown
Member Author

Rebased onto current master (f9c2b9599) as 7148e0376 (resolved the 2-line tests/issues_4200_4299.rs registration conflict with #4257).

  • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings)
  • cargo test --test issues_4200_4299 --locked: 13/13 passed
  • python scripts/check-rustfmt.py: ok (226 targets)

Fresh CI is running now.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain follow-up — the rebase landed clean and the new CI is exactly the known set (census 8 FAIL: issue2024 / issue2340 bare_wait_survivor_is_the_last_job / issue2698 / issue3300 / issue3420 / issue3575 / issue3605 / issue4181, all documented rotators/stable; 0 SIGABRT; goldens only the 2 standing snapshots). I tried to merge it just now, but #4259 landed first in the same minute and this PR now reports merge conflicts.

Please rebase once more onto current master (which now includes #4259) and confirm mergeable + fresh CI. If the census is still the same known names, I'll merge it immediately — nothing else is blocking.

@caomengxuan666
caomengxuan666 force-pushed the fix/4255-procsub-broken-pipe-write-error branch from 7148e03 to 1a64180 Compare November 28, 2026 06:42
@caomengxuan666

Copy link
Copy Markdown
Member Author

Rebased onto master (29384e2fe, after #4259 landed) to resolve the 2-line tests/issues_4200_4299.rs registration conflict. Verified RUSTFLAGS="-D warnings" cargo check --lib --locked (0 warnings), cargo test --test issues_4200_4299 --locked (16/16 passed), and check-rustfmt.py clean.

@caomengxuan666
caomengxuan666 merged commit a4e529f into master Nov 28, 2026
11 of 13 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/4255-procsub-broken-pipe-write-error branch November 28, 2026 08:34
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] spurious write error on unread /dev/fd write end in process substitution

1 participant