diff --git a/Cargo.lock b/Cargo.lock index 17ab2bfbb43..9b836429c59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4378,6 +4378,7 @@ dependencies = [ "bytes", "hang", "humantime", + "libc", "moq-archive", "moq-audio", "moq-auth", diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index cb25037a012..38f5ee99fbd 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -151,6 +151,10 @@ tokio = { workspace = true, features = ["test-util"] } # Plays the `announced` view's frames on a virtual terminal, scrolling included. vt100 = "0.16.2" +[target.'cfg(unix)'.dev-dependencies] +# Sends the signals the `auth serve` shutdown test checks. +libc = { workspace = true } + [[bench]] name = "play_buffer" harness = false diff --git a/rs/moq-cli/src/auth.rs b/rs/moq-cli/src/auth.rs index 477130dc1d1..d430aa47711 100644 --- a/rs/moq-cli/src/auth.rs +++ b/rs/moq-cli/src/auth.rs @@ -19,10 +19,18 @@ pub struct Args { impl Args { /// Run the requested command, writing the key, token, or payload to the chosen - /// destination (stdout by default). `serve` runs until killed. + /// destination (stdout by default). `serve` runs until SIGINT or SIGTERM. pub async fn run(self) -> anyhow::Result<()> { match self.command { - Command::Serve(serve) => serve.run().await?, + // A container runs `moq` as PID 1, where a signal without a handler is + // dropped. Only `serve` races one: `sign` and `verify` block on stdin, so a + // handler would swallow the signal that used to end them. `biased` installs + // the handlers before `serve` logs that it is listening. + Command::Serve(serve) => tokio::select! { + biased; + res = crate::shutdown_signal() => res?, + res = serve.run() => res?, + }, Command::Generate { algorithm, id, diff --git a/rs/moq-cli/tests/auth_serve.rs b/rs/moq-cli/tests/auth_serve.rs new file mode 100644 index 00000000000..fc160ce52a7 --- /dev/null +++ b/rs/moq-cli/tests/auth_serve.rs @@ -0,0 +1,65 @@ +//! `moq auth serve` stops on SIGTERM or SIGINT and exits cleanly. +//! +//! The release image runs `moq` as PID 1, where the kernel drops a signal that has +//! no handler, so a server that relies on the default action ignores `podman stop` +//! and pod termination until SIGKILL. Outside PID 1 the same gap shows as the +//! process dying by the signal instead of exiting 0. +#![cfg(unix)] + +use std::process::Stdio; +use std::time::Duration; + +use tokio::io::{AsyncBufReadExt, BufReader}; + +const TIMEOUT: Duration = Duration::from_secs(10); + +async fn stops_on(signal: libc::c_int) { + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_moq")); + // A `MOQ_*` variable in the developer's shell must not reconfigure the child. + for (name, _) in std::env::vars_os() { + if name.to_string_lossy().starts_with("MOQ_") { + command.env_remove(name); + } + } + let mut child = command + .args(["auth", "serve", "--listen", "127.0.0.1:0"]) + .env("RUST_LOG", "info") + .stderr(Stdio::piped()) + .kill_on_drop(true) + .spawn() + .expect("spawn moq"); + + // Signal only once the handler can be installed: before that, the default action + // would kill the process whether or not the fix is in. + let mut lines = BufReader::new(child.stderr.take().expect("stderr")).lines(); + tokio::time::timeout(TIMEOUT, async { + while let Some(line) = lines.next_line().await.expect("read stderr") { + if line.contains("auth server listening") { + return; + } + } + panic!("moq exited before listening"); + }) + .await + .expect("auth server never listened"); + + let pid = child.id().expect("moq still running") as libc::pid_t; + // SAFETY: `kill` has no memory-safety preconditions; the pid is our live child. + assert_eq!(unsafe { libc::kill(pid, signal) }, 0, "failed to signal moq"); + + let status = tokio::time::timeout(TIMEOUT, child.wait()) + .await + .expect("moq ignored the signal") + .expect("wait for moq"); + assert!(status.success(), "moq did not exit cleanly: {status}"); +} + +#[tokio::test] +async fn auth_serve_stops_on_sigterm() { + stops_on(libc::SIGTERM).await; +} + +#[tokio::test] +async fn auth_serve_stops_on_sigint() { + stops_on(libc::SIGINT).await; +}