From ced6969be678354095739ef09395543d71224809 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 20:53:36 -0800 Subject: [PATCH 1/2] fix(cli): install the shutdown signal handlers before serving `drive` spawned `shutdown_signal()` as a task, and tokio installs a signal's handler on that future's first poll. A listener could bind and log before the task ran, so a SIGTERM sent right after took the default action and killed the process. `stdin_shutdown` hit this under load ("CLI exited with signal: 15 (SIGTERM)"). `shutdown_signal` now installs both handlers when called, and `run_stages` calls it before anything binds. `main` fixed the same race in #5575 with a larger rework; this is the release-line minimum. Co-Authored-By: Claude Opus 5.5 --- rs/moq-cli/src/main.rs | 51 ++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 8dca51a3439..4c6f450d840 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -508,6 +508,9 @@ async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<() async fn run_stages(moq: MoqSide, stages: Vec, net: Net) -> anyhow::Result<()> { let cluster = moq.cluster()?; let mut tasks: JoinSet> = JoinSet::new(); + // Before anything binds: a signal sent once a listener is up would otherwise + // take the default action and kill the process before it shuts down. + tasks.spawn(shutdown_signal()?); // The stdin/capture pipelines run on this thread instead of the JoinSet: the // platform capture stream is not Send, so their futures cannot be spawned. let mut locals: Vec = Vec::new(); @@ -800,8 +803,6 @@ async fn run_stdout(consumer: moq_net::origin::Consumer, name: String, args: Sub /// Run every endpoint until the first finishes (stdin EOF, SIGINT, SIGTERM, or an /// error), then drop the rest. async fn drive(mut tasks: JoinSet>) -> anyhow::Result<()> { - tasks.spawn(shutdown_signal()); - while let Some(res) = tasks.join_next().await { match res { Ok(Ok(())) => return Ok(()), @@ -814,21 +815,30 @@ async fn drive(mut tasks: JoinSet>) -> anyhow::Result<()> { Ok(()) } -/// Resolve on SIGINT or, on unix, SIGTERM (what process supervisors send on stop). -async fn shutdown_signal() -> anyhow::Result<()> { +/// A future that resolves on SIGINT or, on unix, SIGTERM (what process supervisors +/// send on stop). The handlers are installed here rather than on its first poll, so a +/// signal that arrives before the future runs is still caught. +fn shutdown_signal() -> anyhow::Result> + Send + 'static> { #[cfg(unix)] { - let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) - .context("failed to listen for SIGTERM")?; - tokio::select! { - res = tokio::signal::ctrl_c() => res.context("failed to listen for SIGINT")?, - _ = term.recv() => {} - } - Ok(()) + use tokio::signal::unix::{SignalKind, signal}; + let mut term = signal(SignalKind::terminate()).context("failed to listen for SIGTERM")?; + let mut int = signal(SignalKind::interrupt()).context("failed to listen for SIGINT")?; + Ok(async move { + tokio::select! { + _ = int.recv() => {} + _ = term.recv() => {} + } + Ok(()) + }) } #[cfg(not(unix))] { - tokio::signal::ctrl_c().await.context("failed to listen for SIGINT") + let mut ctrl_c = tokio::signal::windows::ctrl_c().context("failed to listen for SIGINT")?; + Ok(async move { + ctrl_c.recv().await; + Ok(()) + }) } } @@ -869,6 +879,23 @@ mod tests { type Pipeline = Pin>>>; + /// The handlers are installed when the future is made, not when it first runs: a + /// SIGTERM in between is caught instead of killing the process. + #[cfg(unix)] + #[tokio::test] + async fn a_signal_before_the_first_poll_is_caught() { + let shutdown = shutdown_signal().unwrap(); + let signaled = std::process::Command::new("kill") + .args(["-TERM", &std::process::id().to_string()]) + .status() + .unwrap(); + assert!(signaled.success()); + tokio::time::timeout(std::time::Duration::from_secs(10), shutdown) + .await + .expect("the SIGTERM was never seen") + .unwrap(); + } + /// A local pipeline that dies takes the process with it, even while another one is /// still running. Reporting completion from inside the task instead would miss /// this: a panic skips the report, leaving the survivor to keep the process alive From ffbf34d215b97196d945bcbfbc8ed132a8f261b4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 21:01:36 -0800 Subject: [PATCH 2/2] docs(cli): the signal handlers precede the stage listeners, not every bind Co-Authored-By: Claude Opus 5.5 --- rs/moq-cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 4c6f450d840..7f930a62c41 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -508,7 +508,7 @@ async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<() async fn run_stages(moq: MoqSide, stages: Vec, net: Net) -> anyhow::Result<()> { let cluster = moq.cluster()?; let mut tasks: JoinSet> = JoinSet::new(); - // Before anything binds: a signal sent once a listener is up would otherwise + // Before the stage listeners bind: a signal sent once one is up would otherwise // take the default action and kill the process before it shuts down. tasks.spawn(shutdown_signal()?); // The stdin/capture pipelines run on this thread instead of the JoinSet: the