https://kotlinlang.org logo
#coroutines
Title
# coroutines
w

wrover

04/10/2019, 6:51 AM
Hi! I wonder if there is a way to adopt the underlying resource closing interrupting approach to deal with blocking method calls like we do in Java in couroutines? I tried to use
Continuation#invokeOnCancellation
from
suspendCancellableCoroutine
but, obviously, it doesn't work this way.
g

gildor

04/10/2019, 7:04 AM
Hm, works for me (without real socket, but just to check general correctness) https://pl.kotl.in/rJfPHGiK4
w

wrover

04/10/2019, 8:14 AM
Yes, but when
accept
is blocking it doesn't.
g

gildor

04/10/2019, 9:23 AM
Yes, and it make sense
wrap blocking call to non-blocking coroutine, anyway you shouldn’t block suspend function
w

wrover

04/10/2019, 10:26 AM
I could not agree more, but actually the question is about integration with Java libraries, e.g. RabbitMQ Client, which have asynchronous methods which block under certain conditions, in this case when server resources are depleted. In this situation using suspending function makes sense but you need a way to cancel execution when a block occurs.
g

gildor

04/10/2019, 10:30 AM
You can do this, you just should wrap blocking calls, otherwise you block thread that should cancel this resource
w

wrover

04/10/2019, 12:29 PM
@gildor thanks!
g

gildor

04/10/2019, 1:07 PM
@wrover You know, I checked your example again, and looks that you have another problem with this code, I realized that you actually running launch on IO, so it should work, maybe it doesn't work just because you do not handle exception in suspendCoroutine block and never return in this case, I will check a bit later my theory
3 Views