Skip to content

Instrument ate-apiserver so that it can shutdown gracefully. - #523

Merged
Benjamin Elder (BenTheElder) merged 1 commit into
agent-substrate:mainfrom
juli4n:apiserver-graceful-shutdown
Jul 24, 2026
Merged

Benjamin Elder (BenTheElder) merged 1 commit into
agent-substrate:mainfrom
juli4n:apiserver-graceful-shutdown

Conversation

@juli4n

@juli4n Julian Gutierrez Oschmann (juli4n) commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator

Make the server trap SIGTERM and initiate a graceful shutdown process. This involves reporting the Pod as not ready, and draining all in-flight gRPC requests so they can complete.

Part of #181

@HavenXia

Copy link
Copy Markdown
Member

Thanks, this is what I am defining for the upgrade model - the old instances need to be gracefully drained before shutdown.

Comment thread cmd/ateapi/main.go
if err := mux.Serve(lis); err != nil {
serverboot.Fatal(ctx, "Failed to serve", err)
}
slog.InfoContext(ctx, "Shutdown complete")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing waits on this to complete right? Should the main function wait for the drainOnShutdown to complete?

e.g

drainFinished := drainOnShutdown(shutdownCtx, mux, readiness)

if err := mux.Serve(lis); err != nil {
	serverboot.Fatal(ctx, "Failed to serve", err)
}
<-drainFinished

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. FWIW, I don't think this is a correctness thing as .Serve will only unblock once we are done with the shutdown, but there is one subtlety, the last log line in drainOnShutdown.

Not sure if it's worth the complexity tbh, but I added it as I like the log line discriminating a interrupted drain from a drain that completed gracefully.

@@ -141,6 +146,15 @@ spec:
port: 9090
initialDelaySeconds: 5
periodSeconds: 2

@michaelasp Michael Aspinwall (michaelasp) Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this run 3 times before the pod is marked unhealthy in the apiserver? That means that it would take ~6s to mark the pod unhealthy. Won't we start rejecting traffic at 5s in the ate-apiserver? That would mean we still aren't graceful unless we tweak some limits. I'd just test this in a kind cluster and make sure that the graceful shutdown timeouts are enough.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, your comment hadn't loaded yet, AI review suggests ~ the same #523 (comment)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From manual follow-up this is what jumps out the most.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted. It was off by a few seconds.

- --session-id-ca-pool=/run/session-id-ca-pool/pool.json
- --workerpool-ca-certs=/run/workerpool-ca-certs/trust-bundle.pem
# Graceful shutdown knobs. The sum must fit within terminationGracePeriodSeconds.
- --drain-delay=5s

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 should-fix 🟡 – The readiness signal can't land within this 5s window. The readinessProbe above sets periodSeconds: 2 and leaves failureThreshold at its default of 3, so kubelet needs ~6s of consecutive 503s before it marks the pod NotReady — after the drain has already closed the listener. As configured, MarkNotReady contributes nothing and removal from Endpoints rests entirely on the deletionTimestamp path.

Setting failureThreshold: 1 on the readinessProbe makes the probe react in ~2s, inside the drain delay.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted.

Comment thread cmd/ateapi/main.go Outdated
slog.InfoContext(ctx, "Shutdown complete")
}

func drainOnShutdown(ctx context.Context, srv *grpc.Server, readiness *serverboot.Readiness) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 nit 🟢 – drainOnShutdown reads the flag globals directly, so the sequence it orchestrates (mark unready → delay → graceful stop → force stop on timeout) can't be unit tested. Passing the two durations as parameters would make it testable, like metricsMux was split out for the same reason.

Make the server trap SIGTERM and initiate a graceful shutdown process.
This involves reporting the Pod as not ready, and draining all
in-flight gRPC requests so they can complete.
@BenTheElder
Benjamin Elder (BenTheElder) merged commit 04aee6c into agent-substrate:main Jul 24, 2026
11 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.

4 participants