After reading about how `produce` can lose element...
# server
m
After reading about how
produce
can lose elements, I wonder whether
Flow::produceIn
has the same potential issue? Does anyone know? I want to use
select
to pull elements out of a flow but without the chance of losing any... (i.e. if that
SelectClause
gets cancelled, it should just receive that element on the next attempt).
j
Oh.. I don't know the answer, but do you know any particular scenarios when
produce
might lose elements?
m
I haven't delved into the implementation yet to fully understand how it happens but it has to do with cancellation of the
receive
suspend function. There's a block titled "Undelivered elements" in the documentation here: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/produce.html
👀 1
e
If you use a Channel with 0 capacity (RENDEZVOUS channel), this would not be an issue I believe?
m
that's the big question... although from how I understand it right now, it can occur even then, as long as one doesn't provide an
onUndeliveredElement
callback that somehow handles the element. How I understand it to work, it's like this: the
onReceive
SelectClause
waits for elements to appear in the flow. Once that happens, they are (a) collected and (b) sent to the channel. But if the
select
statement happens to succeed on another
SelectClause
after (a) and before (b), the
onReceive
clause would be cancelled and the element could become lost.
🤔 1