From 45307cd8f540e496b9182d3f94d11b2e4785f95c Mon Sep 17 00:00:00 2001 From: brasse Date: Sat, 4 Jul 2026 16:33:55 +0200 Subject: [PATCH] Don't panic on broken pipe (e.g. `npmctl list-hosts | head`) Reset SIGPIPE to SIG_DFL at startup so writing to a closed stdout terminates the process quietly like normal Unix tools, instead of panicking with "failed printing to stdout: Broken pipe". Co-Authored-By: Claude Opus 4.8 (1M context) Claude-Session: https://claude.ai/code/session_011YPrEZVBpPVoCRyJz2exkZ --- Cargo.lock | 1 + Cargo.toml | 3 +++ src/main.rs | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index dc6685e..3662d43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -629,6 +629,7 @@ dependencies = [ "chrono", "clap", "dirs", + "libc", "reqwest", "rpassword", "serde", diff --git a/Cargo.toml b/Cargo.toml index 3799f09..da1a005 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ dirs = "5" anyhow = "1" chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } +[target.'cfg(unix)'.dependencies] +libc = "0.2" + [profile.release] strip = true opt-level = "z" diff --git a/src/main.rs b/src/main.rs index 4ebbece..2289ad8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,14 @@ use client::NpmClient; use config::{resolve_base_url, Paths}; fn main() { + // Bete oss som vanliga Unix-verktyg när stdout stängs tidigt (t.ex. `| head`): + // återställ SIGPIPE till default så processen avslutas tyst i stället för att + // panika på "Broken pipe" i println!. + #[cfg(unix)] + unsafe { + libc::signal(libc::SIGPIPE, libc::SIG_DFL); + } + // Egen parse-hantering: vid felaktig input skriver vi ut hjälpen // (utöver felmeddelandet), enligt önskemål. --help/--version skrivs ut normalt. let cli = match Cli::try_parse() {