rust-embedded / rust-embedded/heapless

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

Open
#583 21 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
Rust
Stars
2k
Forks
253
Avg merge
1d 2h
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Rust Playground reproducer and inspect the queue's enqueue and dequeue implementation, especially their behavior when two threads repeatedly move items through a full queue. Compare the observed failure with the mutex and ring-channel results mentioned in the report. Done means the reproducer no longer panics because enqueue reports failure while capacity is available, with regression coverage for the contention case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.