From 529fe5e51f8b62dd19023fb8bef16d1d98bac260 Mon Sep 17 00:00:00 2001 From: OneTooMany Date: Mon, 12 Oct 2026 11:26:12 +0200 Subject: [PATCH 1/2] fix(cli): `moq auth serve` stops on SIGTERM and SIGINT A lone `auth` stage returned before `drive()`, so it never installed the shutdown handlers. As PID 1 in the release image the kernel drops an unhandled SIGTERM, and every container stop waited out the grace period before SIGKILL. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + rs/moq-cli/Cargo.toml | 4 +++ rs/moq-cli/src/auth.rs | 2 +- rs/moq-cli/src/main.rs | 9 ++++- rs/moq-cli/tests/auth_serve.rs | 65 ++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 rs/moq-cli/tests/auth_serve.rs 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..7a19406d452 100644 --- a/rs/moq-cli/src/auth.rs +++ b/rs/moq-cli/src/auth.rs @@ -19,7 +19,7 @@ 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 the task is dropped. pub async fn run(self) -> anyhow::Result<()> { match self.command { Command::Serve(serve) => serve.run().await?, diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 6cc7dab1ab6..57f9e9068d3 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -304,7 +304,14 @@ async fn main() -> anyhow::Result<()> { match stages.remove(0) { Command::Auth(auth) => { cli.reject("auth")?; - return auth.run().await; + // `serve` never returns on its own, and a container runs `moq` as PID 1, + // where a signal without a handler is dropped. `biased` installs the + // handlers before `serve` logs that it is listening. + return tokio::select! { + biased; + res = shutdown_signal() => res, + res = auth.run() => res, + }; } Command::Completion(completion) => { cli.reject("completion")?; 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; +} From 98bbba050e184b272804b61b2b2379a0ca4fca73 Mon Sep 17 00:00:00 2001 From: OneTooMany Date: Mon, 12 Oct 2026 12:14:59 +0200 Subject: [PATCH 2/2] fix(cli): race the shutdown signal only in `auth serve` `auth sign` and `verify` block on stdin, so an installed handler swallowed the signal that used to end them. Co-Authored-By: Claude Opus 5.5 --- rs/moq-cli/src/auth.rs | 12 ++++++++++-- rs/moq-cli/src/main.rs | 9 +-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/rs/moq-cli/src/auth.rs b/rs/moq-cli/src/auth.rs index 7a19406d452..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 the task is dropped. + /// 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/src/main.rs b/rs/moq-cli/src/main.rs index 57f9e9068d3..6cc7dab1ab6 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -304,14 +304,7 @@ async fn main() -> anyhow::Result<()> { match stages.remove(0) { Command::Auth(auth) => { cli.reject("auth")?; - // `serve` never returns on its own, and a container runs `moq` as PID 1, - // where a signal without a handler is dropped. `biased` installs the - // handlers before `serve` logs that it is listening. - return tokio::select! { - biased; - res = shutdown_signal() => res, - res = auth.run() => res, - }; + return auth.run().await; } Command::Completion(completion) => { cli.reject("completion")?;