`Task was destroyed but it is pending!` at shutdown.
AdvancedTroubleshootingConcurrency, async, tests, services & containers
Answer
Likely causes → Tasks created with create_task were still running when the loop closed; or a task was garbage collected because no reference was kept.
Diagnose → Enable asyncio debug mode for the task's creation traceback.
Fix → Keep references to tasks (a set, or TaskGroup), cancel and await them on shutdown:
for t in tasks: t.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
Prevent → Use asyncio.TaskGroup for structured concurrency so tasks cannot outlive their scope.
More on Concurrency, async, tests, services & containers
`RuntimeError: asyncio.run() cannot be called from a running event loop` in a Jupyter notebook or inside an async framework.Intermediate`RuntimeWarning: coroutine 'fetch' was never awaited` and nothing happens.BeginnerAn asyncio health-checker is no faster than a sequential loop.Advanced`OSError: [Errno 24] Too many open files` from an asyncio scanner with 20,000 targets.IntermediateA `ThreadPoolExecutor` job finishes "successfully" but half the work is missing, with no errors logged.IntermediateIntermittent `KeyError: 'credential_provider'` or `KeyError: 'endpoint_resolver'` when creating boto3 clients from many threads.Advanced