pub fn shutdown_signal() -> impl Future<Output = ()> + Send + 'staticExpand description
Resolves when the platform asks this process to stop: SIGTERM (what a
container runtime, systemd and kubectl delete pod send) or SIGINT
(Ctrl-C) on Unix, Ctrl-C elsewhere.
Pass it to Server::serve_with_shutdown
so the server stops accepting, lets in-flight tasks end, and returns;
whatever follows it in main then runs, and a
Telemetry guard (feature otlp) still alive there is dropped,
which flushes. Waiting on tokio::signal::ctrl_c() alone is the common
mistake: a pod is stopped with SIGTERM, which nothing then handles, so the
process is ended with no drain and no flush. Measured 2026-10-26 on
hello-agent: after SIGTERM the collector received no spans and no
metrics.
The handlers are registered when this is called, not when the future is first polled, so a signal that arrives between the two is not lost to the default action. It must be called inside a Tokio runtime. If the handlers cannot be registered it warns and falls back to Ctrl-C alone, rather than pretending the drain will happen.
let server = Server::bind("0.0.0.0:3000").await?;
server.serve_with_shutdown(dispatcher, shutdown_signal()).await;
// Here the signal has arrived and the work has ended: flush, then exit.