python runtime_error ai_generated true

RuntimeWarning: coroutine 'get_db' was never awaited

ID: python/fastapi-depends-async-generator-not-closed

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-03-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 93% success
    Declare the dependency with Depends:
    def route(db=Depends(get_db)): ...
  2. 88% success
    Use an async generator dependency that yields and cleans up in finally.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Blocks the event loop and may break async DB drivers.

  2. 80% fail

    Returns a coroutine/generator you then misuse; resources leak.