Why Wrapping a Suspend Function in withContext(Dispatchers.IO) Is Usually a Mistake

Rows of colored thread spools, representing Kotlin coroutine dispatchers and threads in a withContext(Dispatchers.IO) call

Kotlin’s suspend functions carry an implicit promise. A main-safe suspend function takes care of its own threading, no matter who calls it. In real codebases, callers break that promise constantly. A common defensive habit is wrapping every call that might touch disk or network in withContext(Dispatchers.IO). Developers do it even when the function being called … Read more