python
runtime_error
ai_generated
true
RuntimeWarning: 协程 'foo' 从未被 await
RuntimeWarning: coroutine 'foo' was never awaited
ID: python/asyncio-coroutine-was-never-awaited
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.
解决方案
-
98% 成功率
Add the missing await: async def main(): result = await foo() # was: foo() -
90% 成功率
If you only want to schedule it, wrap with asyncio.create_task(): asyncio.create_task(foo())
无效尝试
常见但无效的做法:
-
95% 失败
Hides the symptom while the coroutine body never executes, causing silent data loss and downstream bugs.
-
85% 失败
Raises RuntimeError: asyncio.run() cannot be called from a running event loop.