⚠️ Dont use withContext(SupervisorJob()) ⚠️
Gotcha withContext(SupervisorJob()) uses SupervisorJob as parent, while creating child as its job Job()
In my workshops, I am often asked if we can use withContext(SupervisorJob()) instead of supervisorScope. No, we can’t. When we use withContext(SupervisorJob()), then withContext is still using a regular Job, and the SupervisorJob() becomes its parent. As a result, when one child raises an exception, the other children will be cancelled as well. withContext will also throw an exception, so its SupervisorJob() is practically useless. This is why I find withContext(SupervisorJob()) pointless and misleading, and I consider it a bad practice. - Kotlin Coroutines Deep Dive
Backlinks