python runtime_error ai_generated true

RuntimeWarning: 协程 'foo' 从未被 await

RuntimeWarning: coroutine 'foo' was never awaited

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

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-01-30首次发现

版本兼容性

版本状态引入弃用备注
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 85% 失败

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