SupervisorJob in Kotlin — Line by Line
John Kenedy | Make This Work
0:00 / 0:00
SupervisorJob in Kotlin — Line by Line
4 просмотра · 10 дней назад
John Kenedy | Make This Work
27 подписчиков
4 просмотра · 10 дней назад
SupervisorJob explained line by line: why one failing coroutine kills all its siblings, how supervision stops that, and why the way most people reach for it does nothing except leak.
Structured concurrency has a rule that surprises everyone once: when a child coroutine fails, the parent cancels every other child. Usually that's exactly what you want. Sometimes the children are genuinely unrelated — an animation and an API call — and one failing shouldn't take the other down.
SupervisorJob is the fix. It's also the thing people most reliably use wrong, and there are two misconceptions that matter more than the definition.
What I cover:
• Why a single failure cancels the whole scope, and why a cancelled scope stays dead
• How SupervisorJob propagates cancellation downward but not upward
• Why supervision applies to direct children only, and is not inherited down the tree
• viewModelScope and lifecycleScope are already supervisor scopes — you inherited the behaviour
• Passing a Job into launch re-parents the coroutine out of the scope: it leaks, nothing awaits it, and it supervises nothing
• supervisorScope for a single block, the option most people forget
• Why a supervised coroutine that throws still crashes your app
The two things worth the watch:
First, if you're adding SupervisorJob to a ViewModel, you already had one. viewModelScope is built as SupervisorJob plus Dispatchers.Main.immediate. Sibling coroutines there already fail independently.
Second, supervision is not error handling. SupervisorJob decides whether siblings get cancelled. It does nothing about the exception itself, which still reaches the default handler and still kills the app. You need a CoroutineExceptionHandler as well — and async is different again, because it holds the exception until you await it.
TIMESTAMPS
00:00 The rule nobody expects
01:34 One child fails, the whole scope dies
04:02 SupervisorJob, and cancelling downward only
06:20 Direct children only — supervision is not inherited
08:05 Your ViewModel already has one
10:12 The trap: passing a Job into launch
13:30 supervisorScope for a single block
15:48 Supervision is not error handling
18:26 async, await, and where the exception goes
Read the full written breakdown:
https://makethiswork.dev/lets-make-su...
Part of the Android, Line by Line series — the WorkManager episode covers a
cancellation bug that comes from the same corner of coroutines:
/ @johnkenedy
Connect with me / More resources:
Blog: https://makethiswork.dev
LinkedIn: / johnkenedy
GitHub: https://github.com/johnkenedy
Support the channel / Join the journey:
Subscribe for more Android deep dives & career tips: / @johnkenedy
Like & comment if this helped you — had you ever checked whether viewModelScope was already supervised?
Share it with someone learning Android or preparing for a career abroad.
Let's make this work!
Tools used in this video:
Android Studio
Kotlin / Coroutines
#Kotlin #AndroidDev #Coroutines