Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic when piping into a bad process? #21

Open
PlexSheep opened this issue Nov 11, 2024 · 6 comments
Open

panic when piping into a bad process? #21

PlexSheep opened this issue Nov 11, 2024 · 6 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@PlexSheep
Copy link
Owner

$ netpulse -d | return 1                 
thread 'main' panicked at std/src/io/stdio.rs:1117:9:
failed printing to stdout: Broken pipe (os error 32)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@PlexSheep PlexSheep added the bug Something isn't working label Nov 11, 2024
@PlexSheep
Copy link
Owner Author

I've noticed this is a problem with more of my programs (like hedu). It is not the case in ripgrep for example. The panic is in writing of the std lib (since writing fails in that case of course, even something like print!

@PlexSheep
Copy link
Owner Author

This seems to be due to a design difference between rust and c programs. When a rust program receives a SIGPIPE, it does nothing, a c program would get terminated however.

more to read

@PlexSheep
Copy link
Owner Author

Based on this: I seem to have two ways: register a signal handler for SIGPIPE, or just ignore this. Seeing that I already have a signal_hook function that I use in the daemon, I might just do the same for all things.

@PlexSheep
Copy link
Owner Author

Tried this approach, but it does not work.

fn signal_hook() {
    unsafe {
        signal::signal(Signal::SIGTERM, SigHandler::Handler(handle_signal))
            .expect("failed to set up signal handler");
        signal::signal(Signal::SIGPIPE, SigHandler::Handler(handle_signal))
            .expect("failed to set up signal handler");
        signal::signal(Signal::SIGINT, SigHandler::Handler(handle_signal))
            .expect("failed to set up signal handler");
    }
}

extern "C" fn handle_signal(signal: i32) {
    let _signal: nix::sys::signal::Signal =
        nix::sys::signal::Signal::try_from(signal).expect("got an undefined SIGNAL");
    {
        // the default behavior is terminating
        std::process::exit(1);
    }
}

The logic would work out, but you can't exit the program in a signal handler or panic there either without causing deeper errors. the handler is a function that cannot be unwinded, so panicing does not work (not that it would actually do anything), and just using exit() results in another panic somewhere in the std lib, so both unsuitable.

@PlexSheep
Copy link
Owner Author

I don't really know what to do with this to make the unfriendly error go away.

more conversation on the topic

@PlexSheep
Copy link
Owner Author

This seems to be the official issue for it in rust. I'm definitely not rewriting every print or checking some global variable from the signal handler for this.

@PlexSheep PlexSheep added the help wanted Extra attention is needed label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant