fix(api): resolve event loop starvation in health/metrics endpoints#1337
Open
sushantguri wants to merge 2 commits into
Open
fix(api): resolve event loop starvation in health/metrics endpoints#1337sushantguri wants to merge 2 commits into
sushantguri wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR targets event-loop starvation in the archived v1 FastAPI health/metrics paths by removing the 1s-blocking psutil.cpu_percent(interval=1) usage and offloading OS-metrics collection to background threads to keep the async loop responsive.
Changes:
- Offload health/metrics router metrics collection (
get_system_metrics,get_detailed_metrics) viaasyncio.to_thread, and switchcpu_percenttointerval=None. - Update the v1
MetricsServicesystem metrics collection to gather psutil metrics in a background thread and avoidinterval=1. - Add a concurrency regression test and document the fix in
CHANGELOG.md.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| CHANGELOG.md | Documents the health/metrics event-loop starvation fix. |
| archive/v1/tests/unit/test_health_concurrency.py | Adds a regression test intended to detect event loop blocking during metrics gathering. |
| archive/v1/src/services/metrics.py | Avoids blocking the event loop during periodic system metrics collection. |
| archive/v1/src/api/routers/health.py | Avoids blocking the event loop in /health and /metrics endpoints by thread offloading + non-blocking CPU percent. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Fixes # — replaces blocking psutil.cpu_percent(interval=1) with non-blocking interval=None wrapped in asyncio.to_thread(). Metrics latency: ~1,000ms → 0.8ms. Regression test added