# lore:python-async/asyncio-gather-detached-siblings/0002

**Summary:** asyncio.gather() with default return_exceptions=False propagates the first exception but does NOT cancel sibling tasks — they keep running detached, their results are discarded and their exceptions may never be retrieved; use TaskGroup for cancel-on-failure.
**Semver:** 2.0.0 | **Kind:** `gotcha` | **Status:** `verified`

## Verbatim Symptoms

- `Task exception was never retrieved`
- `Side effects from gathered tasks continue after gather already raised`
- `Background task completed after asyncio.gather raised an exception`
- `coroutine result lost after gather exception`

## Problem Statement
When one gathered task raises (return_exceptions=False), gather propagates that exception to the awaiter immediately — but per the Python docs, the other awaitables 'won't be cancelled and will continue to run.' They are now detached: their return values are discarded, their in-flight side effects (DB writes, file operations) continue unsupervised, and any exception they raise later is dropped, surfacing at best as 'Task exception was never retrieved' during GC. NOTE: v1 of this lesson claimed gather cancels siblings — that is a myth (it is TaskGroup's behavior); v1's own eval failed against reality and was rewritten.

## Correct Procedure
1. If all tasks should run to completion and every outcome be inspectable, use asyncio.gather(*coros, return_exceptions=True) and check results for Exception instances.
2. If you want cancel-on-first-failure semantics, use asyncio.TaskGroup (Python 3.11+): it cancels siblings on failure and raises an ExceptionGroup containing all errors.
3. If you must stay on gather and need cancellation, keep references to the tasks (asyncio.create_task), and in the except block explicitly cancel and await each remaining task.

## Anti-Patterns
- MYTH: assuming gather cancels remaining tasks when one fails — it does not; that is TaskGroup semantics.
- Catching gather's first exception and assuming the system is quiescent — siblings are still executing.
- Letting detached tasks' exceptions go unobserved — they surface only as 'Task exception was never retrieved' at GC, far from the cause.

## Documented Dead Ends
- **Approach:** Waiting for CancelledError inside siblings after gather raises
  - *Why it fails:* No cancellation is ever delivered by gather itself; the sibling runs to completion. (This false expectation was baked into v1 of this very lesson — its eval could not pass against real behavior.)
  - *Time wasted:* ~30 mins
- **Approach:** Adding done callbacks to recover sibling results after gather raised
  - *Why it fails:* Results are recoverable only if you kept task references before calling gather; coroutines passed directly are wrapped in tasks you never see.
  - *Time wasted:* ~20 mins

## Executable Verification Evals
- **Docker Image:** `python:3.12-slim`
- **Setup Network:** `none` | **Eval Network:** `none`
- **Fix Command:** `python test_fix.py`
- **Broken Command:** `python test_broken.py`
