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: invalidate persistent SCStream after screen unlock/wake on macOS
TerryChengTWfccb154d392e
diff --git a/crates/screenpipe-engine/src/event_driven_capture.rs b/crates/screenpipe-engine/src/event_driven_capture.rsindex 41a1b856c..46d7b03ce 100644--- a/crates/screenpipe-engine/src/event_driven_capture.rs+++ b/crates/screenpipe-engine/src/event_driven_capture.rs@@ -334,6 +334,14 @@ pub async fn event_driven_capture_loop(continue;}+ // After unlock or wake, invalidate persistent SCStream handles so+ // the next capture picks up fresh frames instead of stale ones.+ #[cfg(target_os = "macos")]+ if screenpipe_screen::stream_invalidation::take() {+ info!("invalidating persistent streams after unlock/wake for monitor {}", monitor_id);+ screenpipe_screen::stream_invalidation::invalidate_streams();+ }+// Skip capture while DRM streaming content is focusedif crate::drm_detector::drm_content_paused() {tokio::time::sleep(poll_interval).await;diff --git a/crates/screenpipe-engine/src/sleep_monitor.rs b/crates/screenpipe-engine/src/sleep_monitor.rsindex c1f774caf..fabb04c79 100644--- a/crates/screenpipe-engine/src/sleep_monitor.rs+++ b/crates/screenpipe-engine/src/sleep_monitor.rs@@ -198,6 +198,10 @@ pub fn start_sleep_monitor() {let was_locked = SCREEN_IS_LOCKED.swap(false, Ordering::SeqCst);if was_locked {// State change logged via safety-net poll below if needed.+ // Request invalidation of persistent SCStream handles so+ // the capture loop recreates them with fresh frames.+ #[cfg(target_os = "macos")]+ screenpipe_screen::stream_invalidation::request();}}@@ -256,6 +260,8 @@ pub fn start_sleep_monitor() {info!("Screen locked (CGSession safety-net poll)");} else {info!("Screen unlocked (CGSession safety-net poll)");+ #[cfg(target_os = "macos")]+ screenpipe_screen::stream_invalidation::request();}}});@@ -333,6 +339,11 @@ fn on_did_wake(handle: &tokio::runtime::Handle) {// CFNotification missed the unlock — we're fixing it here}+ // Invalidate persistent SCStream handles so the capture loop+ // recreates them with fresh frames after wake.+ #[cfg(target_os = "macos")]+ screenpipe_screen::stream_invalidation::request();+// Spawn a task on the captured tokio runtime handle to check recording// health after a short delay. We can't use bare tokio::spawn() here// because this callback runs on an NSRunLoop thread, not a tokio thread.@@ -345,6 +356,8 @@ fn on_did_wake(handle: &tokio::runtime::Handle) {let was_locked = SCREEN_IS_LOCKED.swap(locked, Ordering::SeqCst);if was_locked && !locked {info!("Screen unlocked after wake (CGSession safety-net cleared SCREEN_IS_LOCKED)");+ #[cfg(target_os = "macos")]+ screenpipe_screen::stream_invalidation::request();}// Check if recording is healthydiff --git a/crates/screenpipe-screen/src/lib.rs b/crates/screenpipe-screen/src/lib.rsindex d98220f42..c473c1714 100644--- a/crates/screenpipe-screen/src/lib.rs+++ b/crates/screenpipe-screen/src/lib.rs@@ -29,3 +29,31 @@ pub use microsoft::perform_ocr_windows;pub use tesseract::perform_ocr_tesseract;pub mod browser_utils;pub mod snapshot_writer;++/// Flag to request invalidation of persistent SCStream handles after screen+/// unlock or wake. Set by `sleep_monitor` (CFNotification callback thread),+/// consumed by the event-driven capture loop (tokio thread) so that the actual+/// stream teardown happens in a safe context — not inside the C callback.+#[cfg(target_os = "macos")]+pub mod stream_invalidation {+ use std::sync::atomic::{AtomicBool, Ordering};+ static NEEDS_INVALIDATION: AtomicBool = AtomicBool::new(false);++ /// Request that all persistent SCStream handles be invalidated.+ /// Safe to call from any thread (including CFNotification callbacks).+ pub fn request() {+ NEEDS_INVALIDATION.store(true, Ordering::SeqCst);+ }++ /// Check and clear the invalidation flag. If `true`, the caller should+ /// call `invalidate_streams()` to tear down stale SCStream handles.+ pub fn take() -> bool {+ NEEDS_INVALIDATION.swap(false, Ordering::SeqCst)+ }++ /// Stop all persistent SCStream handles. The next capture call will+ /// lazily recreate them with fresh frames.+ pub fn invalidate_streams() {+ sck_rs::stop_all_streams();+ }+}