vaskir
06/04/2020, 4:06 PMp
is Deferred
, then I expected it to be cancelled on timeout:
select<Unit> {
p.onAwait { println("ok") }
onTimeout(1000) { println("timed out!") }
}
but it's not, it prints "timeout out!", p
is not cancelled. What is the best way to cancel a Deferred
by timeout?Zach Klippenstein (he/him) [MOD]
06/04/2020, 4:26 PMp.cancel()
from your onTimeout
block? Alternatively, if p
is a child of the current scope, maybe you have more than one, then you can call scope.coroutineContext.cancelChildren()
.vaskir
06/04/2020, 5:18 PMwithTimeout(1000) {
p.await()
}
🙂 This throws
Exception in thread "main" kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms
at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:158)
at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:128)
at kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run(EventLoop.common.kt:497)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
at kotlinx.coroutines.DefaultExecutor.run(DefaultExecutor.kt:68)
at java.base/java.lang.Thread.run(Thread.java:834)
which is what I need.vaskir
06/04/2020, 5:44 PMwithTimeout(1000) {
runCommand("cmd.exe", File("."))
}
🙂vaskir
06/04/2020, 5:44 PMvaskir
06/04/2020, 5:44 PMZach Klippenstein (he/him) [MOD]
06/04/2020, 5:45 PM