5 Critical Kotlin Coroutine Secrets You Need to Know
Kotlin Coroutines are powerful, but their simplicity can be deceptive. Many developers encounter subtle bugs because they don’t fully understand the underlying mechanics. Here are five secrets to help you write safer, more efficient asynchronous code.
1. Always Use SupervisorJob in Custom Scopes π
When binding a scope to a custom lifecycle (like a Service), the default behavior is that if one child coroutine fails, the entire scope is cancelled. Use SupervisorJob to ensure failures are isolated.
// Good: Use SupervisorJob for independent tasks
val serviceScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
2. Cleanup Safely in Finally Blocks π
When a coroutine is cancelled, it immediately throws a CancellationException. If you try to call a suspend function (like deleting a file) inside a finally block, it will be skipped. Use NonCancellable to ensure cleanup runs.
try {
// work
} finally {
withContext(NonCancellable) {
deleteFile() // Guaranteed to execute
}
}
3. Don’t Swallow Cancellation Exceptions π
Catching Exception will catch CancellationException, which prevents the coroutine from stopping correctly and can lead to infinite loops. Always rethrow cancellation signals.
try {
// polling loop
} catch (e: Exception) {
if (e is CancellationException) throw e
// handle other errors
}
4. Inject Your Dispatchers π
Hardcoding Dispatchers.IO makes unit testing difficult. By injecting a DispatcherProvider interface, you can swap real dispatchers for TestDispatcher during tests.
class ImageReader(private val dispatchers: DispatcherProvider) {
suspend fun read() = withContext(dispatchers.io) { ... }
}
5. Keep Blocking Code Cooperative π
Coroutines are cooperative. If you have a long-running blocking call, it won’t check for cancellation. Manually check ensureActive() or use yield() to keep your code responsive.
while (reading) {
ensureActive() // Check if coroutine was cancelled
val data = inputStream.read()
// process...
}
I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .