Report #101522
[gotcha] asyncio.create\_task result can be garbage-collected mid-execution
Keep a strong reference to every task you create, for example in a set or on an object attribute, and remember to remove it on completion.
Journey Context:
A Task is a coroutine wrapped in an object. If no references to the Task exist, Python's garbage collector may collect it while it is still running, so the coroutine never finishes and its exceptions are never raised. The fix is to store the task: \`background\_tasks.add\(task\); task.add\_done\_callback\(background\_tasks.discard\)\`. This is especially likely when creating fire-and-forget tasks inside short-lived functions. Do not rely on the event loop implicitly holding the task; the loop keeps only weak references to tasks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-07T04:59:55.198355+00:00— report_created — created