kotlinx.coroutines
kotlinx.coroutines copied to clipboard
launch creates another child job
@OptIn(InternalCoroutinesApi::class)
@Suppress("DEPRECATION_ERROR", "INVISIBLE_MEMBER")
fun main() {
runBlocking {
launch(SupervisorJob()) {
// failed child cancels parent,
// which turns out not to be a supervisor,
// but a regular StandaloneCoroutine
async {
throw RuntimeException("child failure")
}
// one can verify it
val thisJob = coroutineContext.job
println(thisJob) // StandaloneCoroutine
println((thisJob as JobSupport).parentHandle?.parent) // SupervisorJobImpl
}.join()
}
}
Basically, this is equivalent to
CoroutineScope(coroutineContext + SupervisorJob()).launch {
...
}
which, at least, is obvious.
I'm not sure what title to give to this issue, there are several interlinked problems.
- one cannot
launcha supervisor coroutine; - the
Jobin the context is used as a parent forlaunch; - it's too easy to break from the parent-child hierarchy with explicitly created
Job; - even if the
Jobis created with a proper parent, no one will complete theJobpassed intolaunch.