-
-
Notifications
You must be signed in to change notification settings - Fork 621
BUG: Sync-microservices: Health Check Blocks Main Event Loop (Performance Risk) #1033
Copy link
Copy link
Open
Description
Is there an existing issue for this?
- I have searched the existing issues
What happened?
Description
The /health endpoint is defined as async, but it calls synchronous blocking database operations. This causes the entire FastAPI application to freeze for all users while the database check is performing I/O.
How to reproduce
- Simulate a slow database connection (e.g., lock the sqlite DB file).
- Send a request to
GET /health - Simultaneously try to hit
GET /watcher/status - Observe that the status request hangs until the health check completes.
Root cause
In health.py
@router.get("/health", response_model=HealthCheckResponse)
async def health_check():
# This function is blocking I/O!
db_status = db_check_database_connection()
return ...
And in folders.py db_check_database_connection() uses sqlite3.connect() , which is a synchronous blocking call. Using blocking code inside an async def function stops the asyncio loop.
Video
On left = with async
On right = without async
My.Movie.mp4
Potential fix
- Simply define the route as
def health_check():removing async, which tells FastAPI to run it in a separate thread automatically. - Might use aiosqlite library instead
I will be happy to work on this issue
Record
- I agree to follow this project's Code of Conduct
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels