YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: prevent use-after-free in audio stream stop (#3306)
Louis Beaumont6430566beb11
diff --git a/crates/screenpipe-audio/src/core/stream.rs b/crates/screenpipe-audio/src/core/stream.rsindex 1b1f1c4e2..d4e1b5d6a 100644--- a/crates/screenpipe-audio/src/core/stream.rs+++ b/crates/screenpipe-audio/src/core/stream.rs@@ -270,8 +270,7 @@ impl AudioStream {// Sources without a cpal control channel (e.g. `from_wav`,// `from_sender_for_test`) drop the receiver, so the send/recv// here will error. That's expected — `is_disconnected` already- // signals the playback task to exit, and the JoinHandle abort- // below cleans up the spawned task. Don't propagate this error.+ // signals the playback task to exit. Don't propagate this error.let (tx, rx) = oneshot::channel();if self.stream_control.send(StreamControl::Stop(tx)).is_ok() {let _ = rx.await;@@ -280,14 +279,21 @@ impl AudioStream {if let Some(thread_arc) = self.stream_thread.as_ref() {let thread_arc_clone = thread_arc.clone();- let thread_handle = tokio::task::spawn_blocking(move || {+ tokio::task::spawn_blocking(move || {let mut thread_guard = thread_arc_clone.blocking_lock();if let Some(join_handle) = thread_guard.take() {- join_handle.abort();+ // Wait for the thread to exit naturally instead of aborting.+ // This ensures cpal stream.pause() and drop() complete before+ // the stream resources are freed. Aborting creates a race with+ // the CoreAudio IO callback, causing use-after-free crashes.+ // See https://github.com/screenpipe/screenpipe/issues/3261+ // We're in spawn_blocking, so we can block here.+ while !join_handle.is_finished() {+ std::thread::sleep(std::time::Duration::from_millis(10));+ }}- });-- thread_handle.await?;+ })+ .await?;}Ok(())diff --git a/crates/screenpipe-audio/tests/core_tests.rs b/crates/screenpipe-audio/tests/core_tests.rsindex 18922cee4..b9112f3db 100644--- a/crates/screenpipe-audio/tests/core_tests.rs+++ b/crates/screenpipe-audio/tests/core_tests.rs@@ -390,4 +390,42 @@ mod tests {debug!("Transcription completed in {:?}", elapsed_time);}++ #[tokio::test]+ #[ignore] // Requires audio device access+ async fn test_audio_stream_stop_no_race_condition() {+ // Test for issue #3261: ensure stream.stop() properly waits for+ // the audio thread to exit instead of aborting it, preventing+ // use-after-free races with the CoreAudio IO callback.+ // See: https://github.com/screenpipe/screenpipe/issues/3261+ setup();++ let device = default_input_device().expect("should find default input device");+ let is_running = Arc::new(AtomicBool::new(true));++ // Create an audio stream+ let stream = AudioStream::from_device(Arc::new(device), is_running.clone(), false)+ .await+ .expect("should create audio stream");++ // Let it run for a bit to ensure the audio thread is active+ tokio::time::sleep(Duration::from_millis(100)).await;++ // Stop the stream. This should wait for the thread to cleanly exit+ // without aborting it. If the fix is working, this should complete+ // without crashes, and the thread should have properly paused and+ // dropped the cpal stream.+ let stop_result = stream.stop().await;+ assert!(+ stop_result.is_ok(),+ "stream.stop() should complete without error"+ );++ // A small delay to ensure no deferred crashes happen+ tokio::time::sleep(Duration::from_millis(100)).await;++ // If we got here without a panic, the fix is working correctly.+ // The thread exited cleanly without the CoreAudio IO callback+ // attempting to access freed memory.+ }}