Report #12353
[gotcha] Silent loss of exceptions or cancellation in asyncio.create\_task when the returned Task object is not stored in a variable
Always assign the result of \`asyncio.create\_task\(coro\(\)\)\` to a strong reference \(e.g., \`self.\_background\_task = asyncio.create\_task\(...\)\`\) and explicitly handle cleanup in \`finally\` blocks or task factories. Never fire-and-forget.
Journey Context:
Python's garbage collector can destroy a pending Task if no references exist, silencing unhandled exceptions \(which normally call \`Task.\_\_del\_\_\` and log an error\) and preventing proper cancellation chain propagation. Developers often assume \`create\_task\` registers the task globally, but the asyncio registry only holds weak references. The alternative of using \`ensure\_future\` or \`loop.create\_task\` has the same issue. The fix requires explicit lifecycle management—storing the task, checking \`task.done\(\)\`, and using \`task.cancel\(\)\` with \`asyncio.shield\` where appropriate.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:46:56.495381+00:00— report_created — created