Skip to content

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

fix: DRM detection false positives and stop/start race condition

Louis Beaumontdc02803cda97
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/health.rs b/apps/screenpipe-app-tauri/src-tauri/src/health.rs
index 9946ac00d..a91a7f4a1 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/health.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/health.rs
@@ -10,7 +10,7 @@ use std::sync::RwLock;
use std::time::Instant;
use tauri::{path::BaseDirectory, Emitter, Manager};
use tokio::time::{interval, Duration};
-use tracing::{error, info, warn};
+use tracing::{debug, error, info, warn};
/// How long after startup to treat connection errors as "starting up" instead of "error".
/// The recording server needs time to load whisper models, FFmpeg, etc.
@@ -315,6 +315,7 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
// DRM pause state — tracked here because engine memory is lost on stop_screenpipe
let mut drm_stopped = false;
+ let mut drm_stop_time: Option<Instant> = None;
// Capture stall detection state
let mut consecutive_audio_stall: u32 = 0;
@@ -475,30 +476,41 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
// When the engine detects DRM streaming content (Netflix, etc.),
// stop the entire recording pipeline — exactly like the "stop recording"
// button. This fully releases ScreenCaptureKit so DRM doesn't black out.
+ //
+ // IMPORTANT: Stop and start are serialized — we never emit start in the
+ // same iteration as stop, and we enforce a cooldown between DRM stop and
+ // DRM resume to prevent the rapid stop/start race that killed the server.
if let Ok(ref health) = health_result {
if health.drm_content_paused && !drm_stopped {
info!("DRM content detected — calling stop_screenpipe to fully release screen recording");
let _ = app.emit("shortcut-stop-recording", ());
drm_stopped = true;
+ drm_stop_time = Some(Instant::now());
+ // Skip resume check this iteration — let the stop complete first
}
}
// Auto-resume: when server is down due to DRM, poll the focused app.
- // If user switched to a non-streaming, non-browser app, restart recording.
- // Also try when server responds (it may still be shutting down).
+ // Wait at least 5s after stop to let shutdown complete before attempting restart.
if drm_stopped {
- info!("DRM stopped state active, health_ok={}, checking focused app for resume", health_result.is_ok());
- }
- if drm_stopped {
- let should_resume = tokio::task::spawn_blocking(|| {
- // poll_drm_clear returns true = still DRM, false = cleared
- !screenpipe_engine::drm_detector::poll_drm_clear()
- }).await.unwrap_or(false);
- if should_resume {
- info!("DRM content no longer focused — auto-restarting recording");
- let _ = app.emit("shortcut-start-recording", ());
- drm_stopped = false;
- // Give the server time to start before checking health again
- last_restart_triggered = Some(Instant::now());
+ let stop_elapsed = drm_stop_time.map(|t| t.elapsed()).unwrap_or_default();
+ if stop_elapsed < Duration::from_secs(5) {
+ debug!(
+ "DRM stop cooldown: {:.1}s elapsed, waiting for 5s before resume check",
+ stop_elapsed.as_secs_f64()
+ );
+ } else {
+ let should_resume = tokio::task::spawn_blocking(|| {
+ // poll_drm_clear returns true = still DRM, false = cleared
+ !screenpipe_engine::drm_detector::poll_drm_clear()
+ }).await.unwrap_or(false);
+ if should_resume {
+ info!("DRM content no longer focused — auto-restarting recording");
+ let _ = app.emit("shortcut-start-recording", ());
+ drm_stopped = false;
+ drm_stop_time = None;
+ // Give the server time to start before checking health again
+ last_restart_triggered = Some(Instant::now());
+ }
}
}
// Clear drm_stopped if server came back and DRM flag is no longer set
@@ -507,6 +519,7 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
if let Ok(ref health) = health_result {
if !health.drm_content_paused {
drm_stopped = false;
+ drm_stop_time = None;
}
}
}
diff --git a/crates/screenpipe-engine/src/drm_detector.rs b/crates/screenpipe-engine/src/drm_detector.rs
index 8fc89f193..a26d9d2d6 100644
--- a/crates/screenpipe-engine/src/drm_detector.rs
+++ b/crates/screenpipe-engine/src/drm_detector.rs
@@ -173,32 +173,6 @@ pub fn check_and_update_drm_state(
}
}
-/// Known browser app names that can host DRM content in tabs.
-/// When one of these is focused, we can't determine the URL without the
-/// accessibility tree (which requires an active capture), so we keep the
-/// DRM pause active. Only clears when user switches to a non-browser app.
-const BROWSER_APPS: &[&str] = &[
- "arc",
- "google chrome",
- "chrome",
- "safari",
- "firefox",
- "microsoft edge",
- "edge",
- "brave browser",
- "brave",
- "opera",
- "vivaldi",
- "chromium",
- "zen browser",
- "orion",
-];
-
-fn is_browser(app_name: &str) -> bool {
- let lower = app_name.to_lowercase();
- BROWSER_APPS.iter().any(|&b| lower.contains(b))
-}
-
/// Query the current foreground app and check if DRM is still active.
/// Called from the Tauri health check to decide when to auto-restart recording.
///
@@ -223,13 +197,14 @@ pub fn poll_drm_clear() -> bool {
if is_drm_app(&app_name) {
debug!("DRM app still focused: {}", app_name);
true
- } else if is_browser(&app_name) {
- debug!(
- "browser '{}' focused — keeping DRM pause (can't check URL)",
- app_name
- );
- true
} else {
+ // Clear DRM pause when ANY non-DRM app is focused, including browsers.
+ // The previous behavior kept DRM paused when a browser was focused
+ // (because we can't check the URL without capture running), but this
+ // caused false positives: switching tabs away from a streaming site
+ // would keep capture paused indefinitely. If the user switches back
+ // to a DRM tab, check_and_update_drm_state() will re-trigger on the
+ // next capture cycle.
info!("focused app is now '{}' — clearing DRM pause", app_name);
set_drm_paused(false);
false
@@ -357,14 +332,4 @@ mod tests {
assert!(!drm_content_paused());
}
- #[test]
- fn test_is_browser() {
- assert!(is_browser("Arc"));
- assert!(is_browser("Google Chrome"));
- assert!(is_browser("Safari"));
- assert!(is_browser("Firefox"));
- assert!(!is_browser("Finder"));
- assert!(!is_browser("Terminal"));
- assert!(!is_browser("WezTerm"));
- }
}