Report #30227
[gotcha] asyncio task disappears mid-execution without error or cancellation traceback
Always store the Task object returned by create\_task\(\) in a strong reference \(e.g., a set or instance variable\) for the duration of execution; use task.add\_done\_callback\(\) to clean up references
Journey Context:
The asyncio event loop holds only a weak reference to tasks to prevent memory leaks. If you call create\_task\(\) without assigning the result \(fire-and-forget\) or the assigned variable goes out of scope, the garbage collector can finalize the pending Task, logging 'Task was destroyed but it is pending\!' and silently cancelling the coroutine without executing finally blocks. This explains why fire-and-forget background tasks mysteriously stop mid-flight. The fix ensures the event loop's internal registry can reach the task until completion.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:07:16.577408+00:00— report_created — created