From a373670c6175050263b60b1797623151dc339ab1 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 07:13:49 -0600 Subject: [PATCH] =?UTF-8?q?fix(fleet):=20quiet=20exit=20on=20broken=20pipe?= =?UTF-8?q?=20=E2=80=94=20restore=20SIGPIPE=20default=20+=20EPIPE-safe=20s?= =?UTF-8?q?ecrets=20output=20(#5569)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/fleet/Cargo.lock | 1 + pkgs/fleet/crates/fleet/Cargo.toml | 4 ++ pkgs/fleet/crates/fleet/src/main.rs | 21 +++++++ pkgs/fleet/crates/fleet/src/secrets.rs | 85 ++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 6 deletions(-) diff --git a/pkgs/fleet/Cargo.lock b/pkgs/fleet/Cargo.lock index 8b2d8889e1..2690fd4702 100644 --- a/pkgs/fleet/Cargo.lock +++ b/pkgs/fleet/Cargo.lock @@ -250,6 +250,7 @@ dependencies = [ "fleet-registry", "fleet-search", "fleet-secrets", + "libc", "serde_json", ] diff --git a/pkgs/fleet/crates/fleet/Cargo.toml b/pkgs/fleet/crates/fleet/Cargo.toml index e764104e0a..13722c749f 100644 --- a/pkgs/fleet/crates/fleet/Cargo.toml +++ b/pkgs/fleet/crates/fleet/Cargo.toml @@ -26,6 +26,10 @@ fleet-proxy = { workspace = true, default-features = false } # the connect variant must not carry it (§2: "nothing else"). fleet-secrets = { workspace = true, optional = true } serde_json = { workspace = true } +# Direct dep for the SIGPIPE disposition reset in main (#5569); +# already in the workspace lockfile as a transitive dependency. +[target.'cfg(unix)'.dependencies] +libc = "0.2" # Capability features (§2). The shipped variants are feature sets: # fleet = default (full) diff --git a/pkgs/fleet/crates/fleet/src/main.rs b/pkgs/fleet/crates/fleet/src/main.rs index 89733b4170..00409a0119 100644 --- a/pkgs/fleet/crates/fleet/src/main.rs +++ b/pkgs/fleet/crates/fleet/src/main.rs @@ -717,7 +717,28 @@ fn run_root(surface: &Surface, args: &[String], invoked_as: &str) -> i32 { } } +/// Restore the default SIGPIPE disposition (#5569). The Rust +/// runtime ignores SIGPIPE, so a `println!` in any domain (queue, +/// search, secrets, ...) panics when a downstream pipe closes +/// early (`fleet secrets list | head`). With the default +/// disposition the process dies quietly by SIGPIPE, like a +/// well-behaved Unix filter, whichever verb is printing. Domains +/// that handle EPIPE explicitly (secrets) keep their quiet-exit +/// path; this covers the rest. +#[cfg(unix)] +fn restore_default_sigpipe() { + // SAFETY: signal() with SIG_DFL for SIGPIPE is a plain + // disposition reset, done before any other thread or I/O. + unsafe { + libc::signal(libc::SIGPIPE, libc::SIG_DFL); + } +} + +#[cfg(not(unix))] +fn restore_default_sigpipe() {} + fn main() { + restore_default_sigpipe(); let argv: Vec = std::env::args().collect(); let invoked_as = argv .first() diff --git a/pkgs/fleet/crates/fleet/src/secrets.rs b/pkgs/fleet/crates/fleet/src/secrets.rs index 774b424184..76f61b261d 100644 --- a/pkgs/fleet/crates/fleet/src/secrets.rs +++ b/pkgs/fleet/crates/fleet/src/secrets.rs @@ -25,6 +25,40 @@ const EXIT_OK: i32 = 0; const EXIT_USAGE: i32 = 2; const EXIT_UNRESOLVED: i32 = 3; +/// Write one line to a writer, classifying the outcome (#5569). +/// A broken pipe means the downstream consumer closed early +/// (`fleet secrets list | head`): that is a quiet, successful exit +/// for a Unix filter, not an error. Every other write error stays +/// loud. `println!` must not be used for streamed output: it panics +/// on EPIPE. +#[derive(Debug, PartialEq, Eq)] +enum LineWrite { + Written, + BrokenPipe, + Failed(std::io::ErrorKind), +} + +fn write_line(w: &mut impl std::io::Write, line: &str) -> LineWrite { + match writeln!(w, "{line}") { + Ok(()) => LineWrite::Written, + Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => LineWrite::BrokenPipe, + Err(e) => LineWrite::Failed(e.kind()), + } +} + +/// Print `text` (a single value) to stdout; broken pipe is a quiet +/// success (#5569), other write errors are loud. +fn print_value(text: &str) -> i32 { + let mut out = std::io::stdout().lock(); + match write_line(&mut out, text) { + LineWrite::Written | LineWrite::BrokenPipe => EXIT_OK, + LineWrite::Failed(kind) => { + eprintln!("fleet secrets: failed writing to stdout: {kind}"); + EXIT_UNRESOLVED + } + } +} + fn usage() -> i32 { eprintln!("usage: fleet secrets [args...]"); eprintln!(); @@ -87,10 +121,7 @@ fn cmd_get(config: &Config, args: &[String]) -> i32 { } let name = &args[0]; match Resolver::default_chain(config).resolve(name) { - Ok(value) => { - println!("{}", value.expose()); - EXIT_OK - } + Ok(value) => print_value(value.expose()), Err(e) => { eprintln!("fleet secrets: {e}"); EXIT_UNRESOLVED @@ -113,8 +144,17 @@ fn cmd_list(config: &Config, args: &[String]) -> i32 { }; match source.list_names() { Ok(names) => { - for name in names { - println!("{name}"); + let mut out = std::io::stdout().lock(); + for name in &names { + match write_line(&mut out, name) { + LineWrite::Written => {} + // Downstream closed early (`| head`): quiet exit. + LineWrite::BrokenPipe => return EXIT_OK, + LineWrite::Failed(kind) => { + eprintln!("fleet secrets: failed writing to stdout: {kind}"); + return EXIT_UNRESOLVED; + } + } } EXIT_OK } @@ -153,6 +193,39 @@ mod tests { } } + struct FailingWriter(std::io::ErrorKind); + + impl std::io::Write for FailingWriter { + fn write(&mut self, _buf: &[u8]) -> std::io::Result { + Err(std::io::Error::from(self.0)) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + + #[test] + fn write_line_maps_broken_pipe_to_quiet_outcome() { + let mut w = FailingWriter(std::io::ErrorKind::BrokenPipe); + assert_eq!(write_line(&mut w, "name"), LineWrite::BrokenPipe); + } + + #[test] + fn write_line_keeps_other_errors_loud() { + let mut w = FailingWriter(std::io::ErrorKind::StorageFull); + assert_eq!( + write_line(&mut w, "name"), + LineWrite::Failed(std::io::ErrorKind::StorageFull) + ); + } + + #[test] + fn write_line_reports_success() { + let mut w: Vec = Vec::new(); + assert_eq!(write_line(&mut w, "name"), LineWrite::Written); + assert_eq!(String::from_utf8(w).unwrap(), "name\n"); + } + #[test] fn non_help_arguments_ask_for_no_usage() { assert_eq!(leaf_help_usage("get", &args(&["some-name"])), None);