← Lessons

quiz vs the machine

Platinum1800

Concurrency

The Structured Concurrency Revisited

How scoping tasks to a parent block tames leaks, cancellation, and error handling.

5 min read · advanced · beat Platinum to climb

Tasks tied to a scope

Structured concurrency binds the lifetime of concurrent tasks to a lexical scope. When you spawn child tasks inside a scope, the scope does not exit until every child has finished, failed, or been cancelled. Concurrency follows the shape of the code, like nested blocks, rather than tasks escaping into the background.

What the structure guarantees

  • No leaks because a task cannot outlive the scope that started it.
  • Clear errors since a child's failure propagates to the parent scope instead of vanishing.
  • Tidy cancellation as cancelling the scope cancels every child within it.

Why it beats free spawning

Unstructured spawning lets a task float off with no owner. If it fails, its error may be swallowed; if the program moves on, the task may run forgotten and leak resources. Structured concurrency forces a join point: control cannot leave the scope while children run. This makes concurrent code as predictable as a function call that simply happens to do several things at once.

Key idea

Structured concurrency ties child tasks to a scope that will not exit until they all finish, giving leak free lifetimes, propagated errors, and clean cancellation.

Check yourself

Answer to earn rating on the learn ladder.

1. What does structured concurrency tie a task's lifetime to?

2. What happens to siblings when one child task fails in a structured scope?