Graceful Shutdown
Stop accepting, let in-flight work finish within a window, then close.
An application shuts down in one sequence, whatever triggers it. The example is examples/10-shutdown. The sequence itself is part of the application lifecycle, described in Core Concepts.
Triggers and bounds
serve returns when one of three things happens first: the signal future passed to it resolves, close(signal) is called on the app or on an AppHandle, or a transport's own serve ends while the app is running. Two builder settings bound the sequence:
pub fn builder() -> AppBuilder {
App::builder(AppModule).drain_timeout(DRAIN).shutdown_timeout(SHUTDOWN_CAP)
}drain_timeout is the window in-flight executions (calls being handled, see Scopes) get after the server stops accepting, ten seconds unless set. shutdown_timeout caps the whole sequence, counted from the trigger, and is unset by default. Set it a few seconds below the orchestrator's grace period.
main passes a signal future that resolves on SIGINT or SIGTERM and prints the report serve returns:
let shutdown = app.serve(shutdown_signal()).await?;
println!("closed on {}: {} abandoned", shutdown.signal, shutdown.abandoned);ulo_runtime_tokio::shutdown_signal() is that future for tokio. On Unix a second SIGINT during the sequence does not end it.
What HTTP does
| Step | HTTP |
|---|---|
| before-shutdown hooks | the server serves normally |
| stop accepting | HTTP/2 GOAWAY; idle HTTP/1.1 keep-alive connections closed; busy ones get Connection: close on their next response |
| drain window | requests finish; an SSE handler sees draining() and ends its stream |
| a request arriving once new executions are refused | 503 with Retry-After and Connection: close, answered directly: the pre-dispatch stage does not run for it |
| window ends | executions still running are cancelled with reason Drain, answered 503 "the server is shutting down", and counted as abandoned |
| close | listeners and connections closed |
A handler that watches for the drain
A handler takes ExecutionRef and awaits draining() to end its own work early:
use ulo::ExecutionRef;
#[ulo_http::get("/watch")]
async fn watch(&self, exec: ExecutionRef) -> &'static str {
self.gate.entered.add_permits(1);
exec.draining().await;
"stopped early"
}A stream ends the same way with take_until(exec.draining()); see Streaming and SSE. A request that does not watch is waited for until drain_timeout, then cancelled. /work in the example waits until released. Ctrl-C while it is waiting gives the caller the 503 when the window ends, and the binary prints closed on SIGINT: 1 abandoned.
Ending the app from code
An AppHandle, taken before serve, can close the app: handle.close(Signal::new("deploy")).await. It starts the same sequence and serve returns its report. A close during a running shutdown starts nothing and returns the same outcome. A background loop holding a handle reads handle.draining() to stop pulling work.
A transport whose serve fails or returns while the app is running starts the shutdown as a signal would. The remaining servers drain, and serve returns an Err whose first failure names the transport.