Skip to content

Commit beabe72

Browse files
fixup! Handle SIGPIPE
1 parent 6707ece commit beabe72

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

Cargo.lock

+1-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mullvad-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ thiserror = { workspace = true }
2222
futures = { workspace = true }
2323
itertools = "0.10"
2424
natord = "1.0.9"
25+
nix = { version = "0.29.0", features = ["signal"] }
2526

2627
mullvad-types = { path = "../mullvad-types", features = ["clap"] }
2728
mullvad-version = { path = "../mullvad-version" }
2829
talpid-types = { path = "../talpid-types" }
2930

3031
mullvad-management-interface = { path = "../mullvad-management-interface" }
3132
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "fs"] }
32-
sigpipe = "0.1"
3333
serde = { workspace = true }
3434
serde_json = { workspace = true }
3535

mullvad-cli/src/main.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ enum Cli {
154154
async fn main() -> Result<()> {
155155
// Handle SIGPIPE
156156
// https://stackoverflow.com/questions/65755853/simple-word-count-rust-program-outputs-valid-stdout-but-panicks-when-piped-to-he/65760807
157-
// https://github.com/rust-lang/rust/issues/119980
158157
// https://github.com/typst/typst/pull/5444
159-
sigpipe::reset();
158+
#[cfg(unix)]
159+
handle_sigpipe().unwrap();
160160

161161
match Cli::parse() {
162162
Cli::Account(cmd) => cmd.handle().await,
@@ -195,3 +195,14 @@ async fn main() -> Result<()> {
195195
}
196196
}
197197
}
198+
199+
/// Install the default signal handler for `SIGPIPE`.
200+
/// Rust is notoriously bad in this regard: https://github.com/rust-lang/rust/issues/119980
201+
#[cfg(unix)]
202+
fn handle_sigpipe() -> Result<(), nix::errno::Errno> {
203+
use nix::sys::signal::{signal, SigHandler, Signal};
204+
// SAFETY: We do not use the previous signal handler, which could cause UB if done carelessly:
205+
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/signal.html
206+
unsafe { signal(Signal::SIGPIPE, SigHandler::SigDfl) }?;
207+
Ok(())
208+
}

0 commit comments

Comments
 (0)