a2a_protocol_server/serve/signal.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! The signal a platform sends to stop a process.
5
6use std::future::Future;
7
8/// Resolves when the platform asks this process to stop: `SIGTERM` (what a
9/// container runtime, `systemd` and `kubectl delete pod` send) or `SIGINT`
10/// (Ctrl-C) on Unix, Ctrl-C elsewhere.
11///
12/// Pass it to [`Server::serve_with_shutdown`](super::Server::serve_with_shutdown)
13/// so the server stops accepting, lets in-flight tasks end, and *returns*;
14/// whatever follows it in `main` then runs, and a
15/// `Telemetry` guard (feature `otlp`) still alive there is dropped,
16/// which flushes. Waiting on `tokio::signal::ctrl_c()` alone is the common
17/// mistake: a pod is stopped with `SIGTERM`, which nothing then handles, so the
18/// process is ended with no drain and no flush. Measured 2026-10-26 on
19/// `hello-agent`: after `SIGTERM` the collector received no spans and no
20/// metrics.
21///
22/// The handlers are registered when this is *called*, not when the future is
23/// first polled, so a signal that arrives between the two is not lost to the
24/// default action. It must be called inside a Tokio runtime. If the handlers
25/// cannot be registered it warns and falls back to Ctrl-C alone, rather than
26/// pretending the drain will happen.
27///
28/// ```rust,no_run
29/// # use std::sync::Arc;
30/// # use a2a_protocol_server::{serve::{Server, shutdown_signal}, dispatch::JsonRpcDispatcher};
31/// # async fn run(dispatcher: JsonRpcDispatcher) -> std::io::Result<()> {
32/// let server = Server::bind("0.0.0.0:3000").await?;
33/// server.serve_with_shutdown(dispatcher, shutdown_signal()).await;
34/// // Here the signal has arrived and the work has ended: flush, then exit.
35/// # Ok(())
36/// # }
37/// ```
38// On Unix the handlers are registered here, before the future is returned, so
39// this cannot be an `async fn`. Elsewhere there is nothing to register, and
40// clippy would have it be one.
41#[cfg_attr(not(unix), allow(clippy::manual_async_fn))]
42pub fn shutdown_signal() -> impl Future<Output = ()> + Send + 'static {
43 #[cfg(unix)]
44 let registered = {
45 use tokio::signal::unix::{SignalKind, signal};
46 match (
47 signal(SignalKind::terminate()),
48 signal(SignalKind::interrupt()),
49 ) {
50 (Ok(terminate), Ok(interrupt)) => Some((terminate, interrupt)),
51 (terminate, interrupt) => {
52 let _why = terminate.err().or_else(|| interrupt.err());
53 trace_warn!(
54 error = ?_why,
55 "cannot listen for SIGTERM and SIGINT; only Ctrl-C will stop the server \
56 gracefully"
57 );
58 None
59 }
60 }
61 };
62 async move {
63 #[cfg(unix)]
64 if let Some((mut terminate, mut interrupt)) = registered {
65 tokio::select! {
66 _ = terminate.recv() => { trace_info!("SIGTERM received; shutting down"); }
67 _ = interrupt.recv() => { trace_info!("SIGINT received; shutting down"); }
68 }
69 return;
70 }
71 let _ = tokio::signal::ctrl_c().await;
72 trace_info!("Ctrl-C received; shutting down");
73 }
74}