RuView icon indicating copy to clipboard operation
RuView copied to clipboard

Title: fix: psutil.cpu_percent(interval=1) blocks FastAPI async event loop

Open sushantguri opened this issue 4 months ago • 2 comments

psutil.cpu_percent(interval=1) in health/metrics endpoints sleeps for 1 second synchronously, freezing the entire async event loop on every health check. Fix: use interval=None + asyncio.to_thread().

sushantguri avatar Jul 15 '26 13:07 sushantguri

Confirmed. psutil.cpu_percent(interval=1) performs a blocking 1-second sleep inside the sampling call — in an async FastAPI handler this freezes the entire event loop and stalls every in-flight request for the duration of each health/metrics poll.

Standard fix:

  1. On startup (or first call), prime the counter with psutil.cpu_percent(interval=None) — returns 0.0 but doesn't block.
  2. In the endpoint handler, offload the blocking sample: await asyncio.to_thread(psutil.cpu_percent, interval=0.1) — runs in a thread pool, keeps the event loop free.

This should be applied to every health/metrics route in the Python stack that currently calls psutil.cpu_percent(interval=...) synchronously.


Generated by Claude Code

ruvnet avatar Jul 18 '26 13:07 ruvnet

Fixed in PR #1355 and included in the release sweep. Archived FastAPI health/metrics sampling now uses non-blocking interval=None and thread offload; the event-loop responsiveness regression test passes. Closing as fixed.

ruvnet avatar Jul 18 '26 22:07 ruvnet