Python asyncio: Task was destroyed but it is pending
Task was destroyed but it is pending!Short answer
A task was still unfinished when Python threw it away. Either nothing held a reference to it, so it was garbage collected mid-run, or the program reached the end of the loop without awaiting it. Keep a strong reference, and await every task with TaskGroup.
Fix it fast
1Find out which cause you have
Run the program in asyncio debug mode. It shows the traceback where each unfinished task was created.
PYTHONASYNCIODEBUG=1 python -X dev main.py2Match what you see, then jump to its fix
- 1
asyncio.create_task(job()) is called and the result is not stored anywhereA fire-and-forget task has no referenceFix - 2
The warning prints once, at shutdown, for tasks that were created but never awaitedmain() returns while tasks are still runningFix - 3
One job failed, the program moved on or ended, and the others are reported as pendinggather() raised and left sibling tasks runningFix - 4
RuntimeWarning: coroutine '...' was never awaitedA coroutine was called but never scheduledFix
- 1
What does the error look like?
Task was destroyed but it is pending!RuntimeWarning: coroutine '...' was never awaitedTask exception was never retrieved
Cause 1 of 4
A fire-and-forget task has no reference
1Check
The event loop keeps only weak references to tasks, so a task nothing else refers to can be garbage collected before it is done.
grep -rn "create_task(" . --include=*.py2Fix
Keep each task in a set, and let it remove itself when finished.
background = set()
def spawn(coro):
task = asyncio.create_task(coro)
background.add(task)
task.add_done_callback(background.discard)
return task3Verify
The background job now runs to the end and the warning stops.
PYTHONASYNCIODEBUG=1 python main.pyConfirmed by docs.python.org
Cause 2 of 4
main() returns while tasks are still running
1Check
Tasks started with a bare create_task() are never awaited by anything. A task nobody awaits also loses its exceptions, which asyncio reports later as Task exception was never retrieved.
PYTHONASYNCIODEBUG=1 python main.py2Fix
Create the tasks inside an asyncio.TaskGroup. All tasks are awaited when the block exits, and their failures come back as an ExceptionGroup.
async def main():
async with asyncio.TaskGroup() as tg:
for i in range(10):
tg.create_task(work(i))3Verify
The program ends only after all ten jobs finish, and the warning is gone.
python main.pyConfirmed by docs.python.org
Cause 3 of 4
gather() raised and left sibling tasks running
1Check
With return_exceptions left at False, gather() passes the first exception to the caller but does not cancel the other awaitables, which keep running.
grep -rn "gather(" . --include=*.py2Fix
Use a TaskGroup, which cancels the remaining tasks when one fails, or pass return_exceptions=True and handle each result.
results = await asyncio.gather(*jobs, return_exceptions=True)
for r in results:
if isinstance(r, Exception):
print("job failed:", r)3Verify
Make one job raise on purpose; the others finish or are cancelled and nothing is reported as destroyed.
python main.pyConfirmed by docs.python.org
Cause 4 of 4
A coroutine was called but never scheduled
1Check
Calling an async function without await or create_task does not run it.
python -X dev main.py2Fix
Add await in front of the call, or wrap it in asyncio.create_task() and keep the reference as above.
async def main():
await test()3Verify
The warning no longer prints.
python -X dev main.pyConfirmed by docs.python.org
How do I stop it coming back?
- Treat every create_task() result as something you must hold and await.
- Prefer TaskGroup over bare create_task and gather in new code.
- Run tests with python -X dev so unawaited coroutines show up early.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Celery in a Django project does not connect to Redis or ignores the broker URL
kombu.exceptions.InconsistencyError: ... _kombu.binding.celery
Celery talks to Redis through a client library and reads its broker URL from your Django settings.
4 causes, each with a fixBack end
Django form POST returns 403 Forbidden: CSRF verification failed
CSRF verification failed. Request aborted.
Django rejects an unsafe request when the CSRF cookie or the form token is missing or wrong, or when the Origin or Referer check fails.
5 causes, each with a fixBack end
Django cannot import corsheaders (django-cors-headers)
ImportError: No module named corsheaders
The pip package is django-cors-headers, but the module Django imports is corsheaders.
3 causes, each with a fixBack end
Django raises NoReverseMatch when building a URL
NoReverseMatch
NoReverseMatch means Django looked for a named URL pattern that fits the name and arguments you gave and found none.
4 causes, each with a fixBack end
npm install fails inside a node:alpine Docker build
Docker Node Alpine Image Build Fails on node-gyp
The node alpine images ship almost nothing beyond Node.js, so a native module that node-gyp must compile has no Python, make or C++ compiler.
2 causes, each with a fix