Structured concurrency
Every task has a parent that waits for it. That one rule eliminates leaks, orphans, and most cancellation bugs.
Unstructured concurrency is the default nearly everywhere: you start a task, get a handle, and the task's lifetime is now unrelated to anything.
That produces the same class of problem goto produced for control flow, and the answer has the same shape.
the problems#
Orphans. A function starts a task and returns. The task is still running. It outlives the scope that created it, holding references, writing to things nobody is watching.
Silent failures. A task throws. Nobody awaits it. The exception vanishes — or, if you are lucky, surfaces as "unhandled rejection" in a log nobody reads, detached from the context that would explain it.
Leaked work. A request times out and returns an error. The three tasks it started keep running, consuming a connection each, producing results nobody will read. Under load this is most of your capacity.
Cancellation that does not propagate. Cancelling a parent does not cancel what it started, so cancellation is advisory and cleanup is manual.
the rule#
A task cannot outlive the scope that started it.
async with asyncio.TaskGroup() as tg:
a = tg.create_task(fetch_user(uid))
b = tg.create_task(fetch_orders(uid))
# control does not pass this line until both finishThe block does not exit until every child completes. If one raises, the others are cancelled and the exception propagates out of the block. If the enclosing scope is cancelled, the children are cancelled.
That single constraint eliminates all four problems, because none of them can be expressed any more.
It is available now in most ecosystems: TaskGroup in Python, structured scopes in Java, errgroup and context in Go, nurseries in Trio, scoped tasks in Swift and Kotlin.
why it feels restrictive and is not#
The objection is always the same: sometimes I want a task that outlives its caller.
Usually you do not — you want it to outlive this function, which is different. Move the scope up to whatever level genuinely owns the work's lifetime. A background task belonging to a service should be scoped to the service's lifetime, not started in a request handler and abandoned.
When you genuinely need a detached task — a fire-and-forget metric, a long-running supervisor — make it explicit and give it an owner:
class Service:
async def start(self):
self._tg = asyncio.TaskGroup()
await self._tg.__aenter__()
self._tg.create_task(self._reconcile_loop())
async def stop(self):
await self._tg.__aexit__(None, None, None) # cancels and waitsNow the task has a scope, an owner, and a shutdown path. The difference from fire-and-forget is that somebody is responsible for it.
the Go version#
Go's goroutines are unstructured by default, which is the language's biggest concurrency footgun — go f() returns nothing, and nothing waits.
The convention that recovers most of the benefit:
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return fetchUser(ctx, uid) })
g.Go(func() error { return fetchOrders(ctx, uid) })
if err := g.Wait(); err != nil {
return err // first error cancels ctx; all children observe it
}errgroup plus context gives you the scope, error propagation and cancellation. It is a convention rather than a language rule, so it has to be applied consistently — but applied consistently it is nearly as good.
the cancellation part#
Structured concurrency is what makes cancellation actually work, and cancellation is what makes timeouts actually work.
Without it, a timeout sets an error on the caller and the work continues. Cancelling the scope cancels every descendant, so a request that times out releases its connections, stops its queries, and abandons its downstream calls.
That property is what turns a slow dependency from an outage into a degradation, which is why this is a reliability topic rather than a style preference.
the adoption path#
You do not need to rewrite anything. Apply it at the boundaries first:
- Request handlers. Everything a request starts finishes or is cancelled before the response is sent.
- Background jobs. Everything a job starts is scoped to the job.
- Service lifecycle. Everything a service starts is scoped to the service and stops on shutdown.
Three places, and they cover the overwhelming majority of leaked work — including the graceful shutdown that most services claim to do and do not.
— Dom, October 26, 2026