Interview Prep/Python for DevOps/Concurrency, async, tests, services & containers

`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.

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency, async, tests, services & containers