Add Prometheus child_exit cleanup for gunicorn workers#22324
Merged
ryan-crabbe merged 1 commit intomainfrom Feb 28, 2026
Merged
Add Prometheus child_exit cleanup for gunicorn workers#22324ryan-crabbe merged 1 commit intomainfrom
ryan-crabbe merged 1 commit intomainfrom
Conversation
When a gunicorn worker exits (e.g. from max_requests recycling), its per-process prometheus .db files remain on disk. For gauges using livesum/liveall mode, this means the dead worker's last-known values persist as if the process were still alive. Wire gunicorn's child_exit hook to call mark_process_dead() so live-tracking gauges accurately reflect only running workers.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Greptile SummaryThis PR adds automatic Prometheus metric cleanup when gunicorn worker processes exit, preventing "ghost" gauge values from dead workers. It hooks into gunicorn's
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| litellm/proxy/prometheus_cleanup.py | Adds mark_worker_exit() function that calls prometheus_client.multiprocess.mark_process_dead() for a dead worker PID, with proper env var guard and exception handling. Clean and correct. |
| litellm/proxy/proxy_cli.py | Registers gunicorn child_exit hook conditionally when PROMETHEUS_MULTIPROC_DIR is set. Correct integration point, though import is inline rather than module-level. |
| tests/test_litellm/proxy/test_prometheus_cleanup.py | Adds 3 well-structured mock-only tests for mark_worker_exit(): positive case, env-not-set noop, and exception handling. No real network calls. |
Sequence Diagram
sequenceDiagram
participant G as Gunicorn Master
participant W as Worker Process
participant PC as proxy_cli.py
participant PM as prometheus_cleanup.py
participant PL as prometheus_client
G->>W: Worker starts
W->>W: Serves requests (writes .db files)
W-->>G: Worker exits (crash/recycle)
G->>PC: child_exit(server, worker)
PC->>PM: mark_worker_exit(worker.pid)
PM->>PM: Check PROMETHEUS_MULTIPROC_DIR env
PM->>PL: multiprocess.mark_process_dead(pid)
PL->>PL: Remove stale .db files for PID
Last reviewed commit: 4fa6742
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewCI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
Changes
For those running gunicorn we can hook into when child workers die and on that we clean up (using prometheus'
mark_process_dead) all the stale live tracking files for that worker.