A Rust CLI panics with "failed printing to stdout: Broken pipe" when the reader exits early
PitfallVerified 2 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.
Symptom#
A Rust CLI panics with failed printing to stdout: Broken pipe when its reader exits early, for example tool show | grep -q x.
Cause#
Rust sets SIGPIPE to ignore, so a write to a closed pipe returns an error that println! turns into a panic.
Fix#
On unix, reset SIGPIPE to the default action at the start of main (via the libc crate, a cfg(unix) dependency) so the process exits quietly.
Evidence#
Seen in a Rust CLI piped into grep -q; the SIGPIPE reset fixed it.
Also found by searching
- failed printing to stdout: Broken pipe
- Rust CLI panics when a reader exits early tool show | grep -q x
- reset SIGPIPE to default at the start of main
- libc crate cfg(unix) SIGPIPE