Graceful shutdown
Last updated: 2026-11-22T21:47:47-03:00 (UTC-3)
Configure the same shutdown behavior in the binary, Docker, Compose, NPX or Kubernetes. Application durations belong in the behavior YAML; the orchestrator’s termination deadline remains a deployment setting.
lifecycle: drain_delay: 10s shutdown_timeout: 130s| Parameter | Default | Allowed range | Existing override |
|---|---|---|---|
lifecycle.drain_delay |
2s; 10s in a Kubernetes Pod |
0s–30s inclusive |
GATEWAY_DRAIN_DELAY |
lifecycle.shutdown_timeout |
130s |
1s–5m inclusive |
GATEWAY_SHUTDOWN_TIMEOUT |
Use YAML string scalars with Go duration syntax, including compound values such as 2m10s. Plain 0s and quoted "0" are valid zero strings; bare numeric scalars (0, 0.0, 0e0, 0x0) are rejected consistently. Null/omitted values use the default. An explicit 0s disables only the drain delay. A nonempty environment override wins over nonempty YAML, then the default applies; an empty override falls back. Explicit values retain their source even when equal to a default. Invalid YAML is rejected even when an override wins or the settings view is disabled. gateway validate now checks both these YAML values and environment overrides before database/exporter startup; a failure at that stage is not an exported runtime event.
The drain delay defaults to 10 seconds in a Kubernetes Pod, detected by the KUBERNETES_SERVICE_HOST variable the kubelet sets in every container, and to 2 seconds everywhere else. A Pod’s endpoint removal reaches each node’s kube-proxy on its own schedule: on a three-zone GKE cluster, new connections kept reaching a terminating Pod for up to about 3.5 seconds after SIGTERM (measured). Outside Kubernetes nothing has to propagate, and a longer delay would only slow a stop. An explicit value, in YAML or the environment, wins in both places; the shipped profiles leave it out. The detection also matches the binary or the NPX package started inside a Kubernetes-hosted development environment (a cloud shell, a CI runner Pod), where a stop then takes up to 10 seconds; set drain_delay there if that matters.
What happens on termination
Section titled “What happens on termination”On SIGTERM/SIGINT, readiness becomes 503 and liveness remains 200, while every request is still served normally for the drain delay: load balancers stop sending new traffic on their own schedule, and what still arrives in that window is answered, not refused. HTTP keep-alive turns off at the same moment: idle client connections close at once, and every response from then on carries Connection: close, so a client’s next request opens a new connection that a load balancer sends to a ready replica. When the delay ends, new operations receive a correlated refusal (503, code draining), the listener closes and accepted requests may finish within the shutdown timeout. Background work (durable queues, batches, evaluation runs, live evaluation) does not wait for the delay: it stops claiming new items at SIGTERM, since it does not arrive through a load balancer. A timeout closes remaining HTTP connections; an unfinished stream is not replayed or given a fabricated terminal event.
Realtime WebSockets and background work retain their existing ownership and share the absolute drain-plus-shutdown deadline. HTTP shutdown alone does not wait for hijacked WebSockets. Their explicit wait remains before database teardown, so terminal usage proof and durable receipt settlement keep their existing rules. Worker, cache-write, log-destination, tracer, database and log-exporter cleanup preserve their current order. Neither duration is a whole-process deadline or a promise to survive SIGKILL. A forced process exit can leave a receipt pending until its lease expires; existing durable recovery then marks the unproven outcome indeterminate, without replay or automatic release of an unknown charge.
Stop it from a terminal or a process manager
Section titled “Stop it from a terminal or a process manager”Send one stop signal and wait. Ctrl+C (SIGINT) and SIGTERM both start the drain above, and the gateway exits 0 when it is done, about the drain delay later on an idle gateway (2 seconds outside Kubernetes). The drain’s first log line, gateway draining, names the signal.
- The same stop delivered twice drains once. A signal that arrives within 1 second of the first is ignored and logged at INFO as
gateway stop signal repeated, ignored. This is what happens when a process manager signals every process of a group or cgroup and a wrapper around the gateway (the NPX launcher,npm exec) forwards its own copy as well. The window is fixed, not a setting. - A second Ctrl+C or SIGTERM more than 1 second after the first stops the gateway at once, without waiting for the requests still running: their connections are cut, the gateway logs
gateway forced exitat WARN and exits with 130 after SIGINT or 143 after SIGTERM. Use it only when you cannot wait. - Closing the terminal (SIGHUP) stops the native binary at once, without the drain: the gateway itself drains only on SIGINT and SIGTERM. Started through NPX, the launcher turns the hangup into SIGTERM, so the gateway drains and then exits. To keep the gateway running after you close the terminal, start the native binary with
nohup, or run either one under a process manager. - systemd: the default
KillMode=control-groupdrains. It sends SIGTERM to every process of the unit; under NPX the gateway gets it directly and again from the launcher, within the 1-second window, and drains once.KillMode=mixed(SIGTERM to the main process only) drains the same way, so you do not need to change the default. SetTimeoutStopSec=to at least the drain delay plus the shutdown timeout plus the cleanup allowance (180 seconds covers the defaults); systemd’s own default of 90 seconds is shorter than the shutdown timeout, and when it expires systemd sends SIGKILL. - A SIGKILL of the NPX launcher alone or of its process group (
timeout -k,kill -9 -<pgid>, a test runner stopping its web server) also drains the gateway: the launcher holds the far end of the gateway’s lifeline,GATEWAY_PARENT_LIFELINE_FD, and its death closes it. The gateway logsgateway parent launcher goneand drains as on a first SIGTERM; a lifeline that closes during a drain already running changes nothing. Since nobody asked for that drain, the first signal after it (a cleanup SIGTERM, say) is ignored like a repeat, and only a second one more than 1 second later forces the exit. A SIGKILL sent to the gateway itself still ends it at once.
How the NPX launcher passes these signals on is in binary and NPX.
Inspect the applied values
Section titled “Inspect the applied values”With the existing boot_settings_view flag and configuration-read permissions, Settings & sources → Lifecycle settings shows two read-only applied/default/source rows. The administrative configuration API exposes them as the separate infrastructure.lifecycle_settings array. Each row has the known key, group: lifecycle, paired applied value/source or null, a documented default and restart_required: true. Sources are default, YAML or environment. The shutdown default may be formatted 2m10s, equivalent to 130s.
An uncaptured programmatic factory reports unavailable, not inferred settings. The startup log and administrative snapshot use the same once-captured values consumed by shutdown. Unrelated edits/replica refreshes preserve them; changes require a rollout. The visibility flag hides this view without disabling drain/shutdown. No hot writer or extra permission is introduced. The three HTTP input-budget rows and startup healthcheck summary are independent. Access loss clears the page’s prior snapshots until an authorized refresh succeeds.
Size the external grace period
Section titled “Size the external grace period”The shipped Kubernetes and Compose profiles use 180 seconds, with the application defaults 10s/130s in a Pod and 2s/130s under Compose. Helm’s legacy gateway.lifecycle.drainDelaySeconds and gateway.lifecycle.shutdownTimeoutSeconds (rendered as GATEWAY_DRAIN_DELAY and GATEWAY_SHUTDOWN_TIMEOUT) default to null, so they do not mask YAML; an explicit integer, including drain zero, remains an environment override. Compose empty overrides and the base manifests also leave YAML/defaults in charge.
Helm validates known effective durations, adds them before rounding up to whole seconds, and requires at least that sum plus a 40-second cleanup allowance (180 seconds with the defaults in a Pod, 10 s + 130 s + 40 s). The allowance covers the bounded cleanup windows that run one after another once drain and shutdown have elapsed, 38 seconds in the worst case: chat cache writes 6s, log destinations 10s, request events 5s, queued log records 2s (asynchronous log writes), the final OTLP metrics snapshot 2s, traces 5s, logs 5s and the spans still queued for the span store 3s. The remaining 2 seconds absorb scheduling and process exit. Worker and database waits are not bounded, so this is not an aggregate bounded-exit guarantee. Increase the outer grace for your workload and dependencies. A long stream still needs appropriate stream clocks and a sufficient application shutdown timeout.
For an existing external ConfigMap, Helm cannot read its lifecycle YAML. It checks only explicit overrides and the permitted lower bounds of unknown fields; the rendered comment marks that limitation. You must align the real configuration and gateway.lifecycle.terminationGracePeriodSeconds yourself. Likewise, Helm cannot inspect additional environment values from an external Secret. Configuration of an endpoint or grace value never provisions infrastructure or certifies the cluster’s termination behavior.
Compose’s GATEWAY_STOP_GRACE_PERIOD controls its external deadline. Native process managers need their own stop timeout. Kubernetes HTTP GET probes remain independent of the standalone healthcheck target. See Kubernetes availability for readiness propagation and workload placement.
Validate a rollout
Section titled “Validate a rollout”Run gateway validate with the intended configuration and overrides. Confirm the applied sources in the console/API. In an isolated test, start a streaming request, send SIGTERM to the gateway process, observe readiness withdrawal and refusal of new operations, then verify completion or cancellation and correlated terminal usage/receipt state. Test WebSockets separately. Retain logs without payloads or credentials, and remove only your own test resources.
The deterministic gate covers parsing, capture, privacy, clone/permission/flag behavior, controlled HTTP completion/cancellation, and actual Helm duration/grace conformance. Independent native gateway/PostgreSQL/browser/signal profiles complement these checks. Local validation does not certify every process manager, Kubernetes distribution, public provider or installed distribution artifact.