In this article
A propagated child exception is not proof that sibling work stopped; TaskGroup adds a cancellation-and-wait boundary, while external effects still need reconciliation.
The parent fails while another branch keeps writing
A request starts concurrent branches: one fetches required data and another updates an external record. The first raises an exception. The parent reports failure and retries the whole operation, assuming every sibling also stopped. That assumption is wrong for default asyncio.gather behavior.
The asyncio task reference says that gather propagates the first exception while other awaitables continue running. A parent failure can therefore coexist with ongoing sibling effects. The service needs an explicit owner for that unfinished work.
Choose the intended group contract
TaskGroup provides a different boundary. When a child raises a non-cancellation exception, remaining group tasks are canceled and awaited. After they finish, non-cancellation failures are propagated as grouped exceptions, with documented special cases for process-control exceptions.
This is not a stylistic replacement for identical behavior. Decide whether branches belong to a common failure scope or should continue independently. Replacing gather with TaskGroup changes sibling lifetime and can cancel useful work that previously survived another branch’s failure.
Preserve cooperative cleanup
The reviewed TaskGroup implementation records errors and requests cancellation of unfinished children. Cancellation is a request delivered through the coroutine machinery, not arbitrary reversal of completed work or an operating-system kill of every dependent activity.
The task documentation recommends cleanup in finally blocks and generally propagating CancelledError afterward. Suppressing cancellation can interfere with structured-concurrency behavior. Review each branch’s cleanup contract instead of assuming the group boundary repairs a coroutine that ignores its own cancellation.
Account for gather after exception propagation
If gather remains the right choice, retain references and explicitly own continued branches. The reference warns that canceling a gather already marked done after propagating an exception does not cancel its submitted awaitables.
A catch block that calls cancel on that completed gather can therefore look defensive while leaving the original work alive. Use the supported task lifecycle and reconcile actual task outcomes rather than declaring cleanup from a method call alone.
Separate local completion from external effects
A TaskGroup that has finished waiting establishes a local child-lifetime boundary. It does not undo a remote write completed before cancellation or prove that a separately delegated external operation stopped. Preserve those receipts and stable operation identities before retrying the parent.
Draining accepted work explains why stopping admission and ending work differ. Duplicate-safe outcomes explains why a canceled branch can still leave an application effect that must be reconciled.
Accept a deliberate failure boundary
In an isolated fixture, compare a failing branch with a sibling whose cleanup records its completion. Observe default gather and TaskGroup separately, including attempted cancellation after gather propagated an exception. Acceptance records which branches continued, which acknowledged cancellation and which external outcomes remain uncertain. Choose the primitive by the intended ownership contract, rather than treating a caught exception as evidence that all related work disappeared.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.