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 SCREEN_IS_LOCKED from getting stuck after sleep/wake
Louis Beaumont5843200cb2a0
diff --git a/crates/screenpipe-engine/src/sleep_monitor.rs b/crates/screenpipe-engine/src/sleep_monitor.rsindex 730556fe3..b138fb5a4 100644--- a/crates/screenpipe-engine/src/sleep_monitor.rs+++ b/crates/screenpipe-engine/src/sleep_monitor.rs@@ -243,7 +243,26 @@ pub fn start_sleep_monitor() {}});- // Thread 2: NSWorkspace notification observers for system sleep/wake.+ // Thread 2: Safety-net CGSession poller. The CFNotificationCenter above is+ // event-driven but notifications can be lost during sleep/wake transitions or+ // if the CFRunLoop thread stalls. This poll catches any missed unlock within 5s.+ std::thread::spawn(|| {+ loop {+ std::thread::sleep(std::time::Duration::from_secs(5));++ let locked = check_screen_locked_cgsession();+ let was_locked = SCREEN_IS_LOCKED.swap(locked, Ordering::SeqCst);+ if locked != was_locked {+ if locked {+ info!("Screen locked (CGSession safety-net poll)");+ } else {+ info!("Screen unlocked (CGSession safety-net poll)");+ }+ }+ }+ });++ // Thread 3: NSWorkspace notification observers for system sleep/wake.// These are still useful for the RECENTLY_WOKE flag and telemetry.std::thread::spawn(move || {let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {@@ -307,13 +326,29 @@ fn on_did_wake(handle: &tokio::runtime::Handle) {// Mark that we recently wokeRECENTLY_WOKE.store(true, Ordering::SeqCst);+ // Immediately re-check screen lock state via CGSession.+ // The CFNotificationCenter unlock notification can be lost during sleep/wake,+ // so we must poll here to avoid SCREEN_IS_LOCKED getting stuck true forever.+ let locked = check_screen_locked_cgsession();+ let was_locked = SCREEN_IS_LOCKED.swap(locked, Ordering::SeqCst);+ if was_locked && !locked {+ // CFNotification missed the unlock — we're fixing it here+ }+// 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.handle.spawn(async {- // Wait 5 seconds for system to stabilize+ // Wait 5 seconds for system to stabilize, then re-check lock state again.+ // The first check in on_did_wake may be too early (display not fully awake).tokio::time::sleep(Duration::from_secs(5)).await;+ let locked = check_screen_locked_cgsession();+ 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)");+ }+// Check if recording is healthylet (audio_healthy, vision_healthy) = check_recording_health().await;diff --git a/crates/screenpipe-engine/src/vision_manager/manager.rs b/crates/screenpipe-engine/src/vision_manager/manager.rsindex ea93f7d3a..5878902e6 100644--- a/crates/screenpipe-engine/src/vision_manager/manager.rs+++ b/crates/screenpipe-engine/src/vision_manager/manager.rs@@ -331,8 +331,28 @@ impl VisionManager {}}- /// Get list of currently recording monitor IDs+ /// Get list of currently recording monitor IDs.+ /// Removes dead tasks (finished JoinHandles) so MonitorWatcher can restart them.pub async fn active_monitors(&self) -> Vec<u32> {+ // Collect dead task IDs first to avoid holding DashMap refs during removal+ let dead: Vec<u32> = self+ .recording_tasks+ .iter()+ .filter(|entry| entry.value().is_finished())+ .map(|entry| *entry.key())+ .collect();++ for id in &dead {+ if let Some((_, handle)) = self.recording_tasks.remove(id) {+ warn!(+ "Monitor {} capture task died unexpectedly, removing so it can be restarted",+ id+ );+ // Await to clean up the JoinHandle+ let _ = handle.await;+ }+ }+self.recording_tasks.iter().map(|entry| *entry.key())