python runtime_error ai_generated true

INFO: 127.0.0.1:54321 - 'GET /sync-endpoint HTTP/1.1' 200 OK (took 5.02s, blocking loop detected)

ID: python/fastapi-event-loop-blocked

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-04-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

A synchronous blocking call (e.g. time.sleep, requests, heavy CPU) was executed directly inside an async def endpoint, blocking the event loop and stalling all concurrent requests.

generic

中文

在 async def 端点中直接执行了同步阻塞调用(如 time.sleep、requests、重 CPU 计算),阻塞了事件循环,导致所有并发请求停滞。

Workarounds

  1. 95% success
    Offload blocking work to a thread:
    
    import anyio
    @app.get('/sync-endpoint')
    async def handler():
        result = await anyio.to_thread.run_sync(blocking_fn)
  2. 92% success
    Define the route as def (not async def) so FastAPI runs it in a threadpool:
    
    @app.get('/sync-endpoint')
    def handler():
        return blocking_fn()
  3. 90% success
    Replace requests with httpx.AsyncClient and time.sleep with asyncio.sleep.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Each worker is still blocked; throughput per worker stays at one request.

  2. 90% fail

    Does not fix the blocking call inside the existing one.

  3. 95% fail

    Unrelated to loop blocking.