python system_error ai_generated true

WARNING: (tracemalloc) ... asyncio: Task was destroyed but it is pending! Blocking call detected in async def route, event loop stalled for 5.2s

ID: python/fastapi-async-route-blocking-event-loop

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-05-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100.x active — — —
0.110.x active — — —

Root Cause

An `async def` route calls a synchronous/blocking function (requests, time.sleep, heavy CPU) directly, freezing the event loop.

generic

中文

`async def` 路由直接调用了同步/阻塞函数(requests、time.sleep、重 CPU 操作),冻结了事件循环。

Workarounds

  1. 95% success
    Use httpx.AsyncClient: `async with httpx.AsyncClient() as c: r = await c.get(url)`
  2. 90% success
    Offload with `await asyncio.to_thread(blocking_fn, args)` or `run_in_executor`.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Each worker still blocks its own loop under load.

  2. 90% fail

    Does not yield for blocking I/O; loop still stalls.