liveBook · Manning

Coroutine hierarchy

As explained in Kotlin in Action, 2E

A coroutine hierarchy is a parent-child organization of coroutines through which execution lifetimes and failures can be related.

A coroutine hierarchy organizes coroutines as parents and children, connecting their execution lifetimes, cancellation, and failures. Structured concurrency manages this hierarchy automatically, preventing forgotten or unnecessarily long-running coroutines. section 15.0

A failed child cancels its sibling

runBlocking { launch { try { while (true) { println("Heartbeat!") delay(500.milliseconds) } } catch (e: Exception) { println("Heartbeat terminated: $e") throw e } } launch { delay(1.seconds) throw UnsupportedOperationException("Ow!") } } The second child throws after one second. Its parent enters cancellation, cancels the heartbeat sibling, and propagates the exception upward. The heartbeat reports a JobCancellationException before the original exception terminates the program. section 18.2.1

The hierarchy provides predictable relationships between concurrent operations:

  • Builds parent-child relationships through Job objects associated with coroutine contexts. section 15.3
  • Propagates an uncaught child exception to its parent, which cancels the other children and propagates the failure further. section 18.2.1
  • Avoids unnecessary work and releases resources when sibling computations can no longer produce a useful common result. section 18.2.1
  • Inherits coroutine context along the hierarchy and supports cancellation through suspension points and cancellation functions. section 15.3
Figure 18.1 When child coroutines fail with an uncaught exception, they notify their parent. The parent, in turn, cancels all the sibling coroutines, and propagates the exception further up the coroutine hierarchy.
CH18_F01_Isakova

Trace the failure path from a child coroutine to its parent, across sibling cancellation, and onward through the coroutine hierarchy. section 18.2.1

Use coroutineScope for concurrent decomposition that waits for child results, and use CoroutineScope to associate coroutines with a class lifecycle, typically with SupervisorJob. Avoid GlobalScope in application code because it breaks structured concurrency. section 15.3

How Kotlin in Action, 2E covers it

How deep it goes
Explains Job-based relationships, context inheritance, cancellation, and failure propagation, with a runnable sibling-cancellation example.
Examples
Demonstrates a heartbeat coroutine cancelled when a sibling throws an uncaught exception.
What it assumes
Assumes Kotlin coroutine usage and coroutine contexts while teaching hierarchy relationships through Job objects and structured concurrency.
What you can do after
Operate/configure coroutine scopes and diagnose cancellation and exception propagation across parent-child relationships.
How it connects
Connects structured concurrency, coroutine context, Job, cancellation, coroutineScope, CoroutineScope, and SupervisorJob.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.