← Lessons

quiz vs the machine

Platinum1850

Concurrency

Structured Concurrency Scopes

Tying the lifetime of concurrent tasks to a scope so none outlive or leak past their parent.

6 min read · advanced · beat Platinum to climb

Concurrency with a shape

In unstructured concurrency you can launch a background task and forget about it. It may outlive its caller, leak, or fail silently. Structured concurrency fixes this by binding tasks to a lexical scope. The scope does not finish until every child task it started has finished.

The scope contract

A structured scope enforces strong guarantees.

  • No leaks because the scope waits for all children before returning.
  • Error propagation because a child failure surfaces to the parent rather than vanishing.
  • Cancellation because if the scope is cancelled, all children are cancelled too.

This mirrors how a function call returns only after its body completes, extending that discipline to concurrent children.

Why it tames complexity

Because tasks cannot escape their scope, you can reason locally. When a block returns, you know nothing it spawned is still running. Errors cannot be dropped, since an unhandled child error fails the whole scope. The result is concurrency that nests cleanly, just like ordinary control flow.

Key idea

Structured concurrency binds task lifetimes to a scope that waits for every child, so tasks never leak, errors propagate to the parent, and cancellation cascades, making concurrency nest like normal control flow.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a structured concurrency scope guarantee about child tasks?

2. How are child task errors handled in structured concurrency?

3. What happens to children when their scope is cancelled?