What this thread does
Do first: choose coroutineScope for one all-or-nothing result or create the owning scope with SupervisorJob(parent) for independent sibling tasks.
Short answer
As of November 2026, this card targets kotlinx.coroutines 1.11.0 and current API docs. Supervision changes failure propagation, not ownership: parent cancellation still cancels supervised children. Observe launch failures with a handler and async failures through await; preserve cancellation and join cleanup before releasing shared resources. (supervision contract).
Choose / don't choose
coroutineScope: a child failure cancels the other children; choose when every child is required for the result; don't isolate failures that must invalidate the whole operation (contract).supervisorScope: choose for a bounded group of independent children; don't assume an exception thrown by the scope body leaves its children running (guide).- Owned supervisor scope: choose for an entity's independent ongoing jobs; don't detach it from the lifecycle accidentally by omitting the intended parent (SupervisorJob).
Not for: Kotlin build-tool alignment; see gradle-8-to-9-8-2-upgrade-jdk-kotlin-dsl-configuration-cache.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
SupervisorJob(parent) | Pass the lifecycle owner's Job; default parent is null; child failure does not cancel siblings, parent cancellation still propagates | Linked coroutines release; current API | supervisor |
CoroutineExceptionHandler | Put in the owned supervisor scope context or on its direct launch children; supervisorScope does not react to child exceptions | Linked coroutines release; current API | handler |
Deferred.await() | Consume every async result and handle failure there; exception handlers do not consume deferred failures | Current coroutine guide | guide |
cancelAndJoin() | Cancel, then suspend until completion; successful return means the job is complete; caller cancellation can interrupt the wait | Current API | join |
withContext(NonCancellable) | Only the necessary cleanup in finally; never launch(NonCancellable) | Current API | cleanup |
Order of operations
1. Create the lifecycle-owned scope before launching children. Select supervision at their shared parent (supervisor, guide). 2. Catch expected domain exceptions inside the operation. If a broad catch is necessary, immediately rethrow CancellationException; catch-all Exception prevents CancellationException propagation. mhernand40 explains this in the July 16, 2020 discussion; runCatching issue documents the same trap. 3. Put owned resource cleanup in finally. If it suspends after cancellation, use a small withContext(NonCancellable) block. Keep its duration bounded by the resource's own policy (cleanup API). 4. Stop admission, cancel the owning job, and await cancelAndJoin() before closing resources shared by children. In an already-cancelled owner, a necessary join belongs in bounded noncancellable cleanup; otherwise cancelAndJoin may throw CancellationException. (join, cleanup).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Job was Cancelled | Catch intercepts CancellationException and prevents propagation | Rethrow CancellationException; catch expected I/O/domain failures separately | discussion |
| Handler never observes async failure (symptom) | async stores failure in Deferred | Await and handle the result; a handler cannot recover a completed coroutine | handler, guide |
CancellationException during cancelAndJoin | The waiting caller is already cancelled, possibly by the failed child | Correct the parent policy and finish mandatory joins in noncancellable cleanup before releasing resources | join |
Missing
- Supervision does not supply per-task retries or a cleanup deadline. These policies depend on resource ownership and failure semantics.
Checked
2026-11-12; linked coroutines release, current API/guide pages and dated cancellation discussions read. No coroutine program executed.
Started from a post by @Kotlin project, packaged by the editor. Original post