Skip to content

fix(supervisor): on SIGTERM wait (bounded) for the surge successor before draining; graceful drain fallback (#795) - #797

Merged
bpalermo merged 1 commit into
mainfrom
fix/795-supervisor-wait-then-drain
Sep 16, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/795-supervisor-wait-then-drain

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Closes #795. Cluster validation: delete-pod on two workers must return to rev213's numbers (≈22 s termination, 0 prober errors).

fix(supervisor): on SIGTERM wait (bounded) for the surge successor before draining; graceful drain fallback (#795)

#785 fixed the epoch-identity probe at shutdown (it had been running on an
already-cancelled context and failing in microseconds without opening a socket),
and in doing so removed the accident that had been keeping pod termination
hitless. With the probe working, "admin LIVE at our own epoch" now correctly
routed to s.shutdown() — a bare SIGTERM, which Envoy does not drain on. The
supervisor then WON the race against its own DaemonSet surge replacement:
rev214, two samples on worker-04/05, termination 2.27s/1.55s, the replacement
logging "no live predecessor; starting fresh at epoch 0", 7.95s/8.11s with no
Envoy on the node and 130/126 prober connection_errors, AetherProberLivenessErrors
paging CRITICAL. Pre-#785 on rev213 the same delete took 21-23s, drained Envoy
and cost zero prober errors. kubectl delete pod is also the code path of node
drain, eviction and preemption.

The design error is the premise that "no handoff in progress" means "nobody is
coming". For a DaemonSet with maxSurge and a transparent-capture node proxy the
replacement is created within ~1s of the deletionTimestamp and hot-restarts us.
So the pre-#785 behaviour becomes deliberate and bounded, and the fallback
becomes a real drain.

handleShutdown now resolves a SIGTERM onto exactly one of four branches, off one
authoritative /server_info probe (still on a detached ctx — #785's fix is kept,
and it is now also what tells "answered at another epoch" apart from "did not
answer at all"):

admin LIVE at a NEWER epoch genuine mid-handoff -> wait for the successor's
parent-shutdown protocol, bounded by #785's
successorWaitBudget. -> handoff
admin LIVE at OUR epoch no handoff yet -> keep the child SERVING and
wait for the surge successor on the same budget
(155s at the chart default). -> successor_wait
admin unreachable child dead or main thread wedged; nothing to
drain. -> child_dead
budget expired, or
--shutdown-drain-immediately POST /drain_listeners?graceful, wait
--drain-time (10s as deployed), then SIGTERM
and reap. -> drain_fallback

A serving Envoy is never bare-SIGTERMed again: shutdown() is split into
terminateChildren() plus the histogram, drainThenShutdown() wraps it with the
graceful drain, and shutdown()'s doc now says where it is and is not correct.
--shutdown-drain-immediately (default false, documented in docs/configuration.md,
NOT set by the chart) is the escape hatch for a deployment where no replacement
can overlap the pod; StateDir == "" takes the same path, since a supervisor with
no cross-pod coordination can have no successor either.

Observability (the drain path had none — "closing and draining listeners" is
Envoy's line, not ours, and aether_supervisor_drain_duration_* has never reached
Prometheus):

  • aether.supervisor.shutdown.branch counter, attribute
    aether.supervisor.shutdown.branch in {handoff, successor_wait, drain_fallback,
    child_dead}, SEEDED AT ZERO for all four (fix(agent): seed the #638 identity mismatch counters at zero (false-zero in Prometheus) #717: the OTel SDK exports a counter
    only after its first Add, and this one is incremented exactly once per pod
    lifetime, so unseeded "never taken" and "never registered" are the same empty
    query).
  • aether.supervisor.drain.duration now spans the WHOLE fallback — the graceful
    drain window included — and is recorded even when Envoy exits during its own
    drain sequence, which is the case terminateChildren then has nothing to report.
  • INFO lines: "waiting for a successor before draining" (with the budget),
    "successor took over during shutdown wait", "no successor within budget;
    draining listeners", "listeners drained; stopping envoy". The existing WARN
    fallback line and every existing counter are kept.

Tests (agent/internal/proxy/hotrestart/shutdown_test.go, extending #785's;
fakeAdminServer gains a /drain_listeners handler recording hits and the query
string):

  • TestHandleShutdownWaitsForTheSurgeSuccessor — LIVE at our epoch, budget 14s,
    successor arrives at 2s: the child is still tracked and undrained at 1s, the
    wait ends on the successor and not at the budget, zero drain calls, and the
    probe really reached the admin (proxy-supervisor: shutdown admin probe runs on the already-cancelled ctx, so SIGTERM may never drain Envoy (needs reproduction) #771's assertion, carried over).
  • TestHandleShutdownDrainsGracefullyWhenNoSuccessorArrives — LIVE at our epoch,
    budget 2s, nobody comes: /drain_listeners?graceful hit exactly once, the
    budget plus DrainTime is spent, then SIGTERM and reap.
  • TestHandleShutdownDrainsImmediatelyWhenToldTo — the escape hatch drains at
    once, still gracefully, without spending the budget.
  • TestHandleShutdownStopsADeadChildWithoutDraining — an unreachable admin is not
    asked to drain and does not enter a successor wait.
  • TestHandleShutdownWaitsForGenuineSuccessor and
    TestHandleShutdownFallsBackToDrainWhenNoSuccessorComes (the mid-handoff branch)
    are unchanged in behaviour; the latter now also asserts the fallback is the
    graceful drain.
  • TestShutdownBranchesSeededAtZero / TestShutdownBranchRecording.

Proof the first test catches the production regression: with handleShutdown's
body replaced by main's, TestHandleShutdownWaitsForTheSurgeSuccessor fails at
shutdown_test.go:94 — "envoy was stopped while a surge successor could still have
hot-restarted it" after 1.03s — because main takes the drain branch immediately
and the child is already SIGTERMed and reaped. The two drain tests fail on main
at 33ms with zero /drain_listeners hits.

#785's TestHandleShutdownProbesOnDetachedCtxAndDrains asserted the very behaviour
this issue reverses (drain immediately when LIVE at our own epoch), so it is
rewritten rather than removed: its #771 assertion — that the branch decision was
made off a probe that really reached the network — is carried into the new test
above, which is also what still fails if the WithoutCancel is reverted.

Docs: docs/runbook.md gains a supervisor/termination section with the four-branch
table, what to expect on kubectl delete pod (~20-25s, zero data-plane cost, the
log lines to grep, and why a 1-2s termination is the alarming outcome), and the
PromQL for the seeded branch counter; docs/configuration.md documents
--shutdown-drain-immediately and --termination-grace. No chart change.

Verification: bazel build //... //test/e2e:e2e_test; bazel test //...
(105 pass); bazel test --config=race --runs_per_test=3 on
//agent/internal/proxy/hotrestart, //agent/internal/supervisorcmd and
//agent/cmd/proxy-supervisor:deps_test; scripts/check-proxy-supervisor-deps.sh OK
(the supervisor links none of the forbidden packages); //test/envoy_validate
pass; bazel run //:format.check clean; bazel build --config=lint
--@aspect_rules_lint//lint:fail_on_violation //... clean; bazel run //:gazelle no
churn; bazel mod tidy idempotent; scripts/go-deps-audit.sh OK.

Refs #771, #785, #772.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

…fore draining; graceful drain fallback (#795)

#785 fixed the epoch-identity probe at shutdown (it had been running on an
already-cancelled context and failing in microseconds without opening a socket),
and in doing so removed the accident that had been keeping pod termination
hitless. With the probe working, "admin LIVE at our own epoch" now correctly
routed to s.shutdown() — a bare SIGTERM, which Envoy does not drain on. The
supervisor then WON the race against its own DaemonSet surge replacement:
rev214, two samples on worker-04/05, termination 2.27s/1.55s, the replacement
logging "no live predecessor; starting fresh at epoch 0", 7.95s/8.11s with no
Envoy on the node and 130/126 prober connection_errors, AetherProberLivenessErrors
paging CRITICAL. Pre-#785 on rev213 the same delete took 21-23s, drained Envoy
and cost zero prober errors. `kubectl delete pod` is also the code path of node
drain, eviction and preemption.

The design error is the premise that "no handoff in progress" means "nobody is
coming". For a DaemonSet with maxSurge and a transparent-capture node proxy the
replacement is created within ~1s of the deletionTimestamp and hot-restarts us.
So the pre-#785 behaviour becomes deliberate and bounded, and the fallback
becomes a real drain.

handleShutdown now resolves a SIGTERM onto exactly one of four branches, off one
authoritative /server_info probe (still on a detached ctx — #785's fix is kept,
and it is now also what tells "answered at another epoch" apart from "did not
answer at all"):

  admin LIVE at a NEWER epoch   genuine mid-handoff -> wait for the successor's
                                parent-shutdown protocol, bounded by #785's
                                successorWaitBudget.            -> handoff
  admin LIVE at OUR epoch       no handoff yet -> keep the child SERVING and
                                wait for the surge successor on the same budget
                                (155s at the chart default).    -> successor_wait
  admin unreachable             child dead or main thread wedged; nothing to
                                drain.                          -> child_dead
  budget expired, or
  --shutdown-drain-immediately  POST /drain_listeners?graceful, wait
                                --drain-time (10s as deployed), then SIGTERM
                                and reap.                       -> drain_fallback

A serving Envoy is never bare-SIGTERMed again: shutdown() is split into
terminateChildren() plus the histogram, drainThenShutdown() wraps it with the
graceful drain, and shutdown()'s doc now says where it is and is not correct.
--shutdown-drain-immediately (default false, documented in docs/configuration.md,
NOT set by the chart) is the escape hatch for a deployment where no replacement
can overlap the pod; StateDir == "" takes the same path, since a supervisor with
no cross-pod coordination can have no successor either.

Observability (the drain path had none — "closing and draining listeners" is
Envoy's line, not ours, and aether_supervisor_drain_duration_* has never reached
Prometheus):
- aether.supervisor.shutdown.branch counter, attribute
  aether.supervisor.shutdown.branch in {handoff, successor_wait, drain_fallback,
  child_dead}, SEEDED AT ZERO for all four (#717: the OTel SDK exports a counter
  only after its first Add, and this one is incremented exactly once per pod
  lifetime, so unseeded "never taken" and "never registered" are the same empty
  query).
- aether.supervisor.drain.duration now spans the WHOLE fallback — the graceful
  drain window included — and is recorded even when Envoy exits during its own
  drain sequence, which is the case terminateChildren then has nothing to report.
- INFO lines: "waiting for a successor before draining" (with the budget),
  "successor took over during shutdown wait", "no successor within budget;
  draining listeners", "listeners drained; stopping envoy". The existing WARN
  fallback line and every existing counter are kept.

Tests (agent/internal/proxy/hotrestart/shutdown_test.go, extending #785's;
fakeAdminServer gains a /drain_listeners handler recording hits and the query
string):
- TestHandleShutdownWaitsForTheSurgeSuccessor — LIVE at our epoch, budget 14s,
  successor arrives at 2s: the child is still tracked and undrained at 1s, the
  wait ends on the successor and not at the budget, zero drain calls, and the
  probe really reached the admin (#771's assertion, carried over).
- TestHandleShutdownDrainsGracefullyWhenNoSuccessorArrives — LIVE at our epoch,
  budget 2s, nobody comes: /drain_listeners?graceful hit exactly once, the
  budget plus DrainTime is spent, then SIGTERM and reap.
- TestHandleShutdownDrainsImmediatelyWhenToldTo — the escape hatch drains at
  once, still gracefully, without spending the budget.
- TestHandleShutdownStopsADeadChildWithoutDraining — an unreachable admin is not
  asked to drain and does not enter a successor wait.
- TestHandleShutdownWaitsForGenuineSuccessor and
  TestHandleShutdownFallsBackToDrainWhenNoSuccessorComes (the mid-handoff branch)
  are unchanged in behaviour; the latter now also asserts the fallback is the
  graceful drain.
- TestShutdownBranchesSeededAtZero / TestShutdownBranchRecording.

Proof the first test catches the production regression: with handleShutdown's
body replaced by main's, TestHandleShutdownWaitsForTheSurgeSuccessor fails at
shutdown_test.go:94 — "envoy was stopped while a surge successor could still have
hot-restarted it" after 1.03s — because main takes the drain branch immediately
and the child is already SIGTERMed and reaped. The two drain tests fail on main
at 33ms with zero /drain_listeners hits.

#785's TestHandleShutdownProbesOnDetachedCtxAndDrains asserted the very behaviour
this issue reverses (drain immediately when LIVE at our own epoch), so it is
rewritten rather than removed: its #771 assertion — that the branch decision was
made off a probe that really reached the network — is carried into the new test
above, which is also what still fails if the WithoutCancel is reverted.

Docs: docs/runbook.md gains a supervisor/termination section with the four-branch
table, what to expect on `kubectl delete pod` (~20-25s, zero data-plane cost, the
log lines to grep, and why a 1-2s termination is the alarming outcome), and the
PromQL for the seeded branch counter; docs/configuration.md documents
--shutdown-drain-immediately and --termination-grace. No chart change.

Verification: bazel build //... //test/e2e:e2e_test; bazel test //...
(105 pass); bazel test --config=race --runs_per_test=3 on
//agent/internal/proxy/hotrestart, //agent/internal/supervisorcmd and
//agent/cmd/proxy-supervisor:deps_test; scripts/check-proxy-supervisor-deps.sh OK
(the supervisor links none of the forbidden packages); //test/envoy_validate
pass; bazel run //:format.check clean; bazel build --config=lint
--@aspect_rules_lint//lint:fail_on_violation //... clean; bazel run //:gazelle no
churn; bazel mod tidy idempotent; scripts/go-deps-audit.sh OK.

Refs #771, #785, #772.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant