Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/fleet/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkgs/fleet/crates/fleet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions pkgs/fleet/crates/fleet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = std::env::args().collect();
let invoked_as = argv
.first()
Expand Down
85 changes: 79 additions & 6 deletions pkgs/fleet/crates/fleet/src/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> [args...]");
eprintln!();
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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<usize> {
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<u8> = 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);
Expand Down