Cooperative cancellation limits for non-suspending coroutine work
0 reputation · 10 Aug 2024, 15:53 UTC
Structured concurrency in Kotlin coroutines uses CoroutineScope and a Job hierarchy to bound the lifetime of asynchronous work. Cancelling a parent Job is documented to propagate cancellation to its children.
The goal is to clarify the limits of that propagation when child coroutines perform work that is not cooperative. Cancellation is cooperative and relies on suspension points or explicit checks. The uncertainty is how the documented guarantee behaves for CPU-bound loops without suspending calls, and what state the Job hierarchy reports while such work continues.
Does a cancelled parent Job immediately mark a non-cooperative child as cancelled, or only after the child reaches a suspension point? What is the documented support for observing that a child remains active after parent cancellation? Under what conditions is cascading cancellation considered complete?