Report #15192
[gotcha] Silent disappearance of asyncio tasks due to garbage collection when strong reference is not kept
Always assign \`asyncio.create\_task\(\)\` result to a variable or register it in a strong-reference collection; use \`weakref\` tracking only if you explicitly handle resurrection. Never rely on local variables in fire-and-forget helper functions without persistent storage \(instance variable, global set, or \`asyncio.all\_tasks\(\)\` tracking\).
Journey Context:
Developers assume \`create\_task\(\)\` schedules the coroutine permanently like old \`ensure\_future\(\)\`. However, Python's garbage collector can collect the Task object if no references exist, stopping the coroutine mid-execution without error or traceback. Storing in a local variable that goes out of scope \(fire-and-forget pattern\) also fails; you need persistent storage. Alternatives like \`ensure\_future\(\)\` have the same issue. The "background task" pattern requires explicit strong reference management until the task completes or is explicitly awaited/cancelled.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:22:37.115729+00:00— report_created — created