For `suspendCancellableCoroutine`, does the `invok...
# coroutines
r
For
suspendCancellableCoroutine
, does the
invokeOnCancellation
get called when I call
resume
normally, or should I clean up there as well?
k
I'm not sure, but I'm curious to find out. I think a good supplementary question is when do we need to use both
invokeOnCancellation
and the cancelation handler provided in
resume
?
e
I'm not quite sure what you mean, but
invokeOnCancellation
callback gets invoked when the continuation is cancelled
resume.onCancellation
callback gets invoked when you try to
resume
after cancellation it's possible for only 1 to happen, or 1 then 2 to happen
k
Oh, right. I see. I typed that message when I was still pre-coffee 😅 makes perfect sense.
r
Weird, I've had 2 happen without 1 somehow, aka the callback should have been unregistered, but wasn't
d
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-cancellable-continuation/invoke-on-cancellation.html should answer this.
synchronously invoked on cancellation (regular or exceptional) of this continuation
This handler is also called when this continuation resumes normally (with a value) and then is cancelled while waiting to be dispatched. More generally speaking, this handler is called whenever the caller of suspendCancellableCoroutine is getting a CancellationException.
So, after a normal
resume
, the handler can only be called if cancellation happened anyway.