trathschlag
03/11/2019, 12:45 PMfun main() {
runBlocking {
val job = launch {
suspendCoroutine<Unit> {}
}
yield()
job.cancel()
println("goodbye")
}
println("exit")
}
prints
goodbye
and then hangs forever. Does anybody know why this happens? When removing the yield()
it just works as expected.sitepodmatt
03/11/2019, 12:52 PMfun main(argv: Array<String>) {
runBlocking {
val job = launch {
suspendCoroutine<Unit> { continuation ->
continuation.resume(Unit)
}
}
yield()
job.cancel()
println("goodbye")
}
println("exit")
}
streetsofboston
03/11/2019, 12:52 PMsitepodmatt
03/11/2019, 12:52 PMribesg
03/11/2019, 12:53 PMsitepodmatt
03/11/2019, 12:53 PMtrathschlag
03/11/2019, 12:55 PMsuspendCoroutine<Unit> { continuation ->
performPotentiallyNeverEndingStuff(callback = {
continuation.resume(Unit)
})
}
Which effectively has the same problemsitepodmatt
03/11/2019, 12:59 PMun main(argv: Array<String>) {
runBlocking {
val job = launch {
suspendCancellableCoroutine<Unit> { }
}
yield()
job.cancel()
println("goodbye")
}
println("exit")
}
trathschlag
03/11/2019, 1:00 PMstreetsofboston
03/11/2019, 1:04 PMtrathschlag
03/11/2019, 1:13 PMrunBlocking
body was never suspended, so the job did not even start executing before it was cancelled again. So the non-cancellable suspendCoroutine
did not start and didn't block the cancellation of the job.fun main(argv: Array<String>) {
runBlocking {
val job = launch(Dispatchers.Default) {
suspendCoroutine<Unit> { }
}
job.cancel()
println("goodbye")
}
println("exit")
}
This sometimes blocks and sometimes not, because if the default dispatcher manages to execute suspendCoroutine
before the runBlocking
finishes, the job is non-cancellable again.streetsofboston
03/11/2019, 1:25 PM