hawkw / thingbuf

in-place allocation-reusing queues for Rust
MIT License
287 stars 24 forks source link

Behavior of `SendRef` can surprise the user #88

Open sgasse opened 5 months ago

sgasse commented 5 months ago

Hi! In #70 , I commented our workaround to avoiding empty sends after already taking out a SendRef. We used to store the SendRef in an Option for the next loop iteration. This workaround came back to bite us. The behavior of SendRef is correct for all I understand but it can still be surprising.

Let's look at the following situation on a high-level:

Naively, we might expect that the following is observable on the receiver side:

Unfortunately, that is not what is happening. Nothing will be received until 10 seconds pass, and then both items are received in succession, but the item from thread 0 first (although it was "sent by dropping the slot" second). With the inner workings of thingbuf, this makes sense since the pointer to the next element to receive cannot advance while the first SendRef is still held.

Though I wonder if it might make sense to mention this in the documentation, so that less people for it. Especially with several tasks / threads, this can lead to ugly situations if someone holds on to a SendRef and thereby inadvertently stalls the receiver.

Thanks to @flxo for helping me in debugging :pray:

sgasse commented 5 months ago

An example can be found in the draft PR #89

Dr-Emann commented 4 months ago

I personally did expect exactly the actual behavior: I think of creating a sendref as "reserving my spot" in the queue, but I do think it's definitely a good idea to make it clearer in the docs.