Endre Deak
05/31/2022, 10:01 PMimport kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import sun.misc.Signal
suspend fun main() {
withContext(Dispatchers.Default) {
Signal.handle(Signal("INT")) {signal ->
println("RECEIVED ${signal.name}")
this.coroutineContext.cancelChildren()
}
launch {
withContext(Dispatchers.Default) {
launch {
while (true) {
try {
println("foo")
// delay(2000) // if I put delay here, it fails with an infinite loop on SIGINT
} catch (e: Exception) {
println("error: ${e.message}")
}
delay(2000) // if I put delay here outside try-catch, no problem on SIGINT
}
}
}
}
}
println("finished")
}
Endre Deak
05/31/2022, 10:01 PMdelay
within a try-catch
causes an infinite loop with errors?ephemient
05/31/2022, 10:10 PMCancellationException
which breaksephemient
05/31/2022, 10:12 PM