Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m0/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ Public API: none. Wire: none.

- [IETF22 fallback](/quest/m0/test-flakes-2/ietf22-fallback.md) - an IETF22 browser viewer follows a fallback within the media lane's bound, keeping its session and its audio
- [Browser buffering and Firefox audio-tone tests pass under load](/quest/m0/test-flakes-2/browser-media-flakes.md) - the two `harness.browser.ts` buffering tests and the Firefox player's audio-tone checks pass reliably in Interop and Nightly, each fixed at its cause under the epic's rules
- [Timing-bound tests pass beside a busy build](/quest/m0/test-flakes-2/load-timing-bounds.md) - four tests that pass alone but have failed under a loaded `just check` pass reliably there, each fixed at its cause under the epic's rules
- [moq-tokio's close-drain test survives load](/quest/m0/test-flakes-2/tokio-close-drain.md) - `quic_client_close_drains_finished_track` stops failing land batches with "Unroutable"
- [@moq/net's close-drain tests survive load](/quest/m0/test-flakes-2/js-close-drain.md) - the lite close-drain cases pass on every full suite run
- [@moq/net's boundary-sync test survives load](/quest/m0/test-flakes-2/js-boundary-sync.md) - "a version without a boundary syncs" passes under load
Expand Down
53 changes: 0 additions & 53 deletions quest/m0/test-flakes-2/load-timing-bounds.md

This file was deleted.

95 changes: 46 additions & 49 deletions rs/kio/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ struct Shared<T> {
base: AtomicU64,
/// The oldest live segment, where a reader without a usable segment starts.
head: AtomicPtr<Segment<T>>,
#[cfg(test)]
collector: Option<epoch::Collector>,
}

impl<T> Shared<T> {
fn pin(&self) -> epoch::Guard {
#[cfg(test)]
if let Some(collector) = &self.collector {
return collector.register().pin();
}
epoch::pin()
}
}

/// Appends entries to a log and evicts them from its front.
Expand Down Expand Up @@ -263,6 +275,8 @@ impl<T: Send + Sync + 'static> Writer<T> {
end: AtomicU64::new(start),
base: AtomicU64::new(start),
head: AtomicPtr::new(ptr::null_mut()),
#[cfg(test)]
collector: None,
}),
segments: SmallVec::new(),
heap: Self::SHARED,
Expand Down Expand Up @@ -380,7 +394,7 @@ impl<T: Send + Sync + 'static> Writer<T> {
// SAFETY: still live; retired below.
self.heap -= Segment::<T>::heap(unsafe { segment.as_ref() }.capacity);
}
Self::retire(retired);
self.retire(retired);
self.take_evicted(index);
}

Expand All @@ -404,7 +418,7 @@ impl<T: Send + Sync + 'static> Writer<T> {
// the slots under it. Relaxed: the writer still owns it.
header.owners.fetch_add(1, Ordering::Relaxed);
header.taken.store(end, Ordering::Release);
let guard = epoch::pin();
let guard = self.shared.pin();
let entries = Taken(segment, taken..end);
// SAFETY: below the base, which is stored first, so only a read pinned before this
// can still reach them, and the collector waits those out. The segment's free
Expand All @@ -421,8 +435,8 @@ impl<T: Send + Sync + 'static> Writer<T> {
}

/// Hand segments no reader can reach any more to the epoch collector.
fn retire(segments: impl IntoIterator<Item = NonNull<Segment<T>>>) {
let guard = epoch::pin();
fn retire(&self, segments: impl IntoIterator<Item = NonNull<Segment<T>>>) {
let guard = self.shared.pin();
for segment in segments {
let segment = Retired(segment);
// SAFETY: unlinked from the head and the base, so only a read pinned before
Expand Down Expand Up @@ -462,7 +476,8 @@ impl<T: Send + Sync + 'static> Drop for Writer<T> {
self.shared.base.store(self.end(), Ordering::Release);
self.shared.head.store(ptr::null_mut(), Ordering::Release);
if !self.segments.is_empty() {
Self::retire(std::mem::take(&mut self.segments));
let segments = std::mem::take(&mut self.segments);
self.retire(segments);
}
}
}
Expand Down Expand Up @@ -503,7 +518,7 @@ impl<T> Reader<T> {
/// Pin the log for a run of reads. Entries read through the returned handle stay
/// alive until it drops; nothing does once it has.
pub fn read(&mut self) -> Read<'_, T> {
let guard = epoch::pin();
let guard = self.shared.pin();
let base = self.shared.base.load(Ordering::Acquire);
// A segment is retired only once the base passes its last entry, so one whose
// start the base hasn't reached was not retired before this pin.
Expand Down Expand Up @@ -638,10 +653,13 @@ mod test {

use super::*;

// Crossbeam shares its collector across logs. Keep unrelated tests from holding
// pins or queuing garbage while another test checks reclamation. Nextest isolates
// tests in processes, but libtest runs them concurrently in one process.
static COLLECTOR: Mutex<()> = Mutex::new(());
/// Reclamation assertions use their own domain, so unrelated log reads cannot pin it.
fn isolated<T: Send + Sync + 'static>(start: u64) -> (Writer<T>, epoch::Collector) {
let collector = epoch::Collector::new();
let mut writer = Writer::new(start);
Arc::get_mut(&mut writer.shared).unwrap().collector = Some(collector.clone());
(writer, collector)
}

/// The `[start, end)` of each live segment, oldest first.
fn layout<T: Send + Sync + 'static>(writer: &Writer<T>) -> Vec<(u64, u64)> {
Expand All @@ -653,10 +671,11 @@ mod test {
.collect()
}

/// Drive reclamation without unrelated pins or garbage, under `COLLECTOR`.
fn collect() {
/// Advance the local domain beyond the retirement epoch and drain its garbage.
fn collect(collector: &epoch::Collector) {
let handle = collector.register();
for _ in 0..8 {
epoch::pin().flush();
handle.pin().flush();
}
}

Expand All @@ -668,7 +687,6 @@ mod test {
/// reader that started before any of them existed or one that jumps.
#[test]
fn reads_every_entry_across_segments() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let mut reader = writer.reader();
assert_eq!(get(&mut reader, 0), Ok(None));
Expand All @@ -693,7 +711,6 @@ mod test {
/// is allocated.
#[test]
fn segments_double_up_to_the_largest() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::<u64>::new(0);
assert_eq!(writer.heap(), Writer::<u64>::SHARED);
writer.reserve();
Expand Down Expand Up @@ -731,7 +748,6 @@ mod test {
/// A segment's heap is its allocation's real size.
#[test]
fn heap_matches_layout() {
let _collector = COLLECTOR.lock().unwrap();
let header = Layout::new::<Segment<StdArc<()>>>();
let slots = Layout::array::<Slot<StdArc<()>>>(4).unwrap();
let layout = header.extend(slots).unwrap().0.pad_to_align();
Expand All @@ -741,7 +757,6 @@ mod test {
/// A log can start past zero; nothing below its start is readable.
#[test]
fn starts_past_zero() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(10);
let mut reader = writer.reader();
writer.push("a");
Expand All @@ -754,7 +769,6 @@ mod test {
/// [`Evicted`] rather than a stale entry, even in the segment it last read.
#[test]
fn eviction_retires_whole_segments() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let mut reader = writer.reader();
for i in 0..10u64 {
Expand Down Expand Up @@ -784,16 +798,15 @@ mod test {
/// and that reader finds them evicted if it does.
#[test]
fn eviction_frees_whatever_readers_do() {
let _collector = COLLECTOR.lock().unwrap();
let entry = StdArc::new(());
let mut writer = Writer::new(0);
let (mut writer, collector) = isolated(0);
let mut reader = writer.reader();
for _ in 0..3 {
writer.push(entry.clone());
}
assert!(get(&mut reader, 1).unwrap().is_some());
writer.evict(3);
collect();
collect(&collector);
assert_eq!(StdArc::strong_count(&entry), 1, "freed while the reader idles");
assert_eq!(get(&mut reader, 1), Err(Evicted));
assert_eq!(get(&mut reader, 3), Ok(None));
Expand All @@ -802,26 +815,24 @@ mod test {
/// A read in progress keeps what it can reach until it ends.
#[test]
fn a_read_outlasts_eviction() {
let _collector = COLLECTOR.lock().unwrap();
let entry = StdArc::new(());
let mut writer = Writer::new(0);
let (mut writer, collector) = isolated(0);
let mut reader = writer.reader();
writer.push(entry.clone());
let mut read = reader.read();
let held = read.get(0).unwrap().unwrap();
writer.evict(1);
collect();
collect(&collector);
assert_eq!(StdArc::strong_count(held), 2, "pinned by the read");
drop(read);
collect();
collect(&collector);
assert_eq!(StdArc::strong_count(&entry), 1);
}

/// A read that spans the writer emptying the log and appending again finds the new
/// entry, though the segment it last read was cut off from the new one.
#[test]
fn a_read_spans_a_full_eviction() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let mut reader = writer.reader();
writer.push(0u64);
Expand All @@ -838,7 +849,6 @@ mod test {
/// misread.
#[test]
fn evicting_the_tail_starts_a_new_segment() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let mut reader = writer.reader();
for i in 0..2u64 {
Expand Down Expand Up @@ -867,35 +877,29 @@ mod test {
/// when the whole segment retires, and never drops one twice.
#[test]
fn evicting_inside_a_segment_drops_its_entries() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let (mut writer, collector) = isolated(0);
let entries: Vec<_> = (0..6).map(Arc::new).collect();
for entry in &entries {
writer.push(entry.clone());
}
// Segments of 1, 2, and 4 slots: the last holds entries 3 to 5 of its 4.
writer.evict(4);
writer.evict(5);
for _ in 0..64 {
epoch::pin().flush();
}
collect(&collector);
for (index, entry) in entries.iter().enumerate() {
let held = usize::from(index >= 5);
assert_eq!(Arc::strong_count(entry), 1 + held, "entry {index}");
}
drop(writer);
for _ in 0..64 {
epoch::pin().flush();
}
collect(&collector);
assert!(entries.iter().all(|entry| Arc::strong_count(entry) == 1));
}

/// A read that still holds an evicted entry may mutate it through interior
/// mutability, and the entry dropped later is the one it mutated, not a stale copy.
#[test]
fn an_evicted_entry_a_read_still_holds_drops_as_mutated() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let (mut writer, collector) = isolated(0);
let held = StdArc::new(());
for _ in 0..3 {
writer.push(Mutex::new(vec![held.clone()]));
Expand All @@ -908,21 +912,16 @@ mod test {
let taken = std::mem::take(&mut *entry.lock().unwrap());
drop(read);
drop(taken);
for _ in 0..64 {
epoch::pin().flush();
}
collect(&collector);
assert_eq!(StdArc::strong_count(&held), 2, "only entry 2 still holds it");
drop(writer);
for _ in 0..64 {
epoch::pin().flush();
}
collect(&collector);
assert_eq!(StdArc::strong_count(&held), 1);
}

/// Evicting past the end skips those indices: the next entry lands at the new base.
#[test]
fn evicting_past_the_end_skips() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let mut reader = writer.reader();
writer.reserve();
Expand All @@ -941,7 +940,6 @@ mod test {
/// A clone reads independently from where the original's segment was.
#[test]
fn clones_read_independently() {
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let mut reader = writer.reader();
for i in 0..20u64 {
Expand All @@ -958,16 +956,16 @@ mod test {
/// alive for it.
#[test]
fn reader_outlives_writer() {
let _collector = COLLECTOR.lock().unwrap();
let _unrelated = epoch::pin();
let entry = StdArc::new(());
let mut writer = Writer::new(0);
let (mut writer, collector) = isolated(0);
let mut reader = writer.reader();
for _ in 0..20 {
writer.push(entry.clone());
}
assert!(get(&mut reader, 10).unwrap().is_some());
drop(writer);
collect();
collect(&collector);
assert_eq!(StdArc::strong_count(&entry), 1);
assert_eq!(get(&mut reader, 10), Err(Evicted));
assert_eq!(get(&mut reader, 20), Ok(None));
Expand All @@ -979,7 +977,6 @@ mod test {
fn concurrent_readers_see_every_entry() {
// Miri takes minutes over the full run; a tenth still evicts under the readers.
const ENTRIES: u64 = if cfg!(miri) { 1_000 } else { 10_000 };
let _collector = COLLECTOR.lock().unwrap();
let mut writer = Writer::new(0);
let readers: Vec<_> = (0..4)
.map(|_| {
Expand Down
3 changes: 2 additions & 1 deletion rs/moq-c/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ impl Callback {
}

fn recv(&self) -> i32 {
self.rx.recv_timeout(TIMEOUT).expect("callback timed out")
// Delivery depends on the runtime and codec threads; nextest guards hangs.
self.rx.recv().expect("callback channel closed")
}

/// Wait for the terminal callback (code <= 0) the task delivers after close
Expand Down
Loading
Loading