Skip to content

Semantics of Receiver::try_recv() differ between Sender and SyncSender #34711

Closed
@GGist

Description

@GGist

I tried this code:

use std::sync::mpsc::{self};
use std::mem;

fn main() {
    let (sync_send, sync_recv) = mpsc::sync_channel(1);

    sync_send.send(()).unwrap();
    mem::drop(sync_send);

    println!("{:?}", sync_recv.try_recv());

    //-----------------------------------------//

    let (send, recv) = mpsc::channel();

    send.send(()).unwrap();
    mem::drop(send);

    println!("{:?}", recv.try_recv());
}

I expected to see this happen:

Ok(())
Ok(())

Instead, this happened:

Err(Disconnected)
Ok(())

Notes

I see that in the docs there is a comment under the Receiver::recv() method that states "However, since channels are buffered, messages sent before the disconnect will still be properly received." but this comment is not duplicated under Receiver::try_recv().

However, I tried tracing the specific Receiver used in conjunction with SyncSenders which led me to here where it looks like an eager check for a disconnect while ignoring possible data that is still buffered in the Queue. Is this intended?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions