PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

BUG: Sync-microservices: Health Check Blocks Main Event Loop (Performance Risk)

Open DeveloperAmrit opened this issue 10 months ago • 5 comments

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

  1. Simulate a slow database connection (e.g., lock the sqlite DB file).
  2. Send a request to GET /health
  3. Simultaneously try to hit GET /watcher/status
  4. 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

DeveloperAmrit avatar Jan 12 '26 09:01 DeveloperAmrit

👋 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 :)

github-actions[bot] avatar Jan 12 '26 09:01 github-actions[bot]

I would like to work on this issue

DeveloperAmrit avatar Jan 12 '26 09:01 DeveloperAmrit

Can you assign me this issue. I would like to work in it.

yummyPancake2607 avatar Jan 12 '26 18:01 yummyPancake2607

Hey @yummyPancake2607 I am already working on it

DeveloperAmrit avatar Jan 12 '26 19:01 DeveloperAmrit

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!

aakashkumarrr08-art avatar Nov 23 '26 00:11 aakashkumarrr08-art