python
runtime_error
ai_generated
true
RuntimeWarning: coroutine 'get_db' was never awaited
ID: python/fastapi-depends-async-generator-not-closed
80%Fix Rate
85%Confidence
0Evidence
2025-03-19First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
An async dependency (async def or async generator) is used as a plain function, or Depends is missing so the coroutine is returned unawaited.
generic中文
异步依赖(async def 或异步生成器)被当作普通函数使用,或漏写 Depends 导致返回未 await 的协程。
Workarounds
-
93% success
Declare the dependency with Depends: def route(db=Depends(get_db)): ...
-
88% success
Use an async generator dependency that yields and cleans up in finally.
Dead Ends
Common approaches that don't work:
-
60% fail
Blocks the event loop and may break async DB drivers.
-
80% fail
Returns a coroutine/generator you then misuse; resources leak.