python
runtime_error
ai_generated
true
RuntimeError: asyncio.run() cannot be called from a running event loop
ID: python/starlette-loop-already-running
80%Fix Rate
87%Confidence
0Evidence
2024-04-21First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 0.27+ | active | — | — | — |
Root Cause
A synchronous FastAPI/Starlette route or dependency called asyncio.run() while the server's event loop was already running.
generic中文
同步的 FastAPI/Starlette 路由或依赖项在服务器事件循环已运行时调用了 asyncio.run()。
Workarounds
-
95% success
@app.get('/x') async def handler(): return await some_coroutine() -
75% success
import anyio result = anyio.from_thread.run(some_coroutine)
Dead Ends
Common approaches that don't work:
-
70% fail
Patches the loop but breaks uvicorn's task scheduling and can deadlock under load.
-
85% fail
Raises 'This event loop is already running' since the loop cannot be re-entered.