Repository navigation
fix(deploy): drain on SIGTERM instead of dying mid-response - #438
Merged
Merged
Conversation
`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]>
This was referenced Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
axum::servewas called withoutwith_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 aRecreaterollout 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
with_graceful_shutdown(shutdown::on_sigterm()).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 shippednode_modules/next/dist/docsrather than assumed, perfrontend/AGENTS.md.preStopsleep withholds the signal while the pod keeps serving, andterminationGracePeriodSeconds: 60covers sleep + drain so the kubelet can't SIGKILL mid-drain and undo it.Two decisions in
shutdown.rsthat look like oversightsTwo things verified rather than assumed
Both would have been real regressions:
axum-0.8.9/src/serve/mod.rs: it delegates to hyper'sconn.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.preStopuses the nativesleepaction (GA 1.30; cluster is 1.36), notexec: ["sleep", "5"]. An exec hook depends on a shell and a coreutilssleepin the image. The day someone slims that image the hook fails as aFailedPreStopHookevent 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:
replicas: 1minAvailable: 1appNodePool'sWhenEmptyOrUnderutilizedconsolidation (consolidateAfter: 1m) and node upgradesmaxUnavailable: 1A 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.rsalready 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 checkgreen — fmt, clippy,cargo docwith-D warnings,er-entity verify,skills-check, workspace tests.helm template+kubeconform -strictaccept the native preStop action against the 1.32 schema, on both the default and kind profiles.🤖 Generated with Claude Code