Worker healthcheck reports unhealthy during Prefect Cloud maintenance, so liveness probes restart workers for the whole window
Bug summary
A worker started with --with-healthcheck behind a liveness probe is restarted over and over for as long as Prefect Cloud is in maintenance, even though nothing is wrong with the worker. The official prefect-worker Helm chart sets up exactly this with livenessProbe.enabled: true.
During maintenance, Cloud answers every request with 503, Prefect-Maintenance: true and a Retry-After. The client handles this as intended: it retries indefinitely and sleeps out each Retry-After. The health check doesn't take that into account, in two ways:
- The poll can't complete during the window.
get_and_submit_flow_runskeeps waiting out the maintenance back-off, so the time since the last successful poll grows for the whole window. AfterPREFECT_WORKER_QUERY_SECONDS × 30(300s by default),/healthreturns 503 and the probe restarts the worker. - After a restart,
/healthisn't served untilsetup()finishes.BaseWorker.startstarts the health server only afterasync with self, andsetup()awaits the first sync with the API, which also waits out the maintenance. Nothing listens on the health port, so the probe fails again about 40s after start. This repeats until the window ends.
A restart can't shorten the maintenance, and it has costs:
- Process workers lose every flow run they're executing, because the runs are child processes.
- After the window, CrashLoopBackOff can delay the worker's return by up to 5 more minutes.
Reproduction. We ran a maintenance window on our dev stack with two Kubernetes workers, using the Helm chart's default probe (/health on :8080, 10s period, 3 failures):
- Stock 3.8.7: restarted 9 times in a ~16-minute window. Every restart after the first was killed about 40s after start.
- Patched build (#23230): stayed at 0 restarts, with
/healthreturning 200 throughout.
To reproduce locally, point a worker at an API that returns 503 with Prefect-Maintenance: true and Retry-After: 120. Run it with --with-healthcheck and poll /health: it turns 503 after 300s, and after a restart the port refuses connections until the API recovers.
Expected: while the API is telling the process to wait out maintenance, the worker isn't reported unhealthy, and /health answers from the moment the worker starts.
Version info
Version: 3.8.7
API version: 0.8.4
Python version: 3.11
Server type: cloud
Integrations:
prefect-kubernetesWorker image prefecthq/prefect:3.8.7-python3.11-kubernetes, deployed with the prefect-worker Helm chart and livenessProbe.enabled: true.
Additional context
- Proposed fix: #23230. It records when a maintenance back-off ends, and counts the health window from the later of the last poll and that point. It also starts the health server before
setup(), asRunner.startalready does. - Alternatives considered:
- Only moving the health server's start doesn't help: a setup blocked for more than 300s still reads unhealthy.
- Only raising the threshold doesn't help: the poll never completes during a window, so any fixed threshold is eventually exceeded.
- Waking the sleep early would defeat the staggered
Retry-After, which exists to spread clients out after the window.
- Runner too:
Runner/flow.serve()health checks have the same problem with a 20s threshold (missed_polls_tolerance × poll_frequency,src/prefect/runner/server.py). That would be a follow-up.
Source: PrefectHQ/prefect