Engine's gracefulStopTimeout equals the Helm chart's terminationGracePeriodSeconds, so a restart with a client attached loses the whole local cache
What happened?
On Kubernetes, restarting the engine while any client is connected reliably destroys the entire local cache. We lost ~800 GB / 16 minutes to this on an ordinary Helm-values rollout, and the mechanism turns out to be a deadline that exactly equals the one the Helm chart ships.
The engine's own shutdown budget is the same 5 minutes as the chart's default kill deadline.
cmd/engine/main.go:55—gracefulStopTimeout = 5 * time.Minutehelm/dagger/values.yaml—engine.terminationGracePeriodSeconds: 300
So the deadline the engine gives itself to finish shutting down expires at the same instant the kubelet SIGKILLs it. There is zero margin, and the step that saves the cache is last in line:
cmd/engine/main.go:489—grpcServer.GracefulStop(). Takes no context, so it is not bounded bystopCtx, and it blocks until every client connection drains.engine/server/server.go:743—srv.GracefulStopruns a full cache prune.dagql/cache.go:3051—engineCache.Close()writesclean_shutdown=1. Only this.
Anything that stalls step 1 means step 3 never runs, the next start reads clean_shutdown=0, and the engine discards the whole local cache.
This also explains a symptom that looked like the engine being slow to flush, when it was actually blocked. Timeline from one of our rollouts (v0.21.8):
11:25:47 SIGTERM. "engine is shutting down", in-flight session cancelled.
11:26:24 engine keeps emitting its per-minute metrics line, dagger-session-count=0,
... uptime incrementing. It has nothing to do for ~3.5 minutes and does not exit.
11:29:24
11:30:52 SIGKILL at the 300s boundary.
11:30:54 new engine: cleanShutdown=0 -> wipes ~800 GB over 16 minutes.Note also internal/buildkit/util/appcontext/appcontext.go: the first SIGTERM only cancels a context, and the process exits on the third signal via os.Exit(1). Nothing in between makes it terminate.
Confirmation that draining is the missing piece
We raised terminationGracePeriodSeconds to 600 and, separately, waited for dagger_connected_clients == 0 before restarting. Both engines then shut down cleanly, and the whole graceful stop took ~2 and ~4 seconds respectively:
19:01:42.654 starting dagql cache close
19:01:44.170 marking dagql cache clean shutdown
19:01:44.179 successfully marked clean shutdown in persistence metadata
19:01:44.188 completed dagql cache close successfullySo this was never about needing more time. It was about grpcServer.GracefulStop() blocking on an attached client until the kubelet killed the process.
Suggested fixes
- Bound
grpcServer.GracefulStop()— run it in a goroutine and fall back toStop()whenstopCtxexpires, so the cache save is always reached. - Make
gracefulStopTimeoutmeaningfully smaller than the chart'sterminationGracePeriodSeconds, or derive one from the other. Two identical deadlines guarantee the race. - Raise the chart default
terminationGracePeriodSecondsabove the engine's internal budget. - Consider writing
clean_shutdown=1earlier, or persisting incrementally, so a partial shutdown is not equivalent to no shutdown at all.
Dagger version
v0.21.8 (registry.dagger.io/engine:v0.21.8), dagger-helm chart 0.21.8, DaemonSet on k3s.
Source: dagger/dagger