If three AI API jobs have already been accepted and you poll them inside one asyncio.TaskGroup, one broken poller can stop the other two observers. That is not a receipt saying the remote jobs were cancelled.
Keep the accepted job IDs outside the polling coroutines, and track local observer state separately from remote job state. This matters for asynchronous generation or batch workflows where submission returns an identifier and results are retrieved later. It is not a claim about every AI API or SDK.
What TaskGroup actually guarantees
The Python 3.14 Task groups documentation describes the failure boundary:
- The first child failure with an exception other than
asyncio.CancelledErrorcancels the remaining tasks in the group. - The group waits for all its tasks to finish.
- Non-cancellation failures are then raised in an
ExceptionGrouporBaseExceptionGroup, as appropriate.KeyboardInterruptandSystemExithave special re-raise rules.
TaskGroup was introduced in Python 3.11; the behavior discussed here is grounded in the 3.14 documentation.
Cancellation is cooperative, not an instant kill. The task cancellation section says CancelledError is raised at the next opportunity, recommends try/finally for cleanup, and says explicitly caught cancellation should generally be propagated after cleanup. Swallowing it can disrupt structured concurrency.
A failed observer is not a failed generation
Consider this hypothetical sequence—not a measured run:
- Jobs A, B, and C have been accepted; their identifiers are recorded.
- Three coroutines poll those identifiers in one group.
- Poller A raises an unhandled response-parsing exception.
- TaskGroup requests cancellation of pollers B and C.
The justified conclusion is that local observation was interrupted. Neither “B and C were cancelled remotely” nor “B and C are definitely still running” follows from Python’s rule. Application cleanup, SDK behavior, and the service’s own protocol determine any remote effect. Python’s documentation does not establish those contracts.
In particular, do not turn a sibling’s CancelledError into a remote cancelled status in your job table.
Give recovery something durable to inspect
My suggested design is to separate these records:
| Record | Meaning |
|---|---|
| Accepted job ID | The existing remote work to look up, not a reason to submit again |
| Last confirmed remote status | What the service last reported; possibly stale |
| Observer outcome | Polling finished, failed, or was cancelled locally |
| Remote cancellation outcome | Separately confirmed result, if the service supports cancellation |
Persist accepted identifiers before launching this polling group if recovery must survive a process restart. Keep them even when a poller fails. After group shutdown, let a recovery owner inspect unresolved IDs through the service’s documented lookup mechanism before deciding whether any resubmission is appropriate. If remote cancellation is the intended policy, treat it as a separate operation whose outcome needs confirmation—not as an implication of the local exception.
This design assumes you already have the accepted IDs. A submission interrupted before its ID is recorded is a separate reconciliation problem, not something TaskGroup fixes.
One group is appropriate when you intentionally want these observers to stop together. If a parsing failure for A should leave B and C polling, choose an explicit independent-observer failure policy instead of hiding CancelledError to defeat the group’s behavior.
Before connecting a real service, use a fake job service to inject A’s parsing failure. Check that B and C’s local cleanup runs, cancellation propagates, their IDs remain recoverable, and no remote terminal status is invented. Then check that recovery looks up those same IDs rather than issuing new submissions. These are proposed acceptance checks, not reported test results.
The boundary to preserve is simple: structured concurrency manages your local tasks; remote job lifecycle needs its own evidence.