Joe
10/08/2019, 4:52 AMval 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?octylFractal
10/08/2019, 4:57 AMsend
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.
Joe
10/08/2019, 5:10 AMAll the suspending functions in kotlinx.coroutines are cancellable
doc that made me think otherwise