Skip to content

fix(deploy): drain on SIGTERM instead of dying mid-response - #445

Merged
athvin merged 1 commit into
mainfrom
bm/bluegreen-p2-drain
Nov 3, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/bluegreen-p2-drain

Conversation

@athvin

@athvin athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Replaces #438. That PR was opened stacked on #437 and GitHub marked it MERGED when #437's branch was deleted on merge — but its code never reached main (shutdown.rs absent, with_graceful_shutdown count 0). Same commit, rebased onto main, targeting main directly this time.

Why

axum::serve was called without with_graceful_shutdown, so er-server had no shutdown path at all: the process died on the default SIGTERM handler and every request in flight became a connection reset at the client. On a Recreate rollout that's a handful of 502s at the edge every deploy, attributed to nothing.

It's also the half of a blue/green cutover that keeps the old colour correct. Traffic moves first; the pod being retired has to finish what it already accepted. Without this, "cut over, then scale down" loses whatever was in flight at scale-down — which would make the cutover look clean while quietly dropping requests.

Three parts, and the third is the one that's easy to miss

  1. er-server drains — with_graceful_shutdown(shutdown::on_sigterm()).
  2. er-web needs no code change. Next's App Router already finishes in-flight requests and runs pending after() callbacks on SIGTERM, and its self-hosting guide asks the platform for a 10–30s drain window — which the grace period now provides. Read from the shipped node_modules/next/dist/docs rather than assumed, per frontend/AGENTS.md.
  3. Endpoint removal is asynchronous with the signal. The kubelet signals the containers at the same moment the endpoint controller starts telling kube-proxy and the ALB to stop routing here. A process that drains perfectly and then exits still refuses the requests in flight across that gap. A preStop sleep withholds the signal while the pod keeps serving, and terminationGracePeriodSeconds: 60 covers sleep + drain so the kubelet can't SIGKILL mid-drain and undo it.

Two decisions in shutdown.rs that look like oversights

  • SIGTERM only; SIGINT keeps its default handler. Catching SIGINT would make the first local Ctrl-C wait — on a hung request, indefinitely — while a second did nothing, because the future has already resolved. Leaving it alone keeps dev behaviour exactly as it was.
  • A failed signal registration waits forever rather than resolving. The future is the server's shutdown trigger, so resolving it on error would shut the server down the instant it started serving: a total outage produced by the error path of a feature meant to prevent small ones.

Two things verified rather than assumed

Both would have been real regressions:

  • Does graceful shutdown wait for connections or requests? If connections, every pod would burn the full grace period on one open browser keep-alive. Read axum-0.8.9/src/serve/mod.rs: it delegates to hyper's conn.graceful_shutdown(), which closes idle keep-alive connections immediately and waits only on in-flight requests. A long Arrow stream will hold shutdown until it finishes, which is the desired behaviour and is bounded by the grace period.
  • preStop uses the native sleep action (GA 1.30; cluster is 1.36), not exec: ["sleep", "5"]. An exec hook depends on a shell and a coreutils sleep in the image. The day someone slims that image the hook fails as a FailedPreStopHook event while the container is signalled immediately — indistinguishable from having no preStop at all.

No PodDisruptionBudget, deliberately

The plan I was working from called for one. Looking at the actual topology, it would be a trap rather than a safeguard:

PDB Effect at replicas: 1
minAvailable: 1 pod is permanently un-evictable — blocks the app NodePool's WhenEmptyOrUnderutilized consolidation (consolidateAfter: 1m) and node upgrades
maxUnavailable: 1 a no-op

A PDB only means something once more than one replica serves, so it belongs with the colour dimension, where it has a replica count to key off.

Known gap, left alone on purpose

The in-process dispatcher (ER_DISPATCH_IN_PROCESS, dev-only) still passes a drain flag that is never set — dispatcher_bin.rs already documents this asymmetry. It's a real gap, left where the dispatcher's own cutover work can address it rather than widened into this change.

Verification

  • make check green — fmt, clippy, cargo doc with -D warnings, er-entity verify, skills-check, workspace tests.
  • helm template + kubeconform -strict accept the native preStop action against the 1.32 schema, on the default and kind profiles. Re-validated after rebasing onto main, confirming the probes (from fix(deploy): probes that can say no — readiness reads /readyz, not a literal #437) and the drain config compose correctly in one pod spec.

🤖 Generated with Claude Code

`axum::serve` was called without `with_graceful_shutdown`, so er-server had no
shutdown path at all: the process died on the default SIGTERM handler, and
every request in flight became a connection reset at the client. On a
`Recreate` rollout that is a handful of 502s at the edge every deploy,
attributed to nothing.

It is also the half of a blue/green cutover that keeps the OLD colour correct.
Traffic moves first; the pod being retired has to finish what it already
accepted before it goes. Without this, "cut over then scale down" loses
whatever was in flight at scale-down.

Three parts, and the third is the one that is easy to miss:

- **er-server drains.** `with_graceful_shutdown(shutdown::on_sigterm())`.
- **er-web needs no code change.** Next's App Router already finishes
  in-flight requests and runs pending `after()` callbacks on SIGTERM; its
  self-hosting guide asks the platform for a 10-30s drain window, which the
  grace period now provides. Verified in the shipped docs rather than assumed
  (`AGENTS.md` says to, and this Next major differs from training data).
- **Endpoint removal is asynchronous with the signal.** The kubelet signals the
  containers at the same moment the endpoint controller starts telling
  kube-proxy and the ALB to stop routing here, so a process that drains
  perfectly and then exits STILL refuses the requests in flight across that
  gap. A `preStop` sleep on each container withholds the signal while the pod
  keeps serving, and `terminationGracePeriodSeconds: 60` covers sleep + drain
  so the kubelet cannot SIGKILL mid-drain and undo it.

Two decisions inside `shutdown.rs` that look like oversights and are not:

- **SIGTERM only; SIGINT keeps its default handler.** Catching SIGINT would
  make the first local Ctrl-C *wait* — on a hung request, indefinitely — while
  a second did nothing, because the future has already resolved. Leaving it
  alone keeps dev behaviour exactly as it was.
- **A failed signal registration waits forever rather than resolving.** The
  future IS the server's shutdown trigger, so resolving it on error would shut
  the server down the instant it started serving: a total outage produced by
  the error path of a feature meant to prevent small ones.

`preStop` uses the native `sleep` action (GA since 1.30; the cluster is 1.36),
not `exec: ["sleep", "5"]`. An exec hook depends on a shell and a coreutils
`sleep` in the image, and the day someone slims the image that dependency fails
as a `FailedPreStopHook` event while the container is signalled immediately —
indistinguishable from having no preStop at all.

**No PodDisruptionBudget, deliberately** — the plan called for one and looking
at the actual topology it would be a trap, not a safeguard. At `replicas: 1`,
`minAvailable: 1` makes the pod permanently un-evictable, which blocks the
`app` NodePool's `WhenEmptyOrUnderutilized` consolidation and node upgrades;
`maxUnavailable: 1` is a no-op. A PDB only becomes meaningful alongside the
colour dimension, so it lands there with a replica count to key off.

The in-process dispatcher (`ER_DISPATCH_IN_PROCESS`, dev-only) still passes a
drain flag that is never set. That is a real gap, left where the dispatcher's
own cutover work can address it rather than widened into this change.

Verified: `make check` green; `helm template` + `kubeconform -strict` accept
the native preStop action against the 1.32 schema, on both the default and kind
profiles.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin merged commit 044628e into main Nov 3, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants