From e2595163db84b7f6598616269e5b2804c4da56b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 9 Aug 2026 14:11:20 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine-audio):=20paused=20se?= =?UTF-8?q?ek=20ate=201.49s=20of=20post-seek=20audio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit S6 verifier finding: ao_flush is consumed only by the next RT callback; while paused there are none, so the new feeder filled the ring behind the pending flag and the first post-resume callback drained the new stream's head (measured 151 underruns = 1.49s = ring capacity, audio then 1.5s ahead of picture). ao_write now refuses samples while a flush is pending (accept() mirrors consume(); feeder's retry loop parks until the RT thread takes the flag). Probe re-run: paused seek 86 underruns = no-drop baseline; mid-play unchanged at 130. --- crates/engine-audio/src/lib.rs | 43 ++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/engine-audio/src/lib.rs b/crates/engine-audio/src/lib.rs index b13b7fd..779d2ca 100644 --- a/crates/engine-audio/src/lib.rs +++ b/crates/engine-audio/src/lib.rs @@ -96,6 +96,19 @@ impl Ring { /// only the RT thread ever moves `read` -- and means a flush asked for while /// paused (no callbacks running) still lands before the next pop, so the stale /// samples can never be heard. +/// The producer half of a flush: while the flag is pending, the ring still +/// holds pre-flush audio waiting to be discarded, and anything accepted now +/// would queue BEHIND that discard -- with no RT callbacks running (paused +/// seek) the next callback would then drain the new stream's head along with +/// the stale tail. Refuse instead; the writer's retry loop parks until the +/// RT thread consumes the flag. +fn accept(flush: &AtomicBool, ring: &Ring, samples: &[f32]) -> usize { + if flush.load(Ordering::Acquire) { + return 0; + } + ring.push(samples) +} + fn consume(flush: &AtomicBool, ring: &Ring, dst: &mut [u8], want: usize) -> usize { if flush.swap(false, Ordering::Acquire) { ring.read @@ -387,7 +400,7 @@ pub unsafe extern "C" fn ao_write(session: *mut c_void, samples: *const f32, n: return -1; } let samples = unsafe { std::slice::from_raw_parts(samples, n) }; - session.shared.ring.push(samples) as isize + accept(&session.shared.flush, &session.shared.ring, samples) as isize })) .unwrap_or(-1) } @@ -468,7 +481,33 @@ pub unsafe extern "C" fn ao_close(session: *mut c_void) { #[cfg(test)] mod tests { - use super::{AtomicBool, Ordering, Ring, consume}; + use super::{AtomicBool, Ordering, Ring, accept, consume}; + + /// The paused-seek ordering: a flush requested with no RT callbacks running + /// must not let the writer queue new audio behind the pending discard. + #[test] + fn pending_flush_refuses_writes_until_consumed() { + let ring = Ring::new(4); + let flush = AtomicBool::new(false); + let mut out = [0u8; 16]; + + // Stale pre-seek audio sits in the ring; a flush is requested while + // paused (no callback runs yet). + assert_eq!(accept(&flush, &ring, &[1.0, 2.0]), 2); + flush.store(true, Ordering::Release); + + // The new stream's feeder tries to write: refused, ring untouched. + assert_eq!(accept(&flush, &ring, &[3.0, 4.0]), 0); + + // First post-resume callback consumes the flag and drains the stale + // tail -- and ONLY the stale tail, because nothing was queued behind it. + assert_eq!(consume(&flush, &ring, &mut out, 2), 0); + + // Writer unparks; the new audio flows intact. + assert_eq!(accept(&flush, &ring, &[3.0, 4.0]), 2); + assert_eq!(consume(&flush, &ring, &mut out, 2), 2); + assert_eq!(&out[..4], &3.0f32.to_le_bytes()); + } #[test] fn ring_wraps_fills_and_starves() {