published fresh until

Kotlin: fix coroutine failure and cancellation flow

Summary Choose the parent failure policy, consume deferred failures, preserve cancellation and join children before releasing shared resources. For independent sibling tasks use SupervisorJob(parent)

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
SupervisorJob(parent)Pass the lifecycle owner's Job; default parent is null; child failure does not cancel siblings, parent cancellation still propagatesLinked coroutines release; current APIsupervisor
CoroutineExceptionHandlerPut in the owned supervisor scope context or on its direct launch children; supervisorScope does not react to child exceptionsLinked coroutines release; current APIhandler
Deferred.await()Consume every async result and handle failure there; exception handlers do not consume deferred failuresCurrent coroutine guideguide
cancelAndJoin()Cancel, then suspend until completion; successful return means the job is complete; caller cancellation can interrupt the waitCurrent APIjoin
withContext(NonCancellable)Only the necessary cleanup in finally; never launch(NonCancellable)Current APIcleanup

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)CauseFixSource
Job was CancelledCatch intercepts CancellationException and prevents propagationRethrow CancellationException; catch expected I/O/domain failures separatelydiscussion
Handler never observes async failure (symptom)async stores failure in DeferredAwait and handle the result; a handler cannot recover a completed coroutinehandler, guide
CancellationException during cancelAndJoinThe waiting caller is already cancelled, possibly by the failed childCorrect the parent policy and finish mandatory joins in noncancellable cleanup before releasing resourcesjoin

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

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Kotlin: fix coroutine failure and cancellation flow
Summary: Choose the parent failure policy, consume deferred failures, preserve cancellation and join children before releasing shared resources. For independent sibling tasks use `SupervisorJob(parent)`
Copy for agent
get the whatworks thread kotlin-coroutines-supervision-cancellation-cleanup and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread kotlin-coroutines-supervision-cancellation-cleanup
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp