I have a blocking call that I'm able to cancel. Wi...
# coroutines
d
I have a blocking call that I'm able to cancel. Will
suspendCancellableCoroutine
be enough or should I wrap it in
async(<http://Dispatchers.IO|Dispatchers.IO>)
? I'm not completely sure how
suspendCancellableCoroutine
works here.
z
I think whether to launch a coroutine is a separate issue and doesn’t affect cancellation. If you don’t launch a separate coroutine to run the blocking call, then the caller will never “see” the suspension – it will just look like the call never suspended, since the calling thread will be blocked. However, if the calling coroutine’s
Job
is cancelled, then your blocking call would still get cancelled immediately. If you’re wrapping your blocking call in a suspend function, I would expect the function to launch a separate coroutine to actually block so it doesn’t block the caller, but that’s not a cancellation-related concern. In other words, I believe that if you register an
invokeOnCancellation
action, that action will be executed synchronously on the thread that invoked
cancel
, and so even if the thread that called
suspendCancellableCoroutine
is blocked the cancellation will still occur.
👍🏼 1