job.cancel() Doesn't Actually Stop Your Coroutine. Here's Why | Kotlin Coroutines EP6

Опубликовано: 29 Июль 2026
на канале: Android Atlas
81
1

You call job.cancel(). The coroutine keeps running. Why? Because Kotlin doesn't kill coroutines — it asks politely.

This episode builds the complete cancellation mental model: why cancellation is cooperative (not a kill switch), how coroutines detect cancellation with isActive and ensureActive, the NonCancellable trap in finally blocks, and why CancellationException is not a failure.

This is Episode 6 of 8. Watch in order — each episode builds on the last.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━
CHAPTERS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
0:00 Why job.cancel() Doesn't Stop Instantly
0:10 Part 1: Cancellation Is a Request, Not a Kill Switch
1:23 Part 2: isActive — The Question
2:18 Part 3: ensureActive — The Enforcement
3:26 Part 4: NonCancellable — The Cleanup Zone
4:32 Part 5: CancellationException — The Mechanism
5:48 Misconceptions Round-Up
6:39 Recap + Bridge to Episode 7

━━━━━━━━━━━━━━━━━━━━━━━━━━━━
THE SERIES — KOTLIN COROUTINES DEEP DIVE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
8 episodes. 35 concepts. The complete mental model — built one layer at a time.

EP 1 → What Is a Coroutine? -    • EP 1 - What Is a Kotlin Coroutine? (It's N...  
EP 2 → suspend, Continuations & Builders -    • EP 2 - Kotlin suspend Keyword: What It REA...  
EP 3 → Scope, Job & Structured Concurrency -    • EP 3 - CoroutineScope & Structured Concurr...  
EP 4 → SupervisorJob & Failure Isolation -    • EP 4 - SupervisorJob & Failure Isolation: ...  
EP 5 → Dispatchers, Threads & withContext -    • EP 5 - Kotlin Dispatchers Explained: Main ...  
EP 6 → Cancellation (this video) -    • EP 6 - Kotlin Coroutines Cancellation: Why...  
EP 7 → Exception Handling -    • EP 7 - Kotlin Coroutines Exceptions: Why O...  
EP 8 → Real Android Patterns & The Complete Model -    • EP 8 - Kotlin Coroutines Complete: Real An...  

Full Playlist:    • Kotlin Coroutines Deep Dive: The Complete ...  

━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ALSO ON THIS CHANNEL
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Want the 60-second version of each concept? Every topic in this series is also a Short.
Shorts Playlist:    • Kotlin Coroutines Tutorial: Mental Models,...  

━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WHAT YOU'LL LEARN
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Why cancellation is cooperative — and why force-killing would be dangerous
• Job as the remote control: cancel() sends a request, not a command
• isActive: how to check cancellation state in loops and polling
• ensureActive: how to enforce immediate stop at checkpoints
• The NonCancellable trap: why suspending cleanup in finally blocks can silently fail
• CancellationException: a control flow mechanism, not a failure signal
• Why catching all exceptions swallows CancellationException — and how to avoid it

━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WHO THIS IS FOR
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Android and Kotlin developers who've wondered why cancelled coroutines keep running, been confused by NonCancellable, or accidentally swallowed CancellationException. This episode builds the mental model that makes all of it make sense.

#KotlinCoroutines #Kotlin #AndroidDev