We have code similar to this: ``` val job = launch...
# coroutines
j
We have code similar to this:
Copy code
val job = launch {
    val flow = slowBlockingCallThatReturnsIterator().asFlow()
    flow.collect { 
        yield() // allows for cancel() to work
        channel.send(it) 
    }
}
If we call
job.cancel()
during the slowBlockingCall (or while the flow is collecting) without the
yield()
call, all entries in the iterator still get sent to the channel. If we add back the yield call, then the collection aborts as we initially expected to happen without the yield(). Since channel.send is a suspend function, shouldn't calling it check the coroutine active state?
o
No, suspension on its own does not check for cancellation, because that's not part of the language, it's part of coroutines lib
send
does check for it, but only if it suspends:
Note that this function does not check for cancellation when it is not suspended. Use yield or CoroutineScope.isActive to periodically check for cancellation in tight loops if needed.
j
ah ok, thanks. I missed that, but saw the
All the suspending functions in kotlinx.coroutines are cancellable
doc that made me think otherwise