Consider this code in Kotlin:
launch {
// If we comment 'cancel()' console shows:
// B
// C
// A
// So there is no suspension point.
cancel()
launch { println("A") }
coroutineScope { println("B") }
println("C") // Will not be printed
}.join()
Why coroutine gets cancelled here even without any suspension point?
I expect “cancel()” command should never happen because there is no actual suspension point there. But it actually cancels the coroutine after executing the coroutineScope function (and printing B). If you comment the “cancel()” command and run the code, you can see that the coroutine never gets suspended (since the order of print is B, C, A).
I guess “cancellation” should work once coroutine is suspended (it does not act immediately); why coroutine gets cancelled here? Do you have any explanation for it?