python runtime_error ai_generated true

RuntimeWarning: coroutine 'foo' was never awaited

ID: python/asyncio-coroutine-was-never-awaited

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-01-30First 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 but the returned coroutine object was never awaited or scheduled, usually by forgetting await or passing it to a non-async API.

generic

中文

调用了协程函数但返回的协程对象从未被 await 或调度,通常是因为忘记 await 或将其传给了非异步 API。

Workarounds

  1. 98% success
    Add the missing await:
    
    async def main():
        result = await foo()  # was: foo()
  2. 90% success
    If you only want to schedule it, wrap with asyncio.create_task():
    
    asyncio.create_task(foo())

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Hides the symptom while the coroutine body never executes, causing silent data loss and downstream bugs.

  2. 85% fail

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