diff --git a/Cargo.lock b/Cargo.lock index 7e3bc7d3a18..2f75101dc24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1955,7 +1955,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" dependencies = [ "crossbeam-utils", - "loom", ] [[package]] @@ -1963,9 +1962,6 @@ name = "crossbeam-utils" version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" -dependencies = [ - "loom", -] [[package]] name = "crunchy" @@ -4794,7 +4790,6 @@ dependencies = [ "crossbeam-epoch", "futures", "kio-macros", - "loom", "smallvec", "tokio", ] @@ -5800,7 +5795,6 @@ dependencies = [ "hashlink", "kio 0.6.2", "libc", - "loom", "moq-clock", "moq-pattern 0.1.2", "num_enum", diff --git a/Cargo.toml b/Cargo.toml index d6c151a5e47..f8ae08f0858 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,10 +69,6 @@ resolver = "2" # That pin sits above every MSRV here; rust-toolchain.toml says why. rust-version = "1.91" -[workspace.lints.rust] -# kio and moq-net model-check their primitives under `--cfg loom`. -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] } - [workspace.lints.clippy] # Every suppression says why, so the next lint audit can judge it. allow_attributes_without_reason = "warn" @@ -127,9 +123,6 @@ kio-macros = { version = "0.1.0", path = "rs/kio/macros" } libc = "0.2" libloading = "0.9" linux-raw-sys = { version = "0.12.1", features = ["ioctl"] } -# Permutation testing for the kio primitives (and everything built on them). -# Only compiled under `--cfg loom`; see `just rs loom`. -loom = { version = "0.7.2", features = ["futures"] } # DNS-SD advertisement and browsing for LAN peer discovery (moq-tokio's `mdns` feature). # `async` awaits the event channel instead of blocking a thread on it. mdns-sd = { version = "0.21", features = ["async"] } diff --git a/quest/m2/crossbeam-pin-fence.md b/quest/m2/crossbeam-pin-fence.md index a8839b3810d..47000acfaf8 100644 --- a/quest/m2/crossbeam-pin-fence.md +++ b/quest/m2/crossbeam-pin-fence.md @@ -6,7 +6,7 @@ Waiting on upstream crossbeam. Once [crossbeam-rs/crossbeam#1352](https://github.com/crossbeam-rs/crossbeam/issues/1352) is fixed in a released crossbeam-epoch, the workspace takes that release and kio's `fence(Release)` in `Read::drop` (`rs/kio/src/log.rs`) is deleted, with -the log eviction loom model still passing on ARM. +kio's log tests still passing on ARM. Check #1352's state and crossbeam-epoch's latest release. Delete this quest if upstream declines the fix. @@ -25,9 +25,10 @@ Decided 2026-10-28 (maintainer): keep crossbeam-epoch rather than replace it. refcounted segments and per-read refcounts, it supports loom, and moka pulls it into moq-relay anyway. -Verify on ARM: run `just rs loom` on an ARM host; Nightly no longer runs it -(decided 2026-10-31, see the [CI epic](/quest/m0/ci/README.md)). Without an -ARM host, force crossbeam's non-x86 branch in a scratch copy, as #7134 did, -and confirm the model passes without the fence. +Verify on ARM: Nightly's `gate` job runs kio's log tests on +`ubuntu-24.04-arm`. The loom model that checked this ordering is gone (decided +2026-10-31, maintainer: "Remove loom entirely"), so also confirm from the +released `Local::pin` that it orders the epoch store before the reads it +guards, which is what the fence restores. Public API: none. Wire: none. diff --git a/quest/m2/miri-memory.md b/quest/m2/miri-memory.md index dff1868cc15..b691394bac6 100644 --- a/quest/m2/miri-memory.md +++ b/quest/m2/miri-memory.md @@ -4,7 +4,7 @@ The hand-written unsafe in `kio` and `moq-net` that touches only memory (no sockets, GPU, or FFI) runs under Miri nightly, so a use-after-free, data race, -or aliasing violation fails a test instead of waiting for Loom or production. +or aliasing violation fails a test instead of waiting for production. ## Plan diff --git a/quest/m2/perf/kio-channel-contention/version.md b/quest/m2/perf/kio-channel-contention/version.md index eab91cb237a..59aaf016e66 100644 --- a/quest/m2/perf/kio-channel-contention/version.md +++ b/quest/m2/perf/kio-channel-contention/version.md @@ -30,6 +30,5 @@ longer contends with readers of the value. Lands with the contended bench (`rs/k `Consumer`, the `Waiter`, or a caller-held cursor). The `Consumer` is shared by `&self` across waiters, which argues for a caller-held cursor. Settle it first, with options and a recommendation. -- Every new atomic protocol gets a loom model in `rs/kio/src/loom.rs`. - Generic across every `T`, with no moq-net restructure. diff --git a/quest/m2/perf/single-delivery-write.md b/quest/m2/perf/single-delivery-write.md index a661d15505b..99de0b6b146 100644 --- a/quest/m2/perf/single-delivery-write.md +++ b/quest/m2/perf/single-delivery-write.md @@ -18,7 +18,7 @@ Decided 2026-10-28 (maintainer): accept the residual in #7141 and explore a lock-free path here, in m2. - The risk is a second copy attaching to a frame while it is still being - written. Model that race in loom before relying on it. + written. Rule that race out before relying on it. - Keep the shared reservation and canonical-content rules #6922 added. - A measured no-win abandons the quest. diff --git a/rs/justfile b/rs/justfile index e00812091e2..889ba633b48 100644 --- a/rs/justfile +++ b/rs/justfile @@ -292,28 +292,6 @@ play *args: doctest *args: bash sh/local.sh cargo test --locked --doc "$@" -# `--cfg loom` swaps kio's primitives for loom's, so it rebuilds the world and -# runs only on demand. -# `--release` because a model check runs the body once -# per interleaving (411s -> 51s); the assertions are plain `assert!`. kio's -# models search every interleaving; moq-net's cap preemptions (see -# `tests/loom.rs`, `LOOM_MAX_PREEMPTIONS`). kio's own unit tests are -# `cfg(not(loom))`. - -# Permutation-test the concurrent handoffs with loom. `crossbeam_loom` puts the -# epoch collector that frees `kio::log` segments under loom too. -[positional-arguments] -loom *args: - RUSTFLAGS="--cfg loom --cfg crossbeam_loom" cargo test --locked --release -p kio --lib loom:: "$@" - RUSTFLAGS="--cfg loom --cfg crossbeam_loom" cargo test --locked --release -p moq-net --test loom "$@" - RUSTFLAGS="--cfg loom --cfg crossbeam_loom" cargo test --locked --release -p moq-net --test expiry-loom "$@" - -# Compile the loom models without running them. The dev profile type-checks -# the same code: after a moq-net edit it took 46s against release's 146s. -loom-check: - RUSTFLAGS="--cfg loom --cfg crossbeam_loom" cargo test --locked --no-run -p kio --lib - RUSTFLAGS="--cfg loom --cfg crossbeam_loom" cargo test --locked --no-run -p moq-net --test loom --test expiry-loom - # Run the moq-quic simulation's random workloads from a fresh seed, printing the settings that replay it, then the bandwidth probe's acceptance suite. quic-sim: #!/usr/bin/env bash diff --git a/rs/kio/Cargo.toml b/rs/kio/Cargo.toml index 205a398271c..4c98ec5b76d 100644 --- a/rs/kio/Cargo.toml +++ b/rs/kio/Cargo.toml @@ -24,14 +24,6 @@ crossbeam-epoch = "0.9.21" kio-macros = { workspace = true, optional = true } smallvec = { version = "1.15", features = ["union"] } -# Loom swaps in its own Arc/Mutex/atomics so it can permute every interleaving. -# It's a normal (not dev) dependency because `sync.rs` re-exports it from the -# library itself, but `cfg(loom)` is never set outside `just rs loom`. -[target.'cfg(loom)'.dependencies] -# Under `--cfg crossbeam_loom` too, so loom permutes the collector's own atomics. -crossbeam-epoch = { version = "0.9.21", features = ["loom"] } -loom = { workspace = true } - [dev-dependencies] # Bench-only: the FuturesUnordered comparison in benches/tasks.rs. criterion = { package = "bench-harness", path = "../bench-harness" } diff --git a/rs/kio/src/bridge.rs b/rs/kio/src/bridge.rs index adf039bc0aa..bd4c32e9693 100644 --- a/rs/kio/src/bridge.rs +++ b/rs/kio/src/bridge.rs @@ -1,15 +1,11 @@ use std::{ fmt, ops::{Deref, DerefMut}, - // std, not `crate::sync`: `Wake` takes std's Arc. - sync::{Arc, PoisonError}, + sync::{Arc, Mutex, MutexGuard, PoisonError}, task::{Context, Wake, Waker}, }; -use crate::{ - Waiter, WaiterList, - sync::{Mutex, MutexGuard}, -}; +use crate::{Waiter, WaiterList}; /// A [`Context`]-based handle that any number of waiters poll at once. /// @@ -168,7 +164,7 @@ impl fmt::Debug for Bridge { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod tests { use std::{ sync::atomic::{AtomicUsize, Ordering}, diff --git a/rs/kio/src/consumer.rs b/rs/kio/src/consumer.rs index 44af36a6a68..744d81fd74e 100644 --- a/rs/kio/src/consumer.rs +++ b/rs/kio/src/consumer.rs @@ -1,6 +1,9 @@ -use std::{sync::atomic::Ordering, task::Poll}; +use std::{ + sync::{Arc, atomic::Ordering}, + task::Poll, +}; -use crate::{Closed, Counts, State, lock::*, producer::Ref, sync::Arc, waiter::*, weak::ConsumerWeak}; +use crate::{Closed, Counts, State, lock::*, producer::Ref, waiter::*, weak::ConsumerWeak}; /// The consuming side of a shared state channel. /// @@ -191,7 +194,7 @@ impl Clone for Consumer { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use crate::{Closed, Producer}; use std::task::Poll; diff --git a/rs/kio/src/coop.rs b/rs/kio/src/coop.rs index 4f3eafc8135..99c6d0d4f54 100644 --- a/rs/kio/src/coop.rs +++ b/rs/kio/src/coop.rs @@ -91,7 +91,7 @@ impl Budget { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod tests { use std::{ sync::{ diff --git a/rs/kio/src/lib.rs b/rs/kio/src/lib.rs index c016d117704..2464989c9d9 100644 --- a/rs/kio/src/lib.rs +++ b/rs/kio/src/lib.rs @@ -29,12 +29,10 @@ use std::{ fmt, ops::{Deref, DerefMut}, + sync::atomic::AtomicUsize, }; -use crate::sync::AtomicUsize; - mod lock; -mod sync; mod waiter; pub mod coop; @@ -49,9 +47,7 @@ mod shared; mod task; mod weak; -#[cfg(all(test, loom))] -mod loom; -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod tests; #[cfg(feature = "macros")] @@ -108,7 +104,10 @@ mod heap { assert_eq!(arc_heap::(), layout::()); assert_eq!(arc_heap::(), layout::()); - assert_eq!(arc_heap::>>(), layout::>>()); + assert_eq!( + arc_heap::>>(), + layout::>>() + ); } } diff --git a/rs/kio/src/lock.rs b/rs/kio/src/lock.rs index a814a47f8bf..e5d52cf83d4 100644 --- a/rs/kio/src/lock.rs +++ b/rs/kio/src/lock.rs @@ -1,14 +1,9 @@ use std::{ fmt, ops::{Deref, DerefMut}, - // std, not `crate::sync`: [`WeakLock`] needs `Arc::downgrade`, which loom's Arc - // doesn't have. Loom still models the `Mutex` inside, which is the part that - // orders every handle against every other. - sync::{Arc, PoisonError, Weak}, + sync::{Arc, Mutex, MutexGuard, PoisonError, Weak}, }; -use crate::sync::{Mutex, MutexGuard}; - /// A cloneable mutex wrapper backed by `Arc>`. /// /// Every kio channel keeps its state in one of these, with its [`WaiterList`]s inside, @@ -34,12 +29,12 @@ pub struct Lock { inner: Arc>, } -// SAFETY: std and loom's Mutex are Send + Sync when T: Send, so this is +// SAFETY: Mutex is Send + Sync when T: Send, so this is // exactly Arc>'s derived bound. Stopping here keeps recursive channel // graphs from expanding the same Arc/Mutex proof at every level. unsafe impl Send for Lock {} -// SAFETY: Arc> has the same T: Send bound for Sync, with either mutex. +// SAFETY: Arc> has the same T: Send bound for Sync. unsafe impl Sync for Lock {} impl Lock { @@ -158,7 +153,7 @@ impl Default for WeakLock { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod tests { use super::*; diff --git a/rs/kio/src/log.rs b/rs/kio/src/log.rs index 29513d581e6..fad879f74f4 100644 --- a/rs/kio/src/log.rs +++ b/rs/kio/src/log.rs @@ -20,18 +20,20 @@ use std::{ alloc::{self, Layout}, + cell::UnsafeCell, fmt, mem::{MaybeUninit, offset_of}, panic::{RefUnwindSafe, UnwindSafe}, ptr::{self, NonNull}, - sync::atomic::Ordering, + sync::{ + Arc, + atomic::{AtomicPtr, AtomicU64, AtomicUsize, Ordering, fence}, + }, }; use crossbeam_epoch as epoch; use smallvec::SmallVec; -use crate::sync::{Arc, AtomicPtr, AtomicU64, AtomicUsize, UnsafeCell, fence}; - /// Slots in the first segment: one, since many logs never hold more. const FIRST_SEGMENT: usize = 1; @@ -69,10 +71,6 @@ struct Segment { owners: AtomicUsize, /// The segment after this one, set before anything is written into it. next: AtomicPtr>, - /// Read by every reader access and written by the free, so loom flags a reader that - /// reaches a segment the collector may have freed. - #[cfg(loom)] - alive: UnsafeCell<()>, slots: [Slot; 0], } @@ -106,8 +104,6 @@ impl Segment { taken: AtomicUsize::new(0), owners: AtomicUsize::new(1), next: AtomicPtr::new(ptr::null_mut()), - #[cfg(loom)] - alive: UnsafeCell::new(()), slots: [], }); for at in 0..capacity { @@ -137,10 +133,7 @@ impl Segment { /// `segment` stays live for `'a`. unsafe fn header<'a>(segment: NonNull) -> &'a Self { // SAFETY: live, per the caller. - let header = unsafe { segment.as_ref() }; - #[cfg(loom)] - header.alive.with(|_| ()); - header + unsafe { segment.as_ref() } } /// The written entry at `index`. @@ -153,8 +146,7 @@ impl Segment { // SAFETY: per the caller, the slot is in bounds, written, and never written again. unsafe { let at = (index - Self::header(segment).start) as usize; - let value = (*Self::slot(segment, at)).with(|slot| slot); - (*value).assume_init_ref() + (*(*Self::slot(segment, at)).get()).assume_init_ref() } } @@ -167,7 +159,7 @@ impl Segment { unsafe fn drop_entries(segment: NonNull, range: std::ops::Range) { for at in range { // SAFETY: written and unreachable, per the caller. - unsafe { (*Self::slot(segment, at)).with_mut(|value| (*value).assume_init_drop()) }; + unsafe { (*(*Self::slot(segment, at)).get()).assume_init_drop() }; } } @@ -205,23 +197,11 @@ impl Segment { return; } fence(Ordering::Acquire); + // The header holds only atomics and the slots `MaybeUninit`s whose entries are + // already dropped, so nothing needs dropping before the free. + let capacity = header.capacity; // SAFETY: the last owner, so nothing else uses the segment. - unsafe { - #[cfg(loom)] - header.alive.with_mut(|_| ()); - let capacity = header.capacity; - for at in 0..capacity { - let slot = Self::slot(segment, at).cast_mut(); - // A write access even to an empty slot, so loom flags any read racing it. - (*slot).with_mut(|_| ()); - ptr::drop_in_place(slot); - } - ptr::drop_in_place(segment.as_ptr()); - // Loom's cells are dropped above, so a racing access fails the model; the - // memory stays mapped so that failure is a report rather than a crash. - #[cfg(not(loom))] - alloc::dealloc(segment.as_ptr().cast(), Self::layout(capacity)); - } + unsafe { alloc::dealloc(segment.as_ptr().cast(), Self::layout(capacity)) }; } } @@ -354,7 +334,7 @@ impl Writer { let at = (end - header.start) as usize; // SAFETY: slot `at` is in bounds and past `filled`, so no reader reads it until the // stores below publish it, and nothing wrote it before. - unsafe { (*Segment::slot(tail, at)).with_mut(|slot| (*slot).write(value)) }; + unsafe { (*(*Segment::slot(tail, at)).get()).write(value) }; // Release so a reader, or the free, that sees the count sees the slot. header.filled.store(at + 1, Ordering::Release); self.shared.end.store(end + 1, Ordering::Release); @@ -652,7 +632,7 @@ impl Read<'_, T> { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use std::sync::{Arc as StdArc, Mutex}; diff --git a/rs/kio/src/loom.rs b/rs/kio/src/loom.rs deleted file mode 100644 index d5e39555a08..00000000000 --- a/rs/kio/src/loom.rs +++ /dev/null @@ -1,522 +0,0 @@ -//! Loom model checks for the kio channel primitives. -//! -//! kio is the shared state under every moq-net handle, so a lost wakeup or a -//! resurrected channel here surfaces as a stalled subscription several layers up. -//! These tests permute the thread interleavings exhaustively instead of hoping a -//! stress loop hits the bad one. -//! -//! Two failure modes are in scope: -//! -//! - **Lost wakeup**: a parked [`loom::future::block_on`] that is never woken leaves -//! every thread blocked, which loom reports as a deadlock. So "the assertion" for -//! these is simply that the test terminates. -//! - **Bad state**: a handle observing something the API promises it can't, asserted -//! directly. -//! -//! Run with `just rs loom`; `cfg(loom)` is never set in a normal build. - -use std::task::Poll; - -use loom::{ - future::block_on, - sync::{ - Arc, - atomic::{AtomicBool, Ordering}, - }, - thread, -}; - -use crate::{Closed, Lock, Producer, Queue, Ref, Shared, WaiterList, wait}; - -/// Ready once the value equals `n`. -fn equals(n: u32) -> impl FnMut(&Ref<'_, u32>) -> Poll<()> + Unpin { - move |v: &Ref<'_, u32>| if **v == n { Poll::Ready(()) } else { Poll::Pending } -} - -/// A write must reach a parked consumer no matter how it interleaves with the -/// producer drop that closes the channel behind it. -#[test] -fn write_wakes_a_parked_consumer() { - loom::model(|| { - let producer = Producer::new(0u32); - let consumer = producer.consume(); - - let writer = thread::spawn(move || { - *producer.write().ok().expect("open") = 1; - // The producer drops here, closing the channel right behind the write. - }); - - assert_eq!(block_on(consumer.wait(equals(1))), Ok(()), "the write was lost"); - writer.join().unwrap(); - }); -} - -/// `Mut::drop` drains the waiter list under the lock and wakes after releasing it. -/// Two writers racing through that window must still leave the consumer woken. -#[test] -fn concurrent_writes_never_lose_a_wakeup() { - loom::model(|| { - let producer = Producer::new(0u32); - let consumer = producer.consume(); - let second = producer.clone(); - - let a = thread::spawn(move || *producer.write().ok().expect("open") += 1); - let b = thread::spawn(move || *second.write().ok().expect("open") += 1); - - assert_eq!(block_on(consumer.wait(equals(2))), Ok(()), "a write was lost"); - a.join().unwrap(); - b.join().unwrap(); - }); -} - -/// Both producers race to be the last one out. Exactly one must run the close, and -/// the consumer parked on `closed()` must be woken by whichever it was. -#[test] -fn racing_last_producer_drops_still_close() { - loom::model(|| { - let producer = Producer::new(0u32); - let second = producer.clone(); - let consumer = producer.consume(); - - let a = thread::spawn(move || drop(producer)); - let b = thread::spawn(move || drop(second)); - - block_on(consumer.closed()); - a.join().unwrap(); - b.join().unwrap(); - }); -} - -/// `ProducerWeak::produce` promises `None` once the channel is closed, so a `Some` -/// must be a producer that can actually write. -#[test] -fn weak_upgrade_never_resurrects_a_closed_channel() { - loom::model(|| { - let producer = Producer::new(0u32); - let weak = producer.weak(); - - let closer = thread::spawn(move || drop(producer)); - - let upgraded = weak.produce(); - closer.join().unwrap(); - - if let Some(upgraded) = upgraded { - assert!( - upgraded.write().is_ok(), - "produce() handed back a producer on a closed channel" - ); - } - }); -} - -/// `Weak` owns nothing, not even the state allocation, so an upgrade races the -/// deallocation as well as the close. Same contract: a `Some` must be writable. -#[test] -fn weak_downgrade_upgrade_races_the_last_drop() { - loom::model(|| { - let producer = Producer::new(0u32); - let weak = producer.downgrade(); - - let closer = thread::spawn(move || drop(producer)); - - let upgraded = weak.upgrade(); - closer.join().unwrap(); - - if let Some(upgraded) = upgraded { - assert!( - upgraded.write().is_ok(), - "upgrade() handed back a producer on a closed channel" - ); - } - }); -} - -/// The first consumer appearing must wake a producer parked on `wanted_changed()`, however the -/// `fetch_add` interleaves with the waiter registration. -#[test] -fn first_consumer_wakes_wanted_changed() { - loom::model(|| { - let producer = Producer::new(0u32); - let second = producer.clone(); - - // The consumer rides back on the join handle so it outlives the `wanted_changed()` poll. - let maker = thread::spawn(move || second.consume()); - - assert_eq!( - block_on(producer.wanted_changed(false)), - Ok(true), - "the new consumer was missed" - ); - drop(maker.join().unwrap()); - }); -} - -/// The mirror image: the last consumer leaving must wake a producer parked on -/// `wanted_changed()`. -#[test] -fn last_consumer_wakes_wanted_changed() { - loom::model(|| { - let producer = Producer::new(0u32); - let consumer = producer.consume(); - - let dropper = thread::spawn(move || drop(consumer)); - - assert_eq!( - block_on(producer.wanted_changed(true)), - Ok(false), - "the last drop was missed" - ); - dropper.join().unwrap(); - }); -} - -/// A consumer created and dropped while `wanted_changed()` is parked must not leave it -/// parked on a stale count. -#[test] -fn consumer_churn_resolves_unwanted() { - loom::model(|| { - let producer = Producer::new(0u32); - let second = producer.clone(); - - let churn = thread::spawn(move || drop(second.consume())); - - assert_eq!( - block_on(producer.wanted_changed(true)), - Ok(false), - "wanted_changed() stalled on churn" - ); - churn.join().unwrap(); - }); -} - -/// The invariant `write_unwanted` exists for: a consumer and a teardown racing must -/// never both win. Either the consumer is minted and the teardown is declined, or -/// the teardown closes the channel and the weak handle mints nothing. -/// -/// Without the count moving under the state lock, the "closed and minted" corner -/// is reachable: the bump lands after the teardown read zero, and the consumer is -/// left holding a channel that was torn down for being unwanted. -#[test] -fn a_teardown_and_a_consumer_never_both_win() { - loom::model(|| { - let producer = Producer::new(0u32); - let weak = producer.weak(); - - let consumer = thread::spawn(move || weak.try_consume()); - - let committed = match producer.write_unwanted() { - crate::Unwanted::Idle(guard) => { - guard.close(); - true - } - crate::Unwanted::Wanted => false, - crate::Unwanted::Closed => unreachable!("nothing else closes this channel"), - }; - - let consumer = consumer.join().unwrap(); - assert!( - !(committed && consumer.is_some()), - "the teardown cancelled a consumer that had already been handed out" - ); - assert!( - committed || consumer.is_some(), - "the consumer was refused by a teardown that never happened" - ); - }); -} - -/// `Shared` has no producer/consumer split: any handle can mutate, and every -/// mutation must wake every other handle parked on a predicate. -#[test] -fn shared_mutation_wakes_a_parked_handle() { - loom::model(|| { - let shared = Shared::new(0u32); - let other = shared.clone(); - - let writer = thread::spawn(move || *other.lock() = 1); - - drop(block_on(shared.wait(equals(1)))); - writer.join().unwrap(); - }); -} - -/// A push must reach a pop parked on an empty queue no matter how the two -/// interleave. -#[test] -fn queue_push_wakes_a_parked_pop() { - loom::model(|| { - let queue = Queue::new(); - let pusher = queue.clone(); - - let push = thread::spawn(move || pusher.try_push(1u32).unwrap()); - - assert_eq!(block_on(queue.pop()), Ok(1), "the push was lost"); - push.join().unwrap(); - }); -} - -/// A pop freeing the only slot of a bounded queue must wake a push parked on it. -#[test] -fn queue_pop_wakes_a_parked_push() { - loom::model(|| { - let queue = Queue::bounded(1); - queue.try_push(1u32).unwrap(); - let popper = queue.clone(); - - let pop = thread::spawn(move || assert_eq!(popper.try_pop().unwrap(), Some(1))); - - assert_eq!(block_on(queue.push(2)), Ok(()), "the freed slot was missed"); - pop.join().unwrap(); - }); -} - -/// A close racing a parked pop must still wake it; drained-then-closed is the -/// only acceptable outcome. -#[test] -fn queue_close_wakes_a_parked_pop() { - loom::model(|| { - let queue = Queue::::new(); - let closer = queue.clone(); - - let close = thread::spawn(move || closer.close()); - - assert_eq!(block_on(queue.pop()), Err(Closed), "the close was lost"); - close.join().unwrap(); - }); -} - -/// Drain the list under its lock, then wake it outside, as the channels do. -fn wake(list: &Lock) { - let mut waiters = list.lock().take(); - waiters.wake(); -} - -/// A partial wake re-polls a waiter still parked on the quiet list. Its registration -/// on the drained list must be real, not skipped as a duplicate, or the terminal -/// wake is lost and the task hangs. -#[test] -fn a_replaced_waiter_hears_the_drained_list_again() { - loom::model(|| { - let woken = Lock::new(WaiterList::new()); - let parked = Lock::new(WaiterList::new()); - let done = Arc::new(AtomicBool::new(false)); - - let waker = { - let woken = woken.clone(); - let done = done.clone(); - - thread::spawn(move || { - // Drains `woken`; a re-poll now re-registers while still parked on - // `parked`. - wake(&woken); - done.store(true, Ordering::SeqCst); - // Must reach the re-registered waiter, or the model deadlocks. - wake(&woken); - }) - }; - - // Register before reading `done`: the other order has a window where the - // store and both wakes land between the read and the registration. - block_on(wait(|waiter| { - waiter.register(&mut woken.lock()); - waiter.register(&mut parked.lock()); - match done.load(Ordering::SeqCst) { - true => Poll::Ready(()), - false => Poll::Pending, - } - })); - - waker.join().unwrap(); - }); -} - -#[test] -fn consumer_creation_cannot_cross_an_idle_guard() { - loom::model(|| { - let producer = Producer::new(0u32); - let existing = producer.consume(); - let weak = producer.weak(); - let minting = thread::spawn(move || weak.consume()); - drop(existing); - if let crate::Unwanted::Idle(guard) = producer.write_unwanted() { - thread::yield_now(); - assert!(!producer.wanted(), "consumer appeared while idle guard was held"); - guard.close(); - } - drop(minting.join().unwrap()); - }); -} - -/// Ready once the gate opens. -fn gated(gate: crate::Consumer) -> impl FnMut(&crate::Waiter) -> Poll<()> + Unpin { - move |waiter| match gate.poll(waiter, |open| if **open { Poll::Ready(()) } else { Poll::Pending }) { - Poll::Ready(_) => Poll::Ready(()), - Poll::Pending => Poll::Pending, - } -} - -/// The owner's poll wakes one of its own tasks, holding that wake of itself until it returns, -/// while another thread wakes the other task. Neither wake may be lost, whether the -/// owner's poll takes its own wake in the same pass or returns first. -#[test] -fn a_held_wake_never_hides_a_remote_wake() { - for same_pass in [false, true] { - loom::model(move || { - let local = Producer::new(false); - let remote = Producer::new(false); - let mut tasks = crate::Tasks::new(); - tasks.push(gated(local.consume())); - tasks.push(gated(remote.consume())); - - let opener = thread::spawn(move || *remote.write().ok().expect("open") = true); - - let mut first = true; - block_on(wait(move |waiter| { - waiter.batch(|| { - if std::mem::take(&mut first) { - let _ = tasks.poll(waiter); - *local.write().ok().expect("open") = true; - if !same_pass { - return Poll::Pending; - } - } - tasks.poll(waiter) - }) - })); - opener.join().unwrap(); - }); - } -} - -/// An entry appended under a channel's write guard reaches a reader that checks the -/// log first and parks on the channel only when nothing is committed, the pairing the -/// log's docs prescribe. The reader either reads the entry or is woken by its guard. -#[test] -fn a_log_append_wakes_a_reader_parked_on_its_channel() { - loom::model(|| { - let producer = Producer::new(crate::log::Writer::new(0)); - let consumer = producer.consume(); - let mut reader = producer.read().reader(); - - let writer = thread::spawn(move || { - let mut log = producer.write().ok().expect("open"); - log.push(7u32); - log.push(8); - }); - - let mut index = 0; - let mut read = Vec::new(); - block_on(wait(|waiter| { - { - let mut log = reader.read(); - while let Ok(Some(value)) = log.get(index) { - read.push(*value); - index += 1; - } - } - if index == 2 { - return Poll::Ready(()); - } - // Re-checked under the channel's lock, which every append holds. - let committed = |log: &Ref<'_, crate::log::Writer>| match log.end() > index { - true => Poll::Ready(()), - false => Poll::Pending, - }; - consumer.poll(waiter, committed).map(|_| ()) - })); - let mut log = reader.read(); - while let Ok(Some(value)) = log.get(index) { - read.push(*value); - index += 1; - } - assert_eq!(read, [7, 8], "an append was lost or misread"); - writer.join().unwrap(); - }); -} - -/// A reader racing front eviction reads each entry correctly or learns it was evicted, -/// and never touches a segment the collector freed: the free writes every slot, so a -/// read it isn't ordered after fails the model. The reader keeps a hint into the first -/// segment from an earlier read, which the eviction may free before its next one. -/// -/// Only an ARM run checks the release at the end of each read: off x86, crossbeam-epoch -/// pins with a Relaxed store, so a collector that sees the reader's second pin is -/// ordered after its first pin's reads only through that release. On x86 its SeqCst CAS -/// orders them anyway. -/// -/// The collector's own atomics run under loom too (`crossbeam_loom`), which makes the -/// full search intractable, so this one bounds preemptions like moq-net's models. -#[test] -fn a_log_reader_racing_eviction_never_touches_freed_memory() { - let mut builder = loom::model::Builder::new(); - builder.preemption_bound.get_or_insert(3); - builder.check(|| { - let mut writer = crate::log::Writer::new(0); - for i in 0..2u64 { - writer.push(i); - } - let mut reader = writer.reader(); - assert_eq!(reader.read().get(0), Ok(Some(&0))); - - let racer = thread::spawn(move || { - for i in [0, 1] { - match reader.read().get(i) { - Ok(Some(value)) => assert_eq!(*value, i, "misread entry {i}"), - Ok(None) => panic!("entry {i} was committed"), - Err(_) => {} - } - } - }); - - writer.evict(1); - writer.evict(2); - // Advance the collector so it may free the segments while the reader runs. - for _ in 0..2 { - crossbeam_epoch::pin().flush(); - } - racer.join().unwrap(); - }); -} - -/// An entry whose drop writes its cell, so loom flags a read racing it. -struct Tracked(loom::cell::UnsafeCell); - -// SAFETY: loom checks every access to the cell. -unsafe impl Send for Tracked {} -unsafe impl Sync for Tracked {} - -impl Drop for Tracked { - fn drop(&mut self) { - self.0.with_mut(|value| unsafe { *value = u64::MAX }); - } -} - -/// A read racing an eviction inside a segment that stays live: the evicted entry is -/// dropped only once the read is done with it, never while it borrows it. -#[test] -fn a_log_reader_racing_a_partial_eviction_never_touches_a_dropped_entry() { - let mut builder = loom::model::Builder::new(); - builder.preemption_bound.get_or_insert(3); - builder.check(|| { - let mut writer = crate::log::Writer::new(0); - // Segments of 1 and 2 slots: entry 1 shares the second with entry 2. - for i in 0..3u64 { - writer.push(Tracked(loom::cell::UnsafeCell::new(i))); - } - let mut reader = writer.reader(); - - let racer = thread::spawn(move || { - if let Ok(Some(entry)) = reader.read().get(1) { - assert_eq!(entry.0.with(|value| unsafe { *value }), 1, "misread entry 1"); - } - }); - - writer.evict(2); - // Retire the segment too, so its free races the pending drop of entry 1. - writer.evict(3); - for _ in 0..2 { - crossbeam_epoch::pin().flush(); - } - racer.join().unwrap(); - }); -} diff --git a/rs/kio/src/pending.rs b/rs/kio/src/pending.rs index 08d07eb3723..5a34d0dbb36 100644 --- a/rs/kio/src/pending.rs +++ b/rs/kio/src/pending.rs @@ -58,7 +58,7 @@ impl Future for Pending

{ } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use super::*; use crate::{Producer, Waiter}; diff --git a/rs/kio/src/producer.rs b/rs/kio/src/producer.rs index 58c90cf9ba7..3e4c39aa46a 100644 --- a/rs/kio/src/producer.rs +++ b/rs/kio/src/producer.rs @@ -1,6 +1,6 @@ use std::{ ops::{Deref, DerefMut}, - sync::atomic::Ordering, + sync::{Arc, atomic::Ordering}, task::Poll, }; @@ -8,7 +8,6 @@ use crate::{ Closed, Counts, State, consumer::{self, Consumer}, lock::*, - sync::Arc, waiter::*, weak::{ProducerWeak, Weak}, }; @@ -48,7 +47,7 @@ impl Producer { /// channel costs this whether it has one consumer or a thousand. /// /// Excludes whatever `T` allocates on its own. - pub const HEAP: usize = crate::arc_heap::>>() + crate::arc_heap::(); + pub const HEAP: usize = crate::arc_heap::>>() + crate::arc_heap::(); /// Create a new producer with the given initial value. pub fn new(value: T) -> Self { @@ -429,7 +428,7 @@ impl Deref for Ref<'_, T> { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use super::*; diff --git a/rs/kio/src/queue.rs b/rs/kio/src/queue.rs index 1e2202d6229..fc322f40375 100644 --- a/rs/kio/src/queue.rs +++ b/rs/kio/src/queue.rs @@ -262,7 +262,7 @@ impl Clone for Queue { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use std::{ sync::{ diff --git a/rs/kio/src/shared.rs b/rs/kio/src/shared.rs index 41a7bd3d85a..9027d4fb8f9 100644 --- a/rs/kio/src/shared.rs +++ b/rs/kio/src/shared.rs @@ -108,7 +108,7 @@ impl Clone for Shared { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use std::{ future::Future, diff --git a/rs/kio/src/sync.rs b/rs/kio/src/sync.rs deleted file mode 100644 index 37402a6a64f..00000000000 --- a/rs/kio/src/sync.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! The synchronization primitives kio is built on, swappable for loom's. -//! -//! Under `--cfg loom` these resolve to `loom::sync`, which records every atomic -//! and lock operation so the model checker can permute the interleavings. Normal -//! builds get `std::sync` and this module compiles away. -//! -//! Loom's `Arc` has no `downgrade`, so the two places that need a `Weak` keep std's: -//! [`crate::lock`] (for `WeakLock`) and [`crate::waiter`] (whose list holds weak waker -//! handles). Both still hold loom's `Mutex`, which is what orders one handle against -//! another; only the refcount itself goes unpermuted. The `Arc` that carries -//! the producer/consumer counts is loom's. -//! -//! [`crate::log`] frees segments through `crossbeam-epoch`, which `--cfg crossbeam_loom` -//! puts under loom as well. - -#[cfg(loom)] -pub(crate) use loom::{ - cell::UnsafeCell, - sync::{ - Arc, Mutex, MutexGuard, - atomic::{AtomicBool, AtomicPtr, AtomicU64, AtomicUsize, fence}, - }, -}; -#[cfg(not(loom))] -pub(crate) use std::sync::{ - Arc, Mutex, MutexGuard, - atomic::{AtomicBool, AtomicPtr, AtomicU64, AtomicUsize, fence}, -}; - -/// `std::cell::UnsafeCell` behind loom's closure API, so loom can check every access. -#[cfg(not(loom))] -#[derive(Debug)] -pub(crate) struct UnsafeCell(std::cell::UnsafeCell); - -#[cfg(not(loom))] -impl UnsafeCell { - pub(crate) const fn new(value: T) -> Self { - Self(std::cell::UnsafeCell::new(value)) - } - - pub(crate) fn with(&self, f: impl FnOnce(*const T) -> R) -> R { - f(self.0.get()) - } - - pub(crate) fn with_mut(&self, f: impl FnOnce(*mut T) -> R) -> R { - f(self.0.get()) - } -} diff --git a/rs/kio/src/task.rs b/rs/kio/src/task.rs index 50c9eb1e42b..67da55b2e94 100644 --- a/rs/kio/src/task.rs +++ b/rs/kio/src/task.rs @@ -41,15 +41,14 @@ use std::{ cell::{Cell, RefCell}, - // std's `Arc`: `Waker::from` needs it. The atomics are loom's under `--cfg loom`. - sync::{Arc, atomic::Ordering}, + sync::{ + Arc, Mutex, + atomic::{AtomicBool, AtomicU64, Ordering}, + }, task::{Context, Poll, RawWakerVTable, Wake, Waker}, }; -use crate::{ - sync::{AtomicBool, AtomicU64, Mutex}, - waiter::{Park, Waiter}, -}; +use crate::waiter::{Park, Waiter}; /// Wake-bits per chunk word and words per chunk: one chunk covers 1024 slots. const WORD_BITS: usize = 64; @@ -146,15 +145,9 @@ impl Shared { } } -// Loom runs its threads on one OS thread, so the scope must be loom's to stay per thread. -#[cfg(not(loom))] std::thread_local! { static POLLING: Polling = const { Polling { current: Cell::new(None), held: RefCell::new(Vec::new()) } }; } -#[cfg(loom)] -loom::thread_local! { - static POLLING: Polling = Polling { current: Cell::new(None), held: RefCell::new(Vec::new()) }; -} /// A waker's identity: what [`Waker::will_wake`] compares. type Identity = (*const (), *const RawWakerVTable); @@ -480,7 +473,7 @@ impl Default for Tasks { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod tests { use super::*; use std::sync::{Mutex, atomic::AtomicUsize}; diff --git a/rs/kio/src/tests.rs b/rs/kio/src/tests.rs index a41d78c8750..3083ef17c70 100644 --- a/rs/kio/src/tests.rs +++ b/rs/kio/src/tests.rs @@ -194,8 +194,7 @@ fn write_unwanted_reports_wanted_closed_and_idle() { /// The deal the guard makes: a consumer minted before it declines the teardown, /// and one asked for after the teardown committed is never handed out. Single -/// threaded here, so this pins the ordering rather than the race; the -/// interleavings are `loom.rs`'s job. +/// threaded here, so this pins the ordering rather than the race. #[test] fn a_consumer_minted_before_the_guard_declines_it() { let producer = Producer::new(0u32); diff --git a/rs/kio/src/waiter.rs b/rs/kio/src/waiter.rs index 1d26af18930..27ea9769ae5 100644 --- a/rs/kio/src/waiter.rs +++ b/rs/kio/src/waiter.rs @@ -2,7 +2,6 @@ use std::{ fmt, future::Future, pin::Pin, - // std, not `crate::sync`: loom's Arc has no `downgrade`. See `sync.rs`. sync::{ Arc, OnceLock, Weak, atomic::{AtomicBool, AtomicU64, Ordering}, @@ -432,7 +431,7 @@ where crate::Pending::new(poll) } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod tests { use super::*; diff --git a/rs/kio/src/weak.rs b/rs/kio/src/weak.rs index a2764b3a9fd..0b1680fff9e 100644 --- a/rs/kio/src/weak.rs +++ b/rs/kio/src/weak.rs @@ -1,11 +1,13 @@ -use std::{sync::atomic::Ordering, task::Poll}; +use std::{ + sync::{Arc, atomic::Ordering}, + task::Poll, +}; use crate::{ Closed, Counts, State, consumer::Consumer, lock::*, producer::{Producer, Ref}, - sync::Arc, waiter::*, }; @@ -301,7 +303,7 @@ impl Clone for ConsumerWeak { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use super::*; diff --git a/rs/moq-net/AGENTS.md b/rs/moq-net/AGENTS.md index 41b445931fc..89d798d36a7 100644 --- a/rs/moq-net/AGENTS.md +++ b/rs/moq-net/AGENTS.md @@ -18,4 +18,3 @@ The wire layer. Generic over the transport and media-agnostic: the relay never i - Tests never use tokio or the wall clock. Prefer a plain `#[test]` that polls with explicit instants; async bodies use `#[moq_clock::test]`, whose simulated time jumps only when every task is stalled. tokio is for the benches. - `just rs fuzz ` (one per file in `fuzz/fuzz_targets/`) needs nightly. The target bodies live in `src/fuzz.rs`; `fuzz/regressions//` replays under `just check`, so commit every crash input there. -- `just rs loom` model-checks the concurrent handoffs. A hang is a lost wakeup, not a flake. diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index 9ed0095737a..ddaff4c7a5f 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -44,11 +44,6 @@ tracing = { workspace = true } url = { workspace = true } uuid = { workspace = true } -# Model-layer locks must yield to Loom whenever kio does. This is compiled only -# by `just rs loom`, never in production builds. -[target.'cfg(loom)'.dependencies] -loom = { workspace = true } - [dev-dependencies] criterion = { package = "bench-harness", path = "../bench-harness" } futures = { workspace = true } diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index 522df4e532f..7fa0d90f905 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -1947,7 +1947,7 @@ impl Drop for RequestVerdict { /// Clones share the grant: the request's [`until`] watches it end while the request renews it. /// Ending a request grant never touches the session: only that one request ends. #[derive(Clone)] -pub(crate) struct RequestGrant(std::sync::Arc>); +pub(crate) struct RequestGrant(std::sync::Arc>); struct Held { verdict: RequestVerdict, @@ -2033,7 +2033,7 @@ impl RequestGrant { } let mut deadline = crate::time::Deadline::new(clock); deadline.set(grant.expires); - Ok(Self(std::sync::Arc::new(crate::sync::Mutex::new(Held { + Ok(Self(std::sync::Arc::new(std::sync::Mutex::new(Held { verdict, grant, deadline, @@ -2046,7 +2046,7 @@ impl RequestGrant { })))) } - fn held(&self) -> crate::sync::MutexGuard<'_, Held> { + fn held(&self) -> std::sync::MutexGuard<'_, Held> { self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) } diff --git a/rs/moq-net/src/lib.rs b/rs/moq-net/src/lib.rs index 9f85c082fe7..ff460ebf788 100644 --- a/rs/moq-net/src/lib.rs +++ b/rs/moq-net/src/lib.rs @@ -123,7 +123,6 @@ pub mod setup; #[cfg(not(feature = "async"))] #[allow(dead_code, unused_imports, reason = "async-only")] mod setup; -mod sync; #[cfg(feature = "async")] mod tail; #[cfg(all(test, feature = "async"))] diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 1d7a3737331..cc7a76e29c4 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -5004,7 +5004,7 @@ impl GroupServe { /// A group that fails mid-stream must reset with its own error code. The subscriber uses /// that code to tell a truncated group (Old, Lagged, Evicted) from a routine cancel, so a /// blanket [`Error::Cancel`] from the writer's drop fallback loses the reason. -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod serve_group_test { use super::*; use crate::lite::test_transport::*; diff --git a/rs/moq-net/src/model/cache.rs b/rs/moq-net/src/model/cache.rs index 4db0626c7b8..ceb63b1aa50 100644 --- a/rs/moq-net/src/model/cache.rs +++ b/rs/moq-net/src/model/cache.rs @@ -40,9 +40,9 @@ //! [`DEFAULT_EXPIRY`], while a relay creates one configured pool and shares it across //! every origin so the whole process caches into a single policy. -use crate::sync::Mutex; use crate::util::SendCell; use std::collections::BTreeMap; +use std::sync::Mutex; use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; use std::sync::{Arc, OnceLock, PoisonError, Weak}; use std::task::{Poll, ready}; diff --git a/rs/moq-net/src/model/clock.rs b/rs/moq-net/src/model/clock.rs index 4545f5ac68d..711cc475444 100644 --- a/rs/moq-net/src/model/clock.rs +++ b/rs/moq-net/src/model/clock.rs @@ -23,18 +23,11 @@ pub(super) fn now() -> crate::time::Instant { /// /// The ambient runtime's clock where there is one, so a paused test runtime pauses /// staleness along with the drivers it supplies time to. -#[cfg(not(any(test, loom)))] +#[cfg(not(test))] pub(super) fn wall() -> crate::time::Instant { moq_clock::now() } -/// Loom permutes handoffs without advancing time, so scheduling cannot age groups. -#[cfg(all(loom, not(test)))] -pub(super) fn wall() -> crate::time::Instant { - static FROZEN: LazyLock = LazyLock::new(moq_clock::now); - *FROZEN -} - /// Unit tests freeze the wall clock, so only time a test supplies moves it: the /// simulated clock inside `moq_clock::sim`, or a pool stepped by hand outside it. #[cfg(test)] diff --git a/rs/moq-net/src/model/datagram.rs b/rs/moq-net/src/model/datagram.rs index a0350b763f4..d09ff1edb63 100644 --- a/rs/moq-net/src/model/datagram.rs +++ b/rs/moq-net/src/model/datagram.rs @@ -24,7 +24,7 @@ use bytes::Bytes; use crate::Timestamp; use crate::cache; -use crate::sync::Mutex; +use std::sync::Mutex; /// Hard ceiling on a datagram payload, matching the QUIC DATAGRAM frame limit. /// diff --git a/rs/moq-net/src/model/expiry.rs b/rs/moq-net/src/model/expiry.rs index 2be67f72b37..7ee947d8839 100644 --- a/rs/moq-net/src/model/expiry.rs +++ b/rs/moq-net/src/model/expiry.rs @@ -4,14 +4,9 @@ //! frame write advances, and the wall clock, which the cache pool's driver advances. use std::collections::BTreeMap; -use std::sync::atomic::Ordering; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering, fence}; use std::time::Duration; -#[cfg(loom)] -use ::loom::sync::atomic::{AtomicBool, AtomicU64, fence}; -#[cfg(not(loom))] -use std::sync::atomic::{AtomicBool, AtomicU64, fence}; - use super::Timestamp; /// A track's parked group reads, indexed by the events that can expire them, so a frame @@ -362,7 +357,7 @@ impl Wakes { } } -#[cfg(all(test, not(loom)))] +#[cfg(test)] mod test { use super::*; diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 7909ef1450c..ce60949c0e9 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -753,7 +753,7 @@ enum Held { /// /// Anchored like [`Held`], on the first clock reading after the change. #[derive(Clone)] -struct Release(Arc>); +struct Release(Arc>); struct ReleaseState { held: Held, @@ -771,7 +771,7 @@ impl Release { return None; } let now = clock.now(); - Some(Self(Arc::new(crate::sync::Mutex::new(ReleaseState { + Some(Self(Arc::new(std::sync::Mutex::new(ReleaseState { held: Held::Anchoring(now), hold, clock: clock.clone(), @@ -10361,7 +10361,7 @@ mod tests { .unwrap(); // The replacement serves the whole group to a fetch, frames still to come included. let fetches = request.requests(); - let fetched = Arc::new(crate::sync::Mutex::new(Vec::::new())); + let fetched = Arc::new(std::sync::Mutex::new(Vec::::new())); let serving = fetched.clone(); let _fetching = moq_clock::sim::spawn(async move { while let Ok(request) = fetches.requested_fill_group().await { diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 480bf042032..f97c95abcb2 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -36,11 +36,11 @@ //! failure, once no copy it replaced may still deliver it. A route change discards what the //! former serving copy gave up and the reader had yet to report. -use crate::sync::{Mutex, MutexGuard}; use rangemap::{RangeMap, RangeSet}; use std::collections::{BTreeSet, VecDeque}; use std::ops::Bound; use std::sync::{Arc, PoisonError, Weak}; +use std::sync::{Mutex, MutexGuard}; use std::task::{Poll, ready}; use std::time::Duration; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index b17660de935..fb451d50464 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -13,9 +13,9 @@ //! //! The track is closed with [Error] when all writers or readers are dropped. -use crate::sync::Mutex; use crate::{Error, Result, StreamError, Timescale, Timestamp, coding}; use crate::{broadcast, cache, group, stats}; +use std::sync::Mutex; use super::Datagram; use super::delivered::Delivered; diff --git a/rs/moq-net/src/sync.rs b/rs/moq-net/src/sync.rs deleted file mode 100644 index e0266d790da..00000000000 --- a/rs/moq-net/src/sync.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Mutexes that let Loom schedule a contending thread instead of blocking its executor. - -#[cfg(loom)] -pub(crate) use loom::sync::{Mutex, MutexGuard}; -#[cfg(not(loom))] -pub(crate) use std::sync::{Mutex, MutexGuard}; diff --git a/rs/moq-net/tests/expiry-loom.rs b/rs/moq-net/tests/expiry-loom.rs deleted file mode 100644 index b57247d2364..00000000000 --- a/rs/moq-net/tests/expiry-loom.rs +++ /dev/null @@ -1,71 +0,0 @@ -#![cfg(loom)] - -// Exercise the private primitive with Loom's atomics without exporting test-only API. -use moq_net::Timestamp; -mod time { - pub use moq_net::time::Instant; -} -#[allow(dead_code)] -#[path = "../src/model/expiry.rs"] -mod expiry; - -mod loom { - use super::expiry::Wakes; - use ::loom::sync::{Arc, RwLock}; - use ::loom::{future::block_on, thread}; - use std::task::Poll; - - fn insertion_race(held: bool) { - ::loom::model(move || { - let shared = Arc::new((Wakes::default(), RwLock::new(false))); - let writer = shared.clone(); - let publisher = thread::spawn(move || { - let mut track = writer.1.write().unwrap(); - *track = true; - if writer.0.land(1) { - writer.0.landed(None, 1, 0); - } - }); - block_on(kio::wait(|waiter| { - let track = shared.1.read().unwrap(); - if held { - shared.0.watch_held(0, waiter); - } else { - let _ = shared.0.watch_landing(0, waiter); - } - if *track { Poll::Ready(()) } else { Poll::Pending } - })); - publisher.join().unwrap(); - }); - } - - #[test] - fn insertion_observes_a_registered_reader_or_the_reader_observes_it() { - insertion_race(false); - } - - #[test] - fn insertion_observes_a_registered_held_reader_or_the_reader_observes_it() { - insertion_race(true); - } - - /// A group's expiry judges it without the track lock: the insert publishes the landing - /// and the read registers, each before checking the other. - #[test] - fn insertion_wakes_a_lock_free_reader_or_the_reader_sees_it() { - ::loom::model(|| { - let wakes = Arc::new(Wakes::default()); - let writer = wakes.clone(); - let publisher = thread::spawn(move || { - if writer.land(1) { - writer.landed(None, 1, 0); - } - }); - block_on(kio::wait(|waiter| match wakes.watch_landing(0, waiter) { - true => Poll::Ready(()), - false => Poll::Pending, - })); - publisher.join().unwrap(); - }); - } -} diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 8438692c8bb..9f756677753 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -3,9 +3,9 @@ //! Tests needing JSON consumers or the hidden `fuzz` module live in `moq-net-tests`. //! Each `tests/*.rs` file is its own crate that links this package and its //! dependencies again, so add new integration tests here as a module. -//! `loom.rs` and `send.rs` stay apart because they build for another cfg or -//! toolchain. `group_charge.rs` stays apart because its counting global -//! allocator would apply to every test in the binary. +//! `send.rs` stays apart because it builds on another toolchain. +//! `group_charge.rs` stays apart because its counting global allocator would +//! apply to every test in the binary. mod announce_covering_route; mod announce_takeover; diff --git a/rs/moq-net/tests/loom.rs b/rs/moq-net/tests/loom.rs deleted file mode 100644 index ea0d7e6cd96..00000000000 --- a/rs/moq-net/tests/loom.rs +++ /dev/null @@ -1,672 +0,0 @@ -#![cfg(feature = "async")] - -//! Loom model checks for the model layer (broadcast -> track -> group -> frame). -//! -//! The unit tests drive these handles from one task at a time, so they can only -//! catch logic bugs. In production a publisher writes on one thread while a -//! session driver reads on another, and the handoff is kio's `Arc>` -//! plus a waker list. These tests hand each side to its own loom thread and let -//! loom permute the interleavings. -//! -//! A lost wakeup shows up as loom reporting a deadlock (every thread parked, none -//! runnable), so "the test terminates" is itself the assertion. Anything else is -//! asserted directly. -//! -//! The model mutexes and kio's primitives use Loom's locks, so a contending thread -//! yields to its scheduler. Reference counts that need weak pointers and moq-net's -//! bare atomic counters still use std: these models cover the locking and wakeup -//! handoffs, not every possible reordering of the cache's counters. -//! -//! Unlike kio's own models, these are bounded (see [`model`]): each one composes -//! dozens of kio operations, so an exhaustive search grows exponentially with every -//! lock or refcount the model layer adds to a path. -//! -//! Run with `just rs loom`; the whole file compiles away without `cfg(loom)`. -#![cfg(loom)] - -use bytes::Bytes; -use loom::{future::block_on, thread}; -use moq_net::{Error, Hop, Timestamp, broadcast, cache, origin}; - -/// A millisecond track: the frames written here carry timestamps. -fn timed() -> moq_net::track::Info { - moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI) -} - -/// Preemptions per execution unless `LOOM_MAX_PREEMPTIONS` overrides it. Loom's own -/// guidance is that 2 or 3 catches most bugs, and each extra level multiplies the -/// search several times over. Unbounded, `back_to_back_groups_arrive_in_order` no -/// longer finishes within an hour. -const PREEMPTION_BOUND: usize = 3; - -/// [`loom::model`] with a preemption bound, so the search stays polynomial in the -/// number of operations on a path rather than exponential. -fn model(f: F) { - let mut builder = loom::model::Builder::new(); - // The origin models include setup and teardown in each execution. Their finite - // paths exceed Loom's default 1,000 branches; this raises the execution budget, - // not the number of schedules or preemptions explored. - if std::env::var_os("LOOM_MAX_BRANCHES").is_none() { - builder.max_branches = 10_000; - } - builder.preemption_bound.get_or_insert(PREEMPTION_BOUND); - builder.check(f); -} - -/// A frame written on the publisher thread must reach a subscriber parked on -/// `next_frame`, however the write interleaves with the reader's parking. -#[test] -fn frame_reaches_a_parked_subscriber() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let track = broadcast.create_track("video", timed()).expect("create track"); - // Subscribed before the publisher runs: a bare handle is no demand, so a track - // finished and dropped before the subscribe would already be gone. - let subscribing = consumer.track("video").expect("track").subscribe(None); - - let publisher = thread::spawn(move || { - let mut group = track.append_group().expect("append group"); - group - .write_frame(Timestamp::ZERO, Bytes::from_static(b"frame")) - .expect("write frame"); - group.finish().expect("finish group"); - track.finish().expect("finish track"); - }); - - let mut subscriber = block_on(subscribing).expect("subscribe"); - let mut group = block_on(subscriber.recv_group()) - .expect("recv group") - .expect("a group was published"); - let frame = block_on(group.read_frame()).expect("read frame").expect("a frame"); - assert_eq!(frame.payload, Bytes::from_static(b"frame")); - - publisher.join().unwrap(); - }); -} - -/// Two groups written back to back must both reach the subscriber in order. This is -/// the path where the producer takes the waiter list for group 1 and wakes outside -/// the lock while group 2 is already being written. -/// -/// The subscription retains the backlog from group 0: a floor alone does not keep -/// superseded groups within a zero delay budget. -#[test] -fn back_to_back_groups_arrive_in_order() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let track = broadcast.create_track("video", None).expect("create track"); - let from_start = moq_net::track::Subscription::default() - .with_range(0..) - .with_max_delay(std::time::Duration::MAX); - let subscribing = consumer.track("video").expect("track").subscribe(from_start); - - let publisher = thread::spawn(move || { - for _ in 0..2 { - let mut group = track.append_group().expect("append group"); - group.finish().expect("finish group"); - } - track.finish().expect("finish track"); - }); - - let mut subscriber = block_on(subscribing).expect("subscribe"); - let first = block_on(subscriber.recv_group()).expect("recv").expect("first group"); - let second = block_on(subscriber.recv_group()).expect("recv").expect("second group"); - assert!(second.sequence > first.sequence, "groups arrived out of order"); - - publisher.join().unwrap(); - }); -} - -/// A track scan reads the group's abort through a mirrored flag rather than the -/// group's state, so the two can disagree. The direction that disagreement is allowed -/// to run is the invariant: the mirror may lag the state (a scan hands out a group -/// that is already aborting, and its consumer surfaces the abort), but it may never -/// lead it (a scan drops a group a reader could still drain). -/// -/// So the racing scan asserts against the group's *own state*, not against the flag it -/// just read: whenever the scan skips the group, a consumer must agree the group is -/// aborted. Only the abort transition takes the loom-backed group lock while delivery -/// takes the loom-backed track lock, which is what lets loom permute the two sides. -/// -/// The mirror itself is a bare `std` atomic, which loom does not instrument (see the -/// module docs). It cannot be: any cross-thread look at the group's state has to take -/// the group lock the abort is holding, so a store moved earlier *within* that guard -/// is unobservable by construction. What is observable, and what this pins down, is a -/// store escaping the guard entirely. -#[test] -fn group_abort_flag_never_leads_the_group_state() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let track = broadcast.create_track("video", None).expect("create track"); - let group = track.append_group().expect("append group"); - let mut before = track.subscribe(None); - let mut racing = track.subscribe(None); - let mut after = track.subscribe(None); - track.finish().expect("finish track"); - - assert!(matches!( - before.poll_recv_group(&kio::Waiter::noop()), - std::task::Poll::Ready(Ok(Some(_))) - )); - - // The group holds no frames and the track is finished, so this consumer parks - // while the group is live and errors once the abort reaches the state itself. - let mut observer = group.consume(); - let aborter = thread::spawn(move || group.abort(Error::Cancel).expect("abort group")); - - match racing.poll_recv_group(&kio::Waiter::noop()) { - // Skipped, so the scan read the mirror as aborted. The state must already - // agree, or the mirror led it and this scan dropped a readable group. - std::task::Poll::Ready(Ok(None)) => assert!( - matches!( - observer.poll_next_frame(&kio::Waiter::noop()), - std::task::Poll::Ready(Err(_)) - ), - "a scan skipped a group whose own state is not aborted" - ), - // Delivered, which the mirror is allowed to do right up until the abort - // commits; the consumer is what surfaces it. - std::task::Poll::Ready(Ok(Some(_))) => {} - _ => panic!("a finalized track cannot park during the abort race"), - } - aborter.join().unwrap(); - - assert!(matches!( - after.poll_recv_group(&kio::Waiter::noop()), - std::task::Poll::Ready(Ok(None)) - )); - }); -} - -/// A publisher parked on `Demand::wanted_changed` drives on-demand capture, so a subscriber -/// appearing on another thread must always wake it. -#[test] -fn subscriber_wakes_parked_demand() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let track = broadcast.create_track("video", None).expect("create track"); - let mut demand = track.demand(); - - let subscriber = thread::spawn(move || { - let track_consumer = consumer.track("video").expect("track"); - // Held until joined, so it outlives the `wanted_changed()` poll. - block_on(track_consumer.subscribe(None)).expect("subscribe") - }); - - while block_on(demand.wanted_changed()) - .expect("the new subscriber was missed") - .is_none() - {} - - drop(subscriber.join().unwrap()); - drop(track); - }); -} - -/// A publisher waiting on `broadcast::Demand` while one reader hands over to the next: -/// the last reader leaves and a new one arrives straight from the track producer, which -/// never touches the broadcast's own state. Whatever the publisher's poll saw, it must be -/// woken for the newcomer. -#[test] -fn broadcast_demand_sees_a_reader_return() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let video = broadcast.create_track("video", None).expect("create track"); - let mut demand = broadcast.demand(); - let first = block_on(consumer.track("video").expect("track").subscribe(None)).expect("subscribe"); - - let reader = thread::spawn(move || { - drop(first); - let next = video.subscribe(None); - // Returned, so neither the reader nor the track goes before the join. - (video, next) - }); - - // The first call reports whatever the swap left; a reader gone must not stay unseen. - while !block_on(demand.wanted_changed()).expect("the returning reader was missed") {} - drop(reader.join().unwrap()); - }); -} - -/// A publisher waiting on `broadcast::Demand` while a reader comes and goes straight -/// through the track producer: its leaving must wake the publisher. -#[test] -fn broadcast_demand_sees_a_reader_leave() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let video = broadcast.create_track("video", None).expect("create track"); - let mut demand = broadcast.demand(); - let reader = video.subscribe(None); - - let leaver = thread::spawn(move || { - drop(reader); - video - }); - - while block_on(demand.wanted_changed()).expect("the reader leaving was missed") {} - drop(leaver.join().unwrap()); - }); -} - -/// A reader taking and dropping a track around the front's demand poll. The front must -/// see it go: one that missed the edge keeps its source subscribed for nobody, which -/// shows up here as a deadlock waiting for the source to go unread. -/// -/// A lookup today also writes the broadcast state the front watches for new tracks, -/// which wakes it regardless, so this only bites once a lookup stops writing. The -/// front's demand poll must not lean on that wake. -#[test] -fn a_front_sees_a_reader_come_and_go() { - model(|| { - let (producer, mut driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); - let consumer = producer.consume(); - let source = producer.publish("room", origin::Route::default()).unwrap(); - let video = source.create_track("video", None).expect("create track"); - let demand = video.demand(); - let noop = kio::Waiter::noop(); - let now = moq_net::time::Instant::now(); - - let pending = consumer.request("room", None); - driver.poll(now, &noop).unwrap(); - let room = block_on(pending).expect("resolves"); - - // A first reader gets the source asked, then leaves. - let first = room.track("video").expect("track").info(); - driver.poll(now, &noop).unwrap(); - assert!(demand.wanted().is_some(), "the front never asked the source"); - drop(first); - - let racing = room.clone(); - let reader = thread::spawn(move || drop(racing.track("video").expect("track").info())); - driver.poll(now, &noop).unwrap(); - reader.join().unwrap(); - - block_on(std::future::poll_fn(|cx| { - driver.poll(now, &kio::Waiter::new(cx.waker().clone())).unwrap(); - match demand.wanted() { - Some(_) => std::task::Poll::Pending, - None => std::task::Poll::Ready(()), - } - })); - }); -} - -/// The other direction: with the track parked, one reader leaves and another arrives -/// around the front's demand poll. A front that missed the arrival leaves the newcomer -/// stalled on a parked track, which shows up here as a deadlock waiting for the source -/// to be asked again. Like `a_front_sees_a_reader_come_and_go`, it must hold without -/// the lookup's incidental wake. -#[test] -fn a_front_sees_a_reader_return() { - model(|| { - let (producer, mut driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); - let consumer = producer.consume(); - let source = producer.publish("room", origin::Route::default()).unwrap(); - let video = source.create_track("video", None).expect("create track"); - let demand = video.demand(); - let noop = kio::Waiter::noop(); - let now = moq_net::time::Instant::now(); - - let pending = consumer.request("room", None); - driver.poll(now, &noop).unwrap(); - let room = block_on(pending).expect("resolves"); - - // A first reader comes and goes, which parks the track. - let first = room.track("video").expect("track").info(); - driver.poll(now, &noop).unwrap(); - drop(first); - driver.poll(now, &noop).unwrap(); - assert!(demand.wanted().is_none(), "the front kept the source for nobody"); - - let leaving = room.track("video").expect("track").info(); - let racing = room.clone(); - let reader = thread::spawn(move || { - drop(leaving); - racing.track("video").expect("track").info() - }); - driver.poll(now, &noop).unwrap(); - let returned = reader.join().unwrap(); - - block_on(std::future::poll_fn(|cx| { - driver.poll(now, &kio::Waiter::new(cx.waker().clone())).unwrap(); - match demand.wanted() { - Some(_) => std::task::Poll::Ready(()), - None => std::task::Poll::Pending, - } - })); - drop(returned); - }); -} - -/// Two tracks publish into one bounded pool from separate threads, and the pool must -/// be back to zero once every handle is gone: a charge that outlives its group is how -/// a cache leaks, and loom's Arc-leak check catches the reference-cycle flavor of the -/// same bug. -/// -/// This does not model the charge accounting itself. `cache::Pool` uses standard -/// atomics, so loom permutes the two tracks only where they meet in kio. Reordering -/// the pool's own counters would need those swapped for loom's atomics too. -#[test] -fn concurrent_tracks_drain_a_shared_pool() { - model(|| { - let config = cache::Config::default() - .with_capacity(512) - .with_expiry(cache::DEFAULT_EXPIRY); - let pool = cache::Pool::new(config); - let mut info = broadcast::Info::new(); - info.pool = pool.clone(); - let broadcast = info.produce(); - - let handles: Vec<_> = ["video", "audio"] - .into_iter() - .map(|name| { - let track = broadcast.create_track(name, timed()).expect("create track"); - thread::spawn(move || { - let mut group = track.append_group().expect("append group"); - group - .write_frame(Timestamp::ZERO, Bytes::from_static(b"0123456789")) - .expect("write frame"); - group.finish().expect("finish group"); - track.finish().expect("finish track"); - }) - }) - .collect(); - - for handle in handles { - handle.join().unwrap(); - } - broadcast.close(); - drop(broadcast); - - assert_eq!(pool.used(), 0, "the pool kept a charge after every group was dropped"); - }); -} - -/// The teardown every wire subscriber runs when its copy of a track goes idle, -/// racing a viewer coming back for the same name. -/// -/// `poll_wanted_changed` is a level snapshot, so the teardown it wakes can only be trusted -/// at the instant it commits. The invariant is that the two can't both win: either -/// the subscribe joins the live track and the teardown declines, or the teardown -/// commits and the subscribe never sees the dead track at all. A subscription joined -/// and then cancelled by that teardown is the bug, and it shows up here as the -/// teardown's `Cancel` reaching the viewer. -#[test] -fn an_idle_teardown_never_cancels_a_returning_viewer() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - // Nothing is consuming it, which is the wake the teardown acts on. - let track = broadcast.create_track("video", None).expect("create track"); - - // A bare handle is no demand, so the subscribe is what races the teardown. - let viewer = thread::spawn(move || consumer.track("video").map(|track| track.subscribe(None).into_inner())); - - // Held to the end: a declined teardown hands back the producer still serving. - let teardown = track.abort_unwanted(Error::Cancel); - let subscribed = viewer.join().unwrap().and_then(|subscribing| { - match subscribing.poll_ok(&kio::Waiter::noop()) { - // A live track parks (nothing has been published); only an error matters. - std::task::Poll::Ready(Ok(mut subscriber)) => match subscriber.poll_recv_group(&kio::Waiter::noop()) { - std::task::Poll::Ready(Err(err)) => Err(err), - _ => Ok(()), - }, - std::task::Poll::Ready(Err(err)) => Err(err), - std::task::Poll::Pending => Ok(()), - } - }); - - // The track was already gone, so nothing was joined to cancel. Nobody serves - // this broadcast on demand, so the re-request has nowhere to go. - if let Err(err) = subscribed { - assert!(teardown.is_ok(), "a declined teardown refused a viewer"); - assert!( - matches!(err, Error::NotFound), - "the teardown cancelled a subscription it raced: {err}" - ); - } - }); -} - -/// Dropping the publisher must resolve a subscriber parked on `recv_group` rather -/// than leaving it waiting for a group that will never come. -#[test] -fn publisher_drop_resolves_a_parked_subscriber() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let consumer = broadcast.consume(); - let track = broadcast.create_track("video", None).expect("create track"); - let subscribing = consumer.track("video").expect("track").subscribe(None); - - let publisher = thread::spawn(move || drop(track)); - - let mut subscriber = block_on(subscribing).expect("subscribe"); - // Ok(None) on a clean finish, Err on an abort; either resolves the park. - let _ = block_on(subscriber.recv_group()); - - publisher.join().unwrap(); - }); -} - -/// The last `fetch_group` caller leaving, racing a new fetch of the same group while -/// the handler drops the request the moment it sees it unused. -/// -/// The leaver withdraws the attempt under the fetch lock a join takes, so the new -/// fetch either joined the live attempt (keeping its request wanted) or queued a fresh -/// one. A fetch joining the withdrawn attempt and failing as its handler drops it is -/// the bug, and shows up as `Err` here. -#[test] -fn a_fetch_never_joins_a_withdrawn_attempt() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let track = broadcast.create_track("video", None).expect("create track"); - let dynamic = track.requests(); - let consumer = track.consume(); - - let first = consumer.fetch_group(5, None); - let request = block_on(dynamic.requested_fill()).expect("requested"); - - let leaver = thread::spawn(move || { - drop(first); - request.wanted().is_some().then_some(request) - }); - let mut retry = consumer.fetch_group(5, None); - let request = match leaver.join().unwrap() { - Some(request) => request, - None => { - let polled = kio::Task::poll(&mut *retry, &kio::Waiter::noop()); - if let std::task::Poll::Ready(Err(err)) = polled { - panic!("the retry joined a withdrawn attempt: {err}"); - } - block_on(dynamic.requested_fill()).expect("the retry queued a fresh request") - } - }; - request.accept(5, None).expect("accept").finish().expect("finish"); - block_on(retry).expect("the retry resolves"); - }); -} - -/// A request racing the front it joins as that front retires: the last holder lets go -/// while the origin's driver runs. The request either holds the front in time, which -/// keeps it, or finds it closed and mints a fresh one. Being handed the broadcast of a -/// front that is ending is the bug, and shows up here as a closed broadcast. -#[test] -fn a_request_never_joins_a_retiring_front() { - model(|| { - let (producer, mut driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); - let consumer = producer.consume(); - let _broadcast = producer.publish("room", origin::Route::default()).unwrap(); - let waiter = kio::Waiter::noop(); - let now = moq_net::time::Instant::now(); - - let pending = consumer.request("room", None); - driver.poll(now, &waiter).unwrap(); - let first = block_on(pending).expect("resolves"); - - let requester = thread::spawn(move || consumer.request("room", None)); - drop(first); - driver.poll(now, &waiter).unwrap(); - let pending = requester.join().unwrap(); - driver.poll(now, &waiter).unwrap(); - let resolved = block_on(pending).expect("resolves"); - assert!(!resolved.is_closed(), "handed a front that is ending"); - }); -} - -/// A claim answer published while its claim retracts on another thread. The answer -/// either lands before the retraction, and stays announced, or is refused without -/// ever being announced. Announcing it and withdrawing it again shows up here as a -/// route seen mid-race on an answer that was refused. -#[test] -fn a_refused_answer_is_never_announced() { - model(|| { - let (producer, mut driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); - let claim = producer.requests("room").unwrap(); - let pending = producer.consume().request("room/alice", None); - driver - .poll(moq_net::time::Instant::now(), &kio::Waiter::noop()) - .unwrap(); - let prepared = block_on(claim.requested_broadcast()) - .expect("requested") - .prepare(moq_net::Epoch::mint()) - .expect("prepare"); - // Keep a refused answer alive so dropping it cannot hide a stray announcement. - let _answer = prepared.broadcast().clone(); - // Only the claim and its answer can cover the path, so any start is a covering route. - let announced = |consumer: &origin::Consumer| { - matches!( - consumer.announced().poll_next(&kio::Waiter::noop()), - std::task::Poll::Ready(Some(moq_net::announce::Event::Start(_))) - ) - }; - - let watch = producer.consume(); - let releaser = thread::spawn(move || { - drop(claim); - announced(&watch) - }); - let published = prepared.publish(); - let seen = releaser.join().unwrap(); - - assert_eq!( - published.is_ok(), - announced(&producer.consume()), - "the route outlived its answer" - ); - assert!(!seen || published.is_ok(), "a refused answer was announced"); - drop(pending); - }); -} - -/// Read a track whose source already aborted with `read`, racing the front ending it. -/// A reader that saw no end, then found one set when it went to park, parked on nothing -/// and never woke, which shows up here as a deadlock. `read` says whether the refusal -/// surfaced as an error. -fn refused_track_race(read: fn(&mut moq_net::track::Subscriber) -> bool) { - model(move || { - let (producer, mut driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); - let consumer = producer.consume(); - let source = producer.publish("room", origin::Route::default()).unwrap(); - let video = source.create_track("video", None).expect("create track"); - let noop = kio::Waiter::noop(); - let now = moq_net::time::Instant::now(); - - let pending = consumer.request("room", None); - driver.poll(now, &noop).unwrap(); - let room = block_on(pending).expect("resolves"); - - let subscribing = room.track("video").expect("track").subscribe(None); - driver.poll(now, &noop).unwrap(); - let mut subscriber = block_on(subscribing).expect("subscribe"); - - video.abort(Error::NotFound).unwrap(); - let reader = thread::spawn(move || read(&mut subscriber)); - driver.poll(now, &noop).unwrap(); - assert!(reader.join().unwrap(), "a refused track read as an end"); - }); -} - -#[test] -fn a_group_reader_sees_the_front_end_a_refused_track() { - refused_track_race(|subscriber| block_on(subscriber.recv_group()).is_err()); -} - -#[test] -fn a_finished_reader_sees_the_front_end_a_refused_track() { - refused_track_race(|subscriber| block_on(subscriber.finished()).is_err()); -} - -/// An abort racing the first parking poll must either be observed or wake the cursor. -#[test] -fn an_aborted_parked_group_releases_a_finished_track() { - model(|| { - let broadcast = broadcast::Info::new().produce(); - let track = broadcast.create_track("parked", timed()).unwrap(); - let mut group = track.append_group().unwrap(); - group.finish().unwrap(); - let mut subscriber = track.subscribe(moq_net::track::Subscription::default().with_range(0..)); - subscriber = subscriber.with_range(..0); - track.finish().unwrap(); - let aborting = thread::spawn(move || group.abort(Error::Old).unwrap()); - assert!(block_on(subscriber.recv_group()).unwrap().is_none()); - aborting.join().unwrap(); - }); -} - -/// An arrival takes the track write lock; revealing an already cached group only takes -/// its read lock. Either path must observe a registered expiry read or leave the -/// successor visible when that read judges it. The landing hint uses Loom atomics. -fn landing_wakes_a_parked_expiry(reveal: bool) { - model(move || { - let broadcast = broadcast::Info::new().produce(); - let track = broadcast.create_track("video", timed()).unwrap(); - let mut head = track.append_group().unwrap(); - head.write_frame(Timestamp::ZERO, Bytes::from_static(b"head")).unwrap(); - let mut subscriber = track.subscribe( - moq_net::track::Subscription::default() - .with_range(0..) - .with_max_delay(std::time::Duration::from_millis(500)), - ); - let mut held = block_on(subscriber.recv_group()).unwrap().unwrap(); - assert!(block_on(held.read_frame()).unwrap().is_some()); - - let landing = if reveal { - let landing = track.append_group().unwrap(); - let mut edge = track.append_group().unwrap(); - edge.write_frame(Timestamp::from_millis(2000).unwrap(), Bytes::from_static(b"edge")) - .unwrap(); - edge.finish().unwrap(); - Some(landing) - } else { - let mut successor = track.append_group().unwrap(); - successor - .write_frame(Timestamp::from_millis(1000).unwrap(), Bytes::from_static(b"successor")) - .unwrap(); - successor.finish().unwrap(); - None - }; - let publisher = thread::spawn(move || { - let mut landing = landing.unwrap_or_else(|| track.append_group().unwrap()); - let at = Timestamp::from_millis(if reveal { 1000 } else { 2000 }).unwrap(); - landing.write_frame(at, Bytes::from_static(b"landing")).unwrap(); - landing.finish().unwrap(); - (track, landing) - }); - assert!(matches!(block_on(held.read_frame()), Err(Error::Old))); - drop(publisher.join().unwrap()); - }); -} - -#[test] -fn a_group_arrival_landing_wakes_parked_expiry() { - landing_wakes_a_parked_expiry(false); -} - -#[test] -fn a_group_reveal_landing_wakes_parked_expiry() { - landing_wakes_a_parked_expiry(true); -} diff --git a/rs/moq-relay/tests/drills.rs b/rs/moq-relay/tests/drills.rs index 1c341ca4bf8..3822f8a08ba 100644 --- a/rs/moq-relay/tests/drills.rs +++ b/rs/moq-relay/tests/drills.rs @@ -22,8 +22,8 @@ //! data ever moved cannot hide here. //! //! See `test/drill/README.md` for the recipe, the sensitivity proof that each -//! drill fails when its recovery behavior is removed, and the loom/fuzz cases -//! covering the primitives underneath. +//! drill fails when its recovery behavior is removed, and the fuzz targets +//! covering the wire underneath. #![cfg(all(feature = "quic", feature = "websocket"))] #![expect( clippy::disallowed_methods, diff --git a/test/drill/README.md b/test/drill/README.md index ada7723f94e..fb417281ade 100644 --- a/test/drill/README.md +++ b/test/drill/README.md @@ -217,18 +217,17 @@ two headers, and add a row above. ## What is underneath -A drill exercises a primitive end to end; the primitive's own orderings are -covered by the loom models, and the wire it rides by the fuzz targets. When a -drill fails, these are where the mechanism is pinned. +A drill exercises a primitive end to end; the wire it rides is covered by the +fuzz targets. When a drill fails, these are where the mechanism is pinned. -| Drill | Loom cases | Fuzz coverage | -|---|---|---| -| cancel under backpressure | `kio::loom::{last_consumer_wakes_unused, consumer_churn_resolves_unused, first_consumer_wakes_used}`, `moq-net tests/loom.rs::{subscriber_wakes_parked_demand, concurrent_tracks_drain_a_shared_pool}` | `lite` (`SUBSCRIBE` / `SUBSCRIBE_UPDATE` encodings) | -| relay killed mid-group | `kio::loom::{racing_last_producer_drops_still_close, queue_close_wakes_a_parked_pop, write_wakes_a_parked_consumer}`, `moq-net tests/loom.rs::publisher_drop_resolves_a_parked_subscriber` | `lite` and `ietf` (a truncated stream must fail to decode, never decode into something else), regression `ietf/subscribe-absolute-filter` | -| interrupted publisher republishes | `kio::loom::{write_wakes_a_parked_consumer, weak_upgrade_never_resurrects_a_closed_channel}`, `moq-net tests/loom.rs::back_to_back_groups_arrive_in_order` | `path` (the broadcast name is a path: normalization, prefixes, and the wire round-trip) | -| bursts across a cluster, steady, flapping, or on moq-transport | `moq-net tests/loom.rs::{a_fetch_never_joins_a_withdrawn_attempt, back_to_back_groups_arrive_in_order}` | `lite` (`FETCH` encodings) | +| Drill | Fuzz coverage | +|---|---| +| cancel under backpressure | `lite` (`SUBSCRIBE` / `SUBSCRIBE_UPDATE` encodings) | +| relay killed mid-group | `lite` and `ietf` (a truncated stream must fail to decode, never decode into something else), regression `ietf/subscribe-absolute-filter` | +| interrupted publisher republishes | `path` (the broadcast name is a path: normalization, prefixes, and the wire round-trip) | +| bursts across a cluster, steady, flapping, or on moq-transport | `lite` (`FETCH` encodings) | -Run them with `just rs loom` and `just rs fuzz `. The committed fuzz +Run them with `just rs fuzz `. The committed fuzz findings under `rs/moq-net/fuzz/regressions/` replay on stable as part of `just check`, so a crash a drill leads you to belongs there rather than in a corpus nobody replays.