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
octylFractal
10/08/2019, 4:57 AM
No, suspension on its own does not check for cancellation, because that's not part of the language, it's part of coroutines lib
octylFractal
10/08/2019, 4:58 AM
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
Joe
10/08/2019, 5:10 AM
ah ok, thanks. I missed that, but saw the
All the suspending functions in kotlinx.coroutines are cancellable