python runtime_error ai_generated true

RuntimeWarning: coroutine 'fetch_data' was never awaited

ID: python/coroutine-never-awaited

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-02-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

A coroutine function was called without await or asyncio.create_task, so the coroutine object was created and garbage collected without running.

generic

中文

调用了一个协程函数但没有 await 或 asyncio.create_task,因此创建了协程对象并在未运行的情况下被垃圾回收。

Workarounds

  1. 95% success
    Await the coroutine in an async context:
    
    async def main():
        data = await fetch_data()
  2. 90% success
    If you intentionally want fire-and-forget, schedule it:
    
    asyncio.create_task(fetch_data())
  3. 88% success
    Enable -W error::RuntimeWarning in tests to catch this at CI time.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Hides the symptom; the async work silently never runs.

  2. 85% fail

    Raises 'asyncio.run() cannot be called from a running event loop'.

  3. 70% fail

    Breaks other callers and loses concurrency.