Exerosis
11/14/2022, 8:27 AMval test = withTimeoutOrNull(3.seconds) {
runInterruptible {
channel.receive(ByteBuffer.allocateDirect(10))
}
}
I would have expected null to be returned. Catching the exception results in a value being returned instead of null.Joffrey
11/14/2022, 8:48 AMrunInterruptible
here? channel.receive
is suspending, so I don't see a reasonJoffrey
11/14/2022, 8:49 AMrunInterruptible
is to convert cancellations from calling code into thread interruption for the wrapped blocking code. If the code is already coroutine-based and supports coroutine cancellation, you shouldn't use runInterruptible
simon.vergauwen
11/14/2022, 9:52 AMjava.nio.channels
đŸ¤” KotlinX Coroutines Channels doesn't have such an API with ByteBuffer
, does it?simon.vergauwen
11/14/2022, 9:53 AMjava.nio.channels
don't work with InterruptedException
it seems, like runInterruptible
relies on.uli
11/14/2022, 2:22 PMClosedByInterruptException
is not an InterruptedException
. That’s the issue.
In more depth: runInterruptible
works by catching InterruptedException
and rethrowing it as CancellationException
.
The issue is here, that channel.receive
(did you mean channel.read?) catches the `InterruptedException`and rethrows it as ClosedByInterruptException
which is unknown to runInterruptible
and breaks everything.uli
11/14/2022, 2:24 PMval test = withTimeoutOrNull(3.seconds) {
runInterruptible {
try {
channel.receive(ByteBuffer.allocateDirect(10))
} catch (e: ClosedByInterruptException) {
throw CancellationException("Blocking call was interrupted due to parent cancellation").initCause(e)
}
}
}