Report #92458
[gotcha] asyncio.create\_task background coroutine disappears mid-execution without error
Maintain a strong reference to the Task object returned by create\_task\(\); store it in an instance set and use task.add\_done\_callback\(\) to remove it upon completion. Do not rely on asyncio.shield\(\) or infinite loops to keep the task alive.
Journey Context:
The event loop holds only a weak reference to tasks. When the Python garbage collector runs, if no user code references the Task object, it is collected even while the coroutine is suspended on an await. The symptom is silent disappearance—no exception, no log, no callback. Common wrong fixes include using shield \(which doesn't hold a strong reference\) or creating daemon threads. The architectural pattern is to maintain a 'fire-and-forget' registry: \`self.\_bg\_tasks.add\(task\); task.add\_done\_callback\(self.\_bg\_tasks.discard\)\`, ensuring the task survives until completion.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:46:52.564345+00:00— report_created — created