Skip to content

fix(k8s): let gateway rollouts and database restarts finish open calls #5301

Closed

Description

@dan-petty

Problem Description

Three settings of the LiteLLM gateway and its database use a Kubernetes or CloudNativePG default. Each default cuts open model calls or stops the gateway start.

  1. Gateway pod grace period. k8s/llm/gateway/deployment.yaml sets no terminationGracePeriodSeconds and no preStop, so the default of 30 s applies. On SIGTERM, uvicorn waits for the open streams. LiteLLM 1.103.11 passes no timeout_graceful_shutdown (proxy_cli.py:282), so uvicorn waits until the kubelet kills it. The router timeout is 1500 s (k8s/llm/gateway/config.yaml:247), and the devops-background timeout is 1100 s. Thus a call that is open at a rollout fails after 30 s. The gateway rolled 10 times in 7 days.
  2. Database smart shutdown. The gateway CloudNativePG Cluster llm-gateway-db has 1 instance (k8s/llm/gateway-db.yaml:91), smartShutdownTimeout 180 (the default) and primaryUpdateMethod restart. On an image change, the operator restarts the only instance. The smart shutdown waits up to 180 s for each session to end, and the old gateway pod keeps its Prisma pool open. During that time, the database refuses new connections. LiteLLM v1 tries the migration check 4 times in about 5 s with no delay (litellm_proxy_extras utils.py:1346, 1379-1381) and then exits with code 1. The new gateway pod crash-loops, and the kubelet back-off is the only wait.
  3. Probe timeout. The liveness and readiness probes (k8s/llm/gateway/deployment.yaml:139-154) set no timeoutSeconds, so the default of 1 s applies. The gateway runs 1 uvicorn worker. The unpaginated v1 /spend/logs reads and serializes up to 10,000 rows in that worker. During that time, /health/liveliness waits for more than 1 s.

Reproduction Steps

  1. Start a long call through the gateway, for example a roadmap refine call on devops-background.
  2. While the call is open, run kubectl -n llm rollout restart deployment/llm-gateway.
  3. Read the old pod log in Loki. It logs Waiting for connections to close and no Finished server process. The client gets an error about 30 s after the rollout.
  4. Change the PostgreSQL image of the Cluster llm-gateway-db while a gateway pod holds open sessions. The database logs the database system is shutting down for 180 s. A gateway pod that starts in that window logs P1001 and exits with code 1.
  5. Call v1 GET /spend/logs?start_date=<day>&end_date=<next day> on a day with about 10,000 rows. The kubelet records Unhealthy probe events with context deadline exceeded.

Expected vs Actual Behavior

Expected: a rollout lets the old pod finish its open calls. A restart of the gateway database takes seconds. One slow request does not make the probes fail.

Actual:

  • 3 of 10 shutdowns in 7 days logged Waiting for connections to close and no Finished server process (2026-11-10T00:30:53Z, 2026-11-11T03:14:30Z and 2026-11-12T14:32:42Z). About 30 s later, roadmap-service logged Refine skipped #4404 ... AIClientError, LLM_INFERENCE_ERROR (2026-11-11T03:15:00Z) and Refine skipped #5100 ... (2026-11-12T14:33:12Z).
  • On 2026-11-12, the database refused connections for 3 minutes, and the new gateway pod crash-looped (restart count 4).
  • 28 probe failures in 7 days. There was no restart, because the failures never reached 3 in a row.

Environment Diagnostics

  • DevOps CLI Version: 0.124.0
  • Gateway: LiteLLM v1.103.11, 1 replica, 1 uvicorn worker, RollingUpdate (default strategy)
  • Database: CloudNativePG Cluster llm-gateway-db, 1 instance, PostgreSQL 17.11
  • Execution Environment: homelab Kubernetes cluster, managed by Argo CD

Logs, Traces, or Terminal Output

# Database, 2026-11-12 (an image-drift restart, not a node drain: the node events show no drain at 14:28)
14:28:42 Detected drift ... runningImages 17.2, latestImages 17.11 ... smartShutdownTimeout 180
14:28:43-14:31:43 275 x "the database system is shutting down", then "Requesting fast shutdown"
# 2 idle sessions of the old gateway pod held the shutdown open.
# New gateway pod, 14:29:41-14:31:28
16 x P1001, 4 x "Database setup failed after multiple retries", restartCount 4
# Probes, 7 days: 28 Unhealthy "context deadline exceeded" events on /health/liveliness
# (20 readiness, 8 liveness, 2026-11-08T13:26Z to 2026-11-09T23:11Z). Each event came about
# 3 s before "GET /spend/logs?start_date=2026-11-09&end_date=2026-11-10" from a port-forward,
# every 70 s. CPU was 0.04 to 0.25 core.
WARNING spend_management_endpoints.py:331 /spend/logs result truncated to the 10000 most recent rows

Proposed Fix

  • k8s/llm/gateway/deployment.yaml (pod spec): set spec.template.spec.terminationGracePeriodSeconds: 600. This covers a normal long call. The upper limit is the router timeout of 1500 s. RollingUpdate starts the new pod first, so the longer grace period only lets the old pod finish its calls and flush its spend queue.
  • k8s/llm/gateway-db.yaml (Cluster spec): set spec.smartShutdownTimeout: 15. A restart then takes seconds.
  • k8s/llm/gateway/deployment.yaml:139-154: set timeoutSeconds: 5 on livenessProbe and readinessProbe.
  • Keep allow_requests_on_db_unavailable unset, as the comment at k8s/llm/gateway/config.yaml:285 requires.
  • Do not put the PostgreSQL image and the LiteLLM image in one manual bump again. Commit 3109817 (build(deps): take every safe version update before Renovate #5102) changed both together. Renovate already updates the two images in separate pull requests (.github/renovate.json has no group for them).
  • Ad-hoc callers use /spend/logs/v2 (paginated) or the Prometheus spend series, not v1 /spend/logs. No code in src calls /spend/logs.

Related: #5267 (a CloudNativePG window for a node drain), #5091 (the next gateway upgrade, which also rolls the pod).

Acceptance Criteria

  • kustomize build k8s/llm/gateway shows terminationGracePeriodSeconds: 600 on the pod and timeoutSeconds: 5 on both probes.
  • The live Cluster llm-gateway-db shows smartShutdownTimeout: 15.
  • A rollout during a long call (more than 30 s) ends with Finished server process in the old pod log, and the client gets a full reply.
  • The next restart of the gateway database ends in less than 30 s, and the restart count of the gateway pod does not increase.
  • A v1 /spend/logs call of 10,000 rows causes no Unhealthy probe event on the gateway.
  • allow_requests_on_db_unavailable stays unset in k8s/llm/gateway/config.yaml.

Activity

  1. added
    source/agent
    type/bug
    scope/k8s
    status/new
    value/high
    effort/medium
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. Three of ten rollouts in seven days killed open AI calls and the database restart caused a crash-loop.
    • Related: test(k8s): verify the LiteLLM gateway v1.103.10 rollout #4373, which asks for a different change. Both concern the gateway rollout, but #4373 verifies one rollout while the candidate fixes the rollout to not kill open calls.
  3. added
    priority/p1-high
    owner/agent
    status/in-review
    and removed
    status/new
    on Nov 13, 2026
  4. removed
    status/in-review
    owner/agent
    on Nov 13, 2026
  5. devops-cli-service commented on Nov 13, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-13 03:45 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p1-highscope/k8ssource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions