An agent starts two tool calls concurrently. One fails. The coordinator catches the exception, marks the batch failed and returns an error to the user. The other call is still running and writes a change after that response.
That sequence is permitted by Python’s default asyncio.gather behaviour. The exception tells the caller that one operation failed. It does not establish that every operation stopped.
For a batch whose remaining work should stop after a child fails, I would use a task group and test the cancellation path explicitly. I would also keep the batch’s failure status separate from the recorded outcome of each write. Even correctly cancelled local tasks can leave effects that need reconciliation.
The example below runs entirely in memory on Python 3.11. It calls no models or external tools. Its effect list stands in for writes so the scheduling behaviour is visible without touching a real service. This is a synthetic failure demonstration, not a reported production incident.
What gather promises when a child raises
The Python 3.11 documentation for asyncio.gather states that, with the default return_exceptions=False, the first raised exception propagates immediately to the awaiting task. Other submitted awaitables continue running; gather does not cancel them.
This wrapper therefore leaves an operational question unanswered:
try:
await asyncio.gather(update_catalogue(), notify_indexer())
except RuntimeError:
mark_batch_failed()
If notify_indexer() raises, update_catalogue() may still be active. A recovery worker that sees only failed could start a second update while the first is unfinished. An operator could also begin compensating for changes while the original batch is still making them.
Calling batch.cancel() in the exception handler is too late when batch is the completed gather future. The same documentation explicitly warns that cancelling gather after it has propagated an exception does not cancel the submitted awaitables. Cancelling a still-pending gather is a different case and does propagate cancellation to unfinished children.
Nor does return_exceptions=True implement stop-on-failure. It collects exceptions alongside successful results. That can be appropriate for independent reads where you want every result, provided you inspect each one. It is a poor substitute for a declared cancellation policy around related writes.
Run a failure with controlled ordering
Download the standard-library fixture, inspect it, then use Python 3.11 or later:
python3 agent-taskgroup.py
python3 agent-taskgroup.py --test
python3 -O agent-taskgroup.py --test
The fixture uses asyncio.Event objects to control the sequence. It does not depend on a guessed delay such as “the write probably takes longer than the failure”. The writer signals that it has started, then waits at a release event. Only after seeing that signal does the second task raise RuntimeError.
In the gather case, the exception handler checks whether the writer is still pending and attempts a late cancellation. It then releases the writer and awaits it, so the demonstration finishes with no abandoned task. The relevant output is:
{
"gather": {
"pending_after_error": true,
"late_cancel_accepted": false,
"effects": ["write"]
}
}
The write occurs after the handler has observed the failure. Holding the event loop open is deliberate. A tiny script that immediately exits after the exception can obscure this behaviour through shutdown cleanup; a long-lived service continues processing work.
The fixture passed six tests on Python 3.11.15, both normally and with Python optimisation enabled. They cover a running sibling after gather failure, ineffective late cancellation, a cooperatively cancelled task-group child, an effect completed before failure, swallowed cancellation and a detached task outside the group. These are runtime tests of Python coroutines, not evidence about a particular HTTP client’s cancellation behaviour.
Put related local tasks inside one owner
asyncio.TaskGroup, introduced in Python 3.11, provides the behaviour this batch needs. When a child fails with an exception other than CancelledError, the group cancels its remaining tasks and waits for them. It then raises the collected failures as an exception group, with special handling for KeyboardInterrupt and SystemExit.
A batch owner can use this shape:
async with asyncio.TaskGroup() as group:
group.create_task(update_catalogue())
group.create_task(notify_indexer())
That fragment illustrates ownership only. Your application still needs to handle the raised exception group and record individual operation outcomes. An ordinary child cancellation is not the same trigger as a child raising RuntimeError; do not design a failure signal around an assumed equivalence.
In the fixture’s task-group case, the writer waits at an event that is never released. The failing child triggers cancellation. The writer observes CancelledError, performs cleanup and re-raises. The group waits for that cleanup before the surrounding handler continues:
{
"effects": [],
"events": ["cancel_seen", "cleanup"],
"errors": ["tool failed"],
"child_done": true,
"child_cancelled": true
}
This outcome depends on where the writer was stopped. It had not appended its effect. It also cooperated with cancellation. The test deliberately establishes both conditions rather than generalising from one fortunate scheduling order.
Test the cases where cancellation cannot protect you
The task cancellation documentation recommends cleanup in try/finally and generally propagating CancelledError after cleanup. Task groups use cancellation internally; swallowing that exception can interfere with their behaviour.
The fixture includes a deliberately bad writer that catches cancellation, appends write_after_cancel and returns. Its task is done, but child_cancelled is false and the effect exists. A blanket “catch everything and keep trying” recovery wrapper can defeat the behaviour the batch owner expects. Review retry decorators and tool adapters as well as the coordinator.
A second case appends write before signalling readiness. The sibling then fails and the group cancels the writer at its next wait. The earlier effect remains in the list. No cancellation API removes it.
The final case creates a writer with asyncio.create_task outside the group and keeps a reference to it. A task inside the group fails, the group exits, and the detached writer is still pending. The fixture releases and awaits it explicitly. A tool adapter that launches background work this way has moved that work outside the group’s ownership. Putting the adapter call inside a task group does not automatically put every task it creates there too.
These cases set useful review questions. Does the adapter propagate cancellation? Does it start detached work? Could it have submitted an effect before its next cancellable wait? Test those paths before labelling a batch stopped.
A cancelled coroutine leaves remote outcomes unresolved
An HTTP request can cross the network before its awaiting Python task receives cancellation. The task group’s exit establishes the state of its local child tasks. It cannot, by itself, establish whether the receiving service accepted a write or whether a separate queue worker will perform it later.
The in-memory fixture does not test that network boundary. For a real adapter, record an operation identity before dispatch and retain the service’s request or job identity when one is returned. If cancellation interrupts the response, keep the effect outcome unknown until a status read or destination check resolves it. Do not translate CancelledError into “nothing changed”.
The earlier article on timed-out MCP writes addresses that reconciliation problem. Stopping queued MCP work requires a service-side stop or admission rule as well. A task group improves local ownership without replacing those controls.
In an application run record, I would keep separate fields for the batch failure, the local task’s terminal state and the effect outcome. For example, a batch can be failed, its local task cancelled, and its remote effect still unknown. That is enough information for a recovery worker to pause replay and investigate. A single failed flag loses the distinction.
Before adopting the change, run one disposable adapter test with a receiving service you control. Have the service acknowledge receipt separately from committing the effect. Cancel the client at that boundary, inspect the service’s final state and confirm that the recovery path does not resend blindly. Repeat with cancellation before dispatch. Keep both observations in the adapter’s tests; the local coroutine fixture cannot answer them for you.
For the broader approach to production tool permissions and review, see my Claude Code book.