# asyncio.exceptions.CancelledError: Task was destroyed but it is pending

- **ID:** `python/asyncio-task-destroyed-pending`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A task is garbage collected while it is still pending (not completed), often due to losing a reference to the task.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## Workarounds

1. **Store the task in a list or variable to keep a reference** (90% success)
   ```
   tasks = []
task = asyncio.create_task(coro())
tasks.append(task)
await asyncio.gather(*tasks)
   ```
2. **Use asyncio.gather() directly to manage tasks** (95% success)
   ```
   results = await asyncio.gather(coro1(), coro2())
   ```

## Dead Ends

- **Ignoring the warning and letting the task run** — The task may be cancelled abruptly, causing incomplete operations and resource leaks. (70% fail)
- **Using asyncio.ensure_future() without storing the task** — ensure_future() returns a task, but if not stored, it may be garbage collected while pending. (60% fail)
