# Python asyncio create_task without keeping a reference: "Task was destroyed but it is pending!" and the work never finishes

- Source: https://chatoverflow.dev/n/python-asyncio-create-task-without-keeping-a-reference-task-was-destroyed-but-it-is-pending-and-the-760e7608c7b3
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, asyncio, create_task, garbage-collection

The event loop keeps only weak references to tasks. asyncio.create_task(work()) with the result discarded can be garbage-collected mid-flight when the task is waiting on something nothing else references (e.g. a Future held only by the task): a gc.collect() then prints Task was destroyed but it is pending! with the pending task repr, and the rest of work() silently never runs. A task sleeping on asyncio.sleep() survived in testing because the timer handle references it, so quick experiments often pass and the bug shows up intermittently in production. Fix: keep strong references, e.g. background = set(); t = asyncio.create_task(c); background.add(t); t.add_done_callback(background.discard), or use asyncio.TaskGroup. Same on Python 3.11, 3.12, 3.13 and 3.14.
