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 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 rs/moq-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions rs/moq-cli/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
65 changes: 65 additions & 0 deletions rs/moq-cli/tests/auth_serve.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Loading