-
Notifications
You must be signed in to change notification settings - Fork 11k
fix(api): resolve event loop starvation in health/metrics endpoints #1337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sushantguri
wants to merge
2
commits into
ruvnet:main
Choose a base branch
from
sushantguri:fix-blocking-health-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−14
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import asyncio | ||
| import time | ||
| import os | ||
| import sys | ||
|
|
||
| # Add project root and archive/v1 to sys.path so we can import src modules | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../"))) | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) | ||
|
|
||
| from archive.v1.src.api.routers.health import get_system_metrics | ||
|
|
||
| async def ticker(): | ||
| """Asynchronous background ticker to measure event loop latency/freezes.""" | ||
| ticks = [] | ||
| for _ in range(15): | ||
| ticks.append(time.time()) | ||
| await asyncio.sleep(0.1) | ||
| return ticks | ||
|
|
||
| async def run_test(): | ||
| print("Starting concurrency verification test...") | ||
|
|
||
| # Start the ticker background task | ||
| ticker_task = asyncio.create_task(ticker()) | ||
|
|
||
| # Let ticker run for a few ticks | ||
| await asyncio.sleep(0.3) | ||
|
|
||
| print("Calling get_system_metrics offloaded to background thread...") | ||
| start_time = time.time() | ||
|
|
||
| # Query system metrics using to_thread (simulating FastAPI request) | ||
| metrics = await asyncio.to_thread(get_system_metrics) | ||
|
|
||
| duration = time.time() - start_time | ||
| print(f"get_system_metrics took: {duration:.4f}s") | ||
|
|
||
| # Wait for the ticker to complete | ||
| ticks = await ticker_task | ||
|
|
||
| # Calculate gaps between consecutive ticks to check for event loop freezes | ||
| gaps = [ticks[i+1] - ticks[i] for i in range(len(ticks)-1)] | ||
| max_gap = max(gaps) | ||
|
|
||
| print(f"All tick gaps: {[round(g, 3) for g in gaps]}") | ||
| print(f"Max event loop freeze: {max_gap:.4f}s") | ||
|
|
||
| # In pre-fix code, psutil.cpu_percent(interval=1) blocks for 1.0s, | ||
| # causing a gap of >1.0s. With our fix, it should be close to 0.1s. | ||
| if max_gap >= 0.35: | ||
| print("FAIL: Event loop was frozen/blocked!") | ||
| sys.exit(1) | ||
| else: | ||
| print("SUCCESS: Event loop remained fully responsive during metrics query!") | ||
| sys.exit(0) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_system_metrics_does_not_starve_event_loop(): | ||
| max_gap, duration = await run_test() | ||
| # ticker sleeps 0.1s; allow slack for CI, but we should not see ~1s gaps | ||
| assert max_gap < 0.6 | ||
| assert duration < 0.6 |
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.
Uh oh!
There was an error while loading. Please reload this page.