python
runtime_error
ai_generated
true
Task exception was never retrieved future: <Task finished name='Task-5' coro=<fetch() done, defined at app.py:42> exception=ValueError('bad payload')>
ID: python/asyncio-task-exception-was-never-retrieved
80%Fix Rate
88%Confidence
0Evidence
2024-07-19First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
A task created with asyncio.create_task() raised an exception, but nobody awaited the task or called task.exception(), so the exception is logged as a warning at garbage collection.
generic中文
使用 asyncio.create_task() 创建的任务抛出异常,但无人 await 该任务或调用 task.exception(),导致异常在垃圾回收时以警告形式记录。
Workarounds
-
90% success
tasks = [asyncio.create_task(fetch(u)) for u in urls] results = await asyncio.gather(*tasks, return_exceptions=True) for r in results: if isinstance(r, Exception): logging.exception('task failed', exc_info=r) -
87% success
def _on_done(t: asyncio.Task): if not t.cancelled() and t.exception(): logging.error('task error', exc_info=t.exception()) t = asyncio.create_task(fetch(u)) t.add_done_callback(_on_done)
Dead Ends
Common approaches that don't work:
-
85% fail
Hides the symptom but the task still fails silently and the error is lost.
-
80% fail
Swallows real errors and produces no diagnostic; debugging becomes impossible.