mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #2683 from jfinkels/uucore-panic-capture-broken-pipe
uucore(panic): guard against "Broken pipe" panics
This commit is contained in:
commit
3e458f524c
1 changed files with 33 additions and 8 deletions
|
@ -1,17 +1,42 @@
|
||||||
|
//! Custom panic hooks that allow silencing certain types of errors.
|
||||||
|
//!
|
||||||
|
//! Use the [`mute_sigpipe_panic`] function to silence panics caused by
|
||||||
|
//! broken pipe errors. This can happen when a process is still
|
||||||
|
//! producing data when the consuming process terminates and closes the
|
||||||
|
//! pipe. For example,
|
||||||
|
//!
|
||||||
|
//! ```sh
|
||||||
|
//! $ seq inf | head -n 1
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
use std::panic;
|
use std::panic;
|
||||||
|
use std::panic::PanicInfo;
|
||||||
|
|
||||||
//## SIGPIPE handling background/discussions ...
|
/// Decide whether a panic was caused by a broken pipe (SIGPIPE) error.
|
||||||
//* `uutils` ~ <https://github.com/uutils/coreutils/issues/374> , <https://github.com/uutils/coreutils/pull/1106>
|
fn is_broken_pipe(info: &PanicInfo) -> bool {
|
||||||
//* rust and `rg` ~ <https://github.com/rust-lang/rust/issues/62569> , <https://github.com/BurntSushi/ripgrep/issues/200> , <https://github.com/crev-dev/cargo-crev/issues/287>
|
if let Some(res) = info.payload().downcast_ref::<String>() {
|
||||||
|
if res.contains("BrokenPipe") || res.contains("Broken pipe") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Terminate without error on panics that occur due to broken pipe errors.
|
||||||
|
///
|
||||||
|
/// For background discussions on `SIGPIPE` handling, see
|
||||||
|
///
|
||||||
|
/// * https://github.com/uutils/coreutils/issues/374
|
||||||
|
/// * https://github.com/uutils/coreutils/pull/1106
|
||||||
|
/// * https://github.com/rust-lang/rust/issues/62569
|
||||||
|
/// * https://github.com/BurntSushi/ripgrep/issues/200
|
||||||
|
/// * https://github.com/crev-dev/cargo-crev/issues/287
|
||||||
|
///
|
||||||
pub fn mute_sigpipe_panic() {
|
pub fn mute_sigpipe_panic() {
|
||||||
let hook = panic::take_hook();
|
let hook = panic::take_hook();
|
||||||
panic::set_hook(Box::new(move |info| {
|
panic::set_hook(Box::new(move |info| {
|
||||||
if let Some(res) = info.payload().downcast_ref::<String>() {
|
if !is_broken_pipe(info) {
|
||||||
if res.contains("BrokenPipe") {
|
hook(info)
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
hook(info)
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue