Skip to content

MpMcQueue can fail to push even when it's not full under contention #583

@nodakai

Description

@nodakai

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=0a9dde3dac37be94314c5719726060c7

#[test]
fn test() {
    fn to_vec<T>(q: &QueueView<T>) -> Vec<T> {
        // inaccurate
        let mut ret = vec![];
        while let Some(v) = q.dequeue() {
            ret.push(v);
        }
        ret
    }
    const N: usize = 4;

    let q0 = Queue::<u8, N>::new();
    for i in 0..N {
        q0.enqueue(i as u8).expect("new enqueue");
    }
    eprintln!("start!");

    std::thread::scope(|sc| {
        for _ in 0..2 {
            sc.spawn(|| {
                for k in 0..1000_000 {
                    if let Some(v) = q0.dequeue() {
                        q0.enqueue(v).unwrap_or_else(|v| {
                            panic!("{}: q0 -> q0: {}, {:?}", k, v, to_vec(&q0))
                        });
                    }
                }
            });
        }
    });
}

Confirmed an implementation based on a mutex, and another based on ring-channel didn't panic

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions