Skip to content

fix(api): resolve event loop starvation in health/metrics endpoints#1337

Open
sushantguri wants to merge 2 commits into
ruvnet:mainfrom
sushantguri:fix-blocking-health-metrics
Open

fix(api): resolve event loop starvation in health/metrics endpoints#1337
sushantguri wants to merge 2 commits into
ruvnet:mainfrom
sushantguri:fix-blocking-health-metrics

Conversation

@sushantguri

Copy link
Copy Markdown

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

Copilot AI review requested due to automatic review settings July 15, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) via asyncio.to_thread, and switch cpu_percent to interval=None.
  • Update the v1 MetricsService system metrics collection to gather psutil metrics in a background thread and avoid interval=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.

Comment thread archive/v1/tests/unit/test_health_concurrency.py
Comment thread archive/v1/tests/unit/test_health_concurrency.py
Comment thread archive/v1/tests/unit/test_health_concurrency.py Outdated
Comment thread archive/v1/src/api/routers/health.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants