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
80%Fix Rate
86%Confidence
0Evidence
2025-05-27First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success
Use httpx.AsyncClient: `async with httpx.AsyncClient() as c: r = await c.get(url)`
-
90% success
Offload with `await asyncio.to_thread(blocking_fn, args)` or `run_in_executor`.
Dead Ends
Common approaches that don't work:
-
80% fail
Each worker still blocks its own loop under load.
-
90% fail
Does not yield for blocking I/O; loop still stalls.