Coroutines have made life very simple to write and understand the asynchronous code but I think it would be great if we improve the debugging of coroutines. Consider the below code as an example:
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val channel = Channel<Int>()
val job1 = launch {
repeat(5) { channel.send(it) }
}
val job2 = launch {
channel.receive()
}
job2.join()
println("Job2 done") // Put a breakpoint here
job1.join()
println("Job1 done")
}
If I put a breakpoint at the code println("Job2 done")
, I can’t access the channel
and job2
variables.
While running the code, those variables are not needed as they are not being used after the println("Job2 done")
line but it would be very helpful in debugging, If a debugger can provide an option to get the variables while debugging. However, the way to get those variables in debugging, I can write println
and it will work but it is little cumbersome.