From ef07ecab1e777cb6182dd47109c812179a71b46f Mon Sep 17 00:00:00 2001 From: hauke Date: Thu, 5 Nov 2026 14:38:40 +0100 Subject: [PATCH] fix #14740: report capture sources that die mid-recording Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/audio/src/capture_thread.rs | 35 +++++++++++++++--- crates/audio/src/linux/capture.rs | 16 ++++++--- crates/audio/src/windows/capture.rs | 36 +++++++++++++------ crates/core/src/capture.rs | 6 ++++ crates/core/src/recorder.rs | 56 ++++++++++++++++++++++++++--- 5 files changed, 125 insertions(+), 24 deletions(-) diff --git a/crates/audio/src/capture_thread.rs b/crates/audio/src/capture_thread.rs index 757e513..98da081 100644 --- a/crates/audio/src/capture_thread.rs +++ b/crates/audio/src/capture_thread.rs @@ -3,11 +3,13 @@ //! Platform stream handles (COM interfaces, PulseAudio connections) are not //! `Send`, so they are created, used and dropped on this one thread. The //! thread reports whether the stream opened through a channel, which lets -//! `CaptureSource::start` return that result synchronously. +//! `CaptureSource::start` return that result synchronously. If the body +//! ends on its own (not because stop was requested), its error is kept for +//! `CaptureSource::failure`. use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{self, Receiver, RecvTimeoutError, SyncSender}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::thread::{self, JoinHandle}; use std::time::Duration; @@ -22,6 +24,8 @@ pub(crate) type Ready = SyncSender>; pub(crate) struct CaptureThread { stop: Arc, + /// The error the body ended with, if it returned one. + error: Arc>>, /// Disconnects when the thread ends (its sender is dropped with it). done: Receiver<()>, handle: JoinHandle<()>, @@ -30,12 +34,15 @@ pub(crate) struct CaptureThread { impl CaptureThread { /// Spawns `body` on a new thread and waits until it has reported the /// stream format (or an error) through `ready`. `body` must return soon - /// after the stop flag it is given is set. + /// after the stop flag it is given is set; it returns `Err(reason)` when + /// the stream ended on its own. pub(crate) fn spawn(name: &str, body: F) -> Result<(Self, StreamFormat), CaptureError> where - F: FnOnce(&AtomicBool, Ready) + Send + 'static, + F: FnOnce(&AtomicBool, Ready) -> Result<(), String> + Send + 'static, { let stop = Arc::new(AtomicBool::new(false)); + let error = Arc::new(Mutex::new(None)); + let thread_error = error.clone(); let (ready_tx, ready_rx) = mpsc::sync_channel(1); let (done_tx, done_rx) = mpsc::channel::<()>(); let thread_stop = stop.clone(); @@ -43,7 +50,9 @@ impl CaptureThread { .name(name.to_string()) .spawn(move || { let _done = done_tx; - body(&thread_stop, ready_tx); + if let Err(reason) = body(&thread_stop, ready_tx) { + *thread_error.lock().unwrap_or_else(|p| p.into_inner()) = Some(reason); + } }) .map_err(|err| CaptureError::Backend(format!("cannot spawn capture thread: {err}")))?; @@ -51,6 +60,7 @@ impl CaptureThread { Ok(Ok(format)) => Ok(( Self { stop, + error, done: done_rx, handle, }, @@ -69,6 +79,21 @@ impl CaptureThread { } } + /// Why the thread ended without being asked to stop (`None` while it + /// runs or after a requested stop). A body that panicked or returned + /// `Ok` early gets a generic reason. + pub(crate) fn failure(&self) -> Option { + if self.stop.load(Ordering::SeqCst) || !self.handle.is_finished() { + return None; + } + let error = self.error.lock().unwrap_or_else(|p| p.into_inner()); + Some( + error + .clone() + .unwrap_or_else(|| "capture stopped unexpectedly".into()), + ) + } + /// Signals the thread to stop and waits for it to end. pub(crate) fn stop(self) { self.stop.store(true, Ordering::SeqCst); diff --git a/crates/audio/src/linux/capture.rs b/crates/audio/src/linux/capture.rs index a95e770..caafe0a 100644 --- a/crates/audio/src/linux/capture.rs +++ b/crates/audio/src/linux/capture.rs @@ -80,6 +80,10 @@ impl CaptureSource for PulseSource { thread.stop(); } } + + fn failure(&self) -> Option { + self.thread.as_ref().and_then(CaptureThread::failure) + } } impl Drop for PulseSource { @@ -127,12 +131,13 @@ fn run( stop: &AtomicBool, ready: Ready, mut on_data: DataCallback, -) { +) -> Result<(), String> { let simple = match open(device, stream_name) { Ok(simple) => simple, Err(err) => { + let reason = err.to_string(); let _ = ready.send(Err(err)); - return; + return Err(reason); } }; let _ = ready.send(Ok(FORMAT)); @@ -141,14 +146,15 @@ fn run( let mut samples = vec![0f32; CHUNK_SAMPLES]; while !stop.load(Ordering::SeqCst) { if let Err(err) = simple.read(&mut bytes) { - // Source gone (unplugged, server restart): stop delivering; the - // recorder keeps the track aligned with silence. + // Source gone (unplugged, server restart): stop delivering and + // report it; the recorder keeps the track aligned with silence. log::warn!("PulseAudio capture '{stream_name}' ended: {err}"); - return; + return Err(format!("PulseAudio: {err}")); } for (sample, raw) in samples.iter_mut().zip(bytes.as_chunks::<4>().0) { *sample = f32::from_ne_bytes(*raw); } on_data(&samples); } + Ok(()) } diff --git a/crates/audio/src/windows/capture.rs b/crates/audio/src/windows/capture.rs index 8ffd336..c6d730e 100644 --- a/crates/audio/src/windows/capture.rs +++ b/crates/audio/src/windows/capture.rs @@ -143,6 +143,10 @@ impl CaptureSource for WasapiSource { thread.stop(); } } + + fn failure(&self) -> Option { + self.thread.as_ref().and_then(CaptureThread::failure) + } } impl Drop for WasapiSource { @@ -243,24 +247,35 @@ fn open(endpoint: &Endpoint, format: Option) -> Result Result<(), String> { if let Err(err) = initialize_mta().ok() { - let _ = ready.send(Err(CaptureError::Backend(format!( - "COM initialisation failed: {err}" - )))); - return; + let reason = format!("COM initialisation failed: {err}"); + let _ = ready.send(Err(CaptureError::Backend(reason.clone()))); + return Err(reason); } - capture(&endpoint, stop, ready, on_data); + let result = capture(&endpoint, stop, ready, on_data); // Every COM object is dropped by now. deinitialize(); + result } -fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: DataCallback) { +fn capture( + endpoint: &Endpoint, + stop: &AtomicBool, + ready: Ready, + mut on_data: DataCallback, +) -> Result<(), String> { let mut stream = match open(endpoint, None) { Ok(stream) => stream, Err(err) => { + let reason = err.to_string(); let _ = ready.send(Err(err)); - return; + return Err(reason); } }; let format = stream.format; @@ -279,7 +294,7 @@ fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: Da }; if !is_device_invalidated(&err) || reopened { log::warn!("WASAPI capture '{label}' ended: {err}"); - return; + return Err(format!("WASAPI: {err}")); } reopened = true; log::warn!("WASAPI capture '{label}': device invalidated, reopening the default device"); @@ -288,10 +303,11 @@ fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: Da Ok(stream) => stream, Err(err) => { log::warn!("WASAPI capture '{label}': reopening failed: {err}"); - return; + return Err(format!("device lost, reopening failed: {err}")); } }; } + Ok(()) } /// Delivers every packet currently queued in the capture buffer. diff --git a/crates/core/src/capture.rs b/crates/core/src/capture.rs index d47f280..4f56e62 100644 --- a/crates/core/src/capture.rs +++ b/crates/core/src/capture.rs @@ -69,4 +69,10 @@ pub trait CaptureSource: Send { fn start(&mut self, on_data: DataCallback) -> Result; fn stop(&mut self); + + /// Why the stream ended on its own (device lost, read error, reopen + /// failed), or `None` while it is healthy, before `start` and after a + /// requested `stop`. Switching to another device successfully is not a + /// failure. + fn failure(&self) -> Option; } diff --git a/crates/core/src/recorder.rs b/crates/core/src/recorder.rs index f31d818..e0cf3ae 100644 --- a/crates/core/src/recorder.rs +++ b/crates/core/src/recorder.rs @@ -317,12 +317,16 @@ impl Recorder { (self.tracks[0].shared.level(), self.tracks[1].shared.level()) } - /// Names ("mic" / "out") of tracks whose source failed to start or whose - /// file could not be written. + /// Names ("mic" / "out") of tracks whose source failed to start, died + /// mid-recording, or whose file could not be written. pub fn failed_tracks(&self) -> Vec<&'static str> { self.tracks .iter() - .filter(|t| t.start_failed || t.shared.write_failed.load(Ordering::Relaxed)) + .filter(|t| { + t.start_failed + || t.source.failure().is_some() + || t.shared.write_failed.load(Ordering::Relaxed) + }) .map(|t| t.name) .collect() } @@ -377,11 +381,13 @@ mod tests { /// Test source: a thread that pushes a constant-amplitude 20 ms chunk /// every 20 ms, except inside the `silent` window (relative to start), - /// where it sends nothing at all. + /// where it sends nothing at all. With `dies_after`, the thread ends on + /// its own at that time, like a source whose device went away. struct FakeSource { format: StreamFormat, amplitude: f32, silent: Option<(Duration, Duration)>, + dies_after: Option, fail: bool, stop: Arc, thread: Option>, @@ -393,6 +399,7 @@ mod tests { format: StreamFormat { rate, channels }, amplitude, silent: None, + dies_after: None, fail: false, stop: Arc::new(AtomicBool::new(false)), thread: None, @@ -404,6 +411,11 @@ mod tests { self } + fn dies_after(mut self, after: Duration) -> Self { + self.dies_after = Some(after); + self + } + fn failing() -> Self { let mut source = Self::new(16_000, 1, 0.0); source.fail = true; @@ -420,6 +432,7 @@ mod tests { let frames = (format.rate as u128 * CHUNK.as_millis() / 1000) as usize; let chunk = vec![self.amplitude; frames * format.channels as usize]; let silent = self.silent; + let dies_after = self.dies_after; let stop = self.stop.clone(); self.thread = Some(thread::spawn(move || { let started = Instant::now(); @@ -430,6 +443,9 @@ mod tests { thread::sleep(wait); } sent += 1; + if dies_after.is_some_and(|after| due > after) { + return; + } let muted = silent.is_some_and(|(from, to)| due > from && due <= to); if !muted { on_data(&chunk); @@ -445,6 +461,11 @@ mod tests { thread.join().unwrap(); } } + + fn failure(&self) -> Option { + let ended = self.thread.as_ref().is_some_and(JoinHandle::is_finished); + (ended && !self.stop.load(Ordering::SeqCst)).then(|| "fake source died".into()) + } } fn read_wav(path: &Path) -> Vec { @@ -588,6 +609,33 @@ mod tests { ); } + #[test] + fn recorder_marks_track_failed_when_source_dies_mid_recording() { + let dir = tempfile::tempdir().unwrap(); + let mic_path = dir.path().join("mic.wav"); + let out_path = dir.path().join("out.wav"); + let out = FakeSource::new(16_000, 1, 0.25).dies_after(Duration::from_millis(200)); + let recorder = Recorder::start( + Box::new(FakeSource::new(16_000, 1, 0.25)), + Box::new(out), + &mic_path, + &out_path, + ) + .unwrap(); + thread::sleep(Duration::from_millis(100)); + assert!(recorder.failed_tracks().is_empty(), "healthy at first"); + thread::sleep(Duration::from_millis(400)); + + assert_eq!(recorder.failed_tracks(), vec!["out"]); + recorder.stop().unwrap(); + + let mic_samples = read_wav(&mic_path); + let out_samples = read_wav(&out_path); + assert_eq!(mic_samples.len(), out_samples.len()); + let tail = &mic_samples[(0.4 * 16_000.0) as usize..]; + assert!(tail.iter().any(|&s| s != 0), "mic keeps recording"); + } + #[test] fn recorder_fails_when_both_sources_fail() { let dir = tempfile::tempdir().unwrap();