Skip to main content

Graceful Shutdown

When your server receives a termination signal (Ctrl+C, kill, Docker stop), you don’t want to drop active requests. PrismGracefulShutdown catches signals, runs cleanup hooks, and rejects new requests while in-flight ones finish.

Quick Setup

Enable Graceful Shutdown

Shutdown Middleware

Reject new requests during shutdown with a 503 Service Unavailable:
Reject During Shutdown
When shutdown begins, new requests get:
  • Status: 503 Service Unavailable
  • Header: Connection: close
  • Header: Retry-After: 5
  • Body: "Server is shutting down"
In-flight requests that were already past the middleware continue normally.

How It Works

  1. Signal received — SIGTERM or SIGINT caught by DispatchSource
  2. Shutdown beginsisShuttingDown flag set, new requests rejected
  3. Hooks run — Each registered onShutdown handler runs in order
  4. Final shutdown — The closure passed to installSignalHandlers runs last

Practical Example

Full Shutdown Setup

Checking Shutdown State

Check State

Manual Shutdown

Trigger shutdown programmatically (e.g., from an admin endpoint):
Admin Shutdown
Place PrismShutdownMiddleware early in your middleware stack — before authentication, logging, etc. This ensures new requests are rejected before any middleware processes them.
In Docker/Kubernetes, the orchestrator sends SIGTERM and waits (default 30 seconds) before SIGKILL. Set your drainTimeout shorter than the orchestrator’s grace period to ensure clean shutdown completes before the process is killed.