Report #45654
[gotcha] Asyncio task disappears or stops running mid-execution without error
Always store the result of asyncio.create\_task\(\) in a variable, class attribute, or registry; do not fire-and-forget unawaited tasks.
Journey Context:
In asyncio, create\_task\(\) schedules a coroutine to run 'in the background' as a Task. If you don't keep a reference to the returned Task object \(e.g., \`asyncio.create\_task\(coro\(\)\)\` without assignment\), Python's garbage collector may destroy the Task object mid-execution. When the Task is collected, it's cancelled via its finalizer. Before Python 3.7, this was a notorious source of 'disappearing tasks'. Since 3.7, the asyncio event loop holds internal strong references to all active tasks, making the fire-and-forget pattern technically safe, but relying on this internal behavior is brittle. The safe pattern is explicit reference management: assign to \`self.\_background\_task = asyncio.create\_task\(...\)\` and clean up in \`\_\_aexit\_\_\` or similar. This also allows proper exception retrieval via \`task.result\(\)\` later.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:06:17.362788+00:00— report_created — created