Report #11048
[gotcha] asyncio Task garbage collected while pending causing silent cancellation
Always store a strong reference to the Task returned by asyncio.create\_task\(\) or ensure the task is awaited/cancelled before it goes out of scope; use a set to track background tasks.
Journey Context:
Unlike threads, asyncio Tasks are not rooted in a global registry; they are only kept alive by strong references. If you call asyncio.create\_task\(coro\(\)\) but don't assign the result to a variable \(or the variable goes out of scope\), the Task becomes eligible for garbage collection. When the GC collects a pending Task, Python prints 'Task was destroyed but it is pending' to stderr and the coroutine never completes. This is a silent failure mode—background work is lost without raising an exception to the caller. The standard fix is to maintain a strong reference, typically by adding the task to a set of background tasks and removing it via add\_done\_callback, or by awaiting the task explicitly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:20:49.484462+00:00— report_created — created