#5323·miri

Improve random scheduler to consider more "interesting" interleavings

Author: bal-eCreated Sep 18, 2026Updated Sep 19, 2026
LabelsC-proposalA-concurrency

I was preparing a simple example of a race condition to demonstrate how MIRI catches it, but I ran into some unexpected semantics in MIRI's evaluation of std::thread::scope(). Here's a reduced test case, that passes consistently under many trials of -Zmiri-many-seeds:

rust
let x = AtomicI32::new(0);
std::thread::scope(|s| {
    s.spawn(|| x.store(1, Release));
    // The store of 1 is not observed here.
    assert_eq!(x.load(Acquire), 0);
});

If I wrap the assertion in another .spawn(), MIRI will begin detecting failures.

rust
let x = AtomicI32::new(0);
std::thread::scope(|s| {
    s.spawn(|| x.store(1, Release));
    s.spawn(|| {
        // The above store is observable, so this often fails.
        assert_eq!(x.load(Acquire), 0);
    });
});

This is very surprising behavior to me. The documentation of std::thread::scope() does not seem to guarantee that spawned threads will start after the scope closure finishes. Am I missing something?

My original code
rust
use std::sync::{
    Arc,
    atomic::{
        AtomicPtr,
        Ordering::{AcqRel, Acquire},
    },
};

/// Mom's `Atomic<Arc<T>>`
struct MomsAtomicArc<T>(AtomicPtr<T>);

impl<T> MomsAtomicArc<T> {
    fn new(x: Arc<T>) -> Self {
        Self(AtomicPtr::new(Arc::into_raw(x).cast_mut()))
    }

    fn load(&self) -> Arc<T> {
        let ptr = self.0.load(Acquire).cast_const();
        unsafe {
            // NOTE: THIS SHOULD BE UNSOUND
            // clone the `Arc` manually
            Arc::increment_strong_count(ptr);
            Arc::from_raw(ptr)
        }
    }

    fn swap(&self, new: Arc<T>) -> Arc<T> {
        let new = Arc::into_raw(new).cast_mut();
        let old = self.0.swap(new, AcqRel);
        unsafe { Arc::from_raw(old.cast_const()) }
    }
}

impl<T> Drop for MomsAtomicArc<T> {
    fn drop(&mut self) {
        unsafe { Arc::from_raw(self.0.get_mut().cast_const()) };
    }
}

#[test]
fn everything_is_fine() {
    let swapper = MomsAtomicArc::new(Arc::new(0i32));

    std::thread::scope(|s| {
        s.spawn(|| {
            let _ = swapper.swap(Arc::new(1i32));
        });

        // I expect MIRI to report a use-after-free here
        let x = swapper.load();
        println!("Saw {x}");
        // For some reason, this passes consistently:
        assert_eq!(*x, 0);
    });
}
> cargo +nightly miri --version
miri 0.1.0 (330d317121 2026-09-17)