From f490585a1beb021f113217e669171f7bf61930da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 13 Aug 2026 07:59:06 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(playback):=20a=20session=20c?= =?UTF-8?q?ounts=20its=20own=20workers,=20not=20the=20whole=20house?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The seek tests judged a scrub by `/proc/self/status`, which counts every thread in the process: twenty other tests decoding files of their own moved the number under a session that had done nothing, and the budget assertions flaked at roughly one run in ten -- twice today. `PlaybackSession::live_workers` counts what the session in hand actually owns: its decode worker, the retired ones that have not returned yet, and the audio feeders still in the air (detached, so they are counted rather than joined). Same tight budget, same thing caught -- a storm of forty seeks reads twelve the instant it ends and two again fifty milliseconds later, so a worker that stopped exiting would still be seen. --- crates/engine/src/playback.rs | 56 ++++++++++++++++++++++++++------- crates/engine/tests/playback.rs | 31 +++++++----------- 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 90dcd14..e9fe7bf 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -12,7 +12,7 @@ //! the renderer decides which frame that means. use std::path::Path; -use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, AtomicUsize, Ordering}; use std::sync::mpsc::{Receiver, TryRecvError}; use std::sync::{Arc, Mutex}; use std::thread; @@ -116,6 +116,11 @@ struct Audio { /// is not the device's own callback -- the plugin's RT thread never sees /// this lock, and the feeder holds it for one memcpy per write. tap: Arc>>, + /// Feeder threads started and not yet returned. They are detached, so there + /// is no handle to count instead, and a scrub leaves dozens of them exiting + /// at once -- which is precisely what + /// [`live_workers`](PlaybackSession::live_workers) is asked about. + feeders: Arc, } /// How much of the played signal the tap keeps. A power of two, because what @@ -174,24 +179,35 @@ impl Audio { ) -> bool { let me = self.clone(); let epoch = self.epoch.load(Ordering::Acquire); - thread::Builder::new() + // Counted before the spawn, so the count is never behind the thread. + self.feeders.fetch_add(1, Ordering::Release); + let started = thread::Builder::new() .name("audio-feed".into()) .spawn(move || { // Superseded before this thread was even scheduled: do not open // anything at all. A scrub leaves all but the last one here. - if me.epoch.load(Ordering::Acquire) != epoch { - return; - } - if let Some(rx) = open() { - feed(rx, &me, epoch); - } - // Only the current feeder gets to declare the end of the audio: - // a superseded one is finished, the stream is not. if me.epoch.load(Ordering::Acquire) == epoch { - me.fed_all.store(true, Ordering::Release); + if let Some(rx) = open() { + feed(rx, &me, epoch); + } + // Only the current feeder gets to declare the end of the + // audio: a superseded one is finished, the stream is not. + if me.epoch.load(Ordering::Acquire) == epoch { + me.fed_all.store(true, Ordering::Release); + } } + me.feeders.fetch_sub(1, Ordering::Release); }) - .is_ok() + .is_ok(); + if !started { + self.feeders.fetch_sub(1, Ordering::Release); + } + started + } + + /// Feeder threads still running; see [`Self::feeders`]. + fn live_feeders(&self) -> usize { + self.feeders.load(Ordering::Acquire) } } @@ -1219,6 +1235,21 @@ impl PlaybackSession { self.retired.retain(|w| !w.is_finished()); } + /// How many threads this session still has running of its own: the decode + /// worker, every retired one that has not returned yet, and the audio + /// feeders still in the air. One for a session playing along quietly. + /// + /// The oracle a seek storm is judged by. `/proc/self/status` counts the + /// *process*, and a suite decoding twenty files at once moves that number + /// under a test that never touched them -- this counts only what the + /// session in hand owns, so it says the same thing alone and in a crowd. + #[doc(hidden)] + pub fn live_workers(&self) -> usize { + usize::from(!self.worker.is_finished()) + + self.retired.iter().filter(|w| !w.is_finished()).count() + + self.audio.as_ref().map_or(0, Audio::live_feeders) + } + /// Starts feeding whatever follows the current span on the timeline -- /// another clip, or a gap, which is black frames from a worker that opens no /// file. `false` past the end. The next timeline frame is derived rather @@ -2942,6 +2973,7 @@ fn open_audio(path: &Path, stream: usize) -> (Option