BUG: Sync-microservices: Health Check Blocks Main Event Loop (Performance Risk)
Is there an existing issue for this?
- [x] 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
https://github.com/user-attachments/assets/eccb75a5-f8aa-4a93-8ec0-325ead7aaac2
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
- [x] I agree to follow this project's Code of Conduct
👋 Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)
I would like to work on this issue
Can you assign me this issue. I would like to work in it.
Hey @yummyPancake2607 I am already working on it
Hi @DeveloperAmrit @rohan-pandeyy,
I've implemented a fix for this issue and submitted PR #1767.
The fix changes the health-check endpoint from async def to a synchronous def, allowing FastAPI to execute the blocking SQLite operation in its threadpool.
I've also added regression tests, including a concurrency test to verify that the event loop remains responsive during slow database operations.
Validation:
- 6 sync-microservice tests passed.
- 1,636 backend tests passed.
- Black and Ruff checks passed.
I noticed the earlier discussion about this issue being worked on. Could you please review PR #1767 and let me know if any changes are needed?
Thank you!