smol-rs/async-channel

Document MPMC behavior

schell opened this issue · 2 comments

I was surprised to find that cloning a Receiver and sending a message on the connected Sender results in the message showing up at only one of the downstream Receivers, but not both. Sending more messages results in a round-robin of sorts, where the Receiver next in line awaiting recv will get the next message.

This doesn't seem to be the implied MPMC behavior, though I'm not an authority on the matter.

Here's a minimal example in the form of a failing test:

    #[test]
    fn channel_sanity() {
        let (tx, rx1) = async_channel::unbounded::<u32>();
        let rx2 = rx1.clone();
        let t1 = smol::spawn(async move {
            let n = rx2.recv().await.unwrap();
            assert_eq!(n, 666);
        });
        let t2 =
            smol::spawn(async move {
            let n = rx1.recv().await.unwrap();
            assert_eq!(n, 666);
        });

        smol::block_on(async move {
            tx.send(666).await.unwrap();
            tx.send(123).await.unwrap();
            let ((), ()) = futures::future::join(t1, t2).await;
        });
    }

Ah, I see. I missed that update. Thanks for the clarity.