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: schedule deferred restart when cooldown blocks spawn_screenpipe

Louis Beaumont41cebdf35e49
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx
index cad80fd8e..d7497b6c3 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-permission-monitor.tsx
@@ -78,8 +78,21 @@ export function usePermissionMonitor() {
}, 300000);
});
+ // Listen for deferred restart requests from the cooldown logic in recording.rs.
+ // When a restart is blocked by cooldown, the backend schedules a deferred check
+ // and emits this event if the server is still dead after cooldown expires.
+ const unlistenRestart = listen("request-server-restart", async () => {
+ console.log("Deferred server restart requested by backend");
+ try {
+ await commands.spawnScreenpipe(null);
+ } catch (error) {
+ console.error("Deferred server restart failed:", error);
+ }
+ });
+
return () => {
unlisten.then((fn) => fn());
+ unlistenRestart.then((fn) => fn());
};
}, [pathname]);
}
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
index 417d2961e..d1c00fc89 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
@@ -158,9 +158,50 @@ pub async fn spawn_screenpipe(
let last_spawn = state.last_spawn_epoch.load(Ordering::SeqCst);
if last_spawn > 0 && now_epoch.saturating_sub(last_spawn) < RESTART_COOLDOWN_SECS {
let remaining = RESTART_COOLDOWN_SECS - now_epoch.saturating_sub(last_spawn);
- warn!(
- "Restart cooldown active ({remaining}s remaining). Skipping spawn to prevent restart spam."
- );
+ warn!("Restart cooldown active ({remaining}s remaining). Deferring spawn.");
+ // Schedule a deferred restart after cooldown expires so the server
+ // doesn't stay dead if no other caller triggers spawn_screenpipe.
+ let last_spawn_epoch = state.last_spawn_epoch.clone();
+ let is_starting = state.is_starting.clone();
+ let handle = state.handle.clone();
+ let app_handle = app.clone();
+ tokio::spawn(async move {
+ tokio::time::sleep(std::time::Duration::from_secs(remaining + 1)).await;
+ info!("Cooldown expired, checking if server needs restart");
+ // Check health first — if server is already running, skip
+ let port = SettingsStore::get(&app_handle)
+ .ok()
+ .flatten()
+ .map(|s| s.recording.port)
+ .unwrap_or(3030);
+ if let Ok(resp) = reqwest::Client::new()
+ .get(format!("http://localhost:{}/health", port))
+ .timeout(std::time::Duration::from_secs(2))
+ .send()
+ .await
+ {
+ if resp.status().is_success() {
+ info!("Deferred spawn: server already healthy, skipping");
+ return;
+ }
+ }
+ // Server not running — check if handle exists
+ {
+ let handle_guard = handle.lock().await;
+ if handle_guard.is_some() {
+ info!(
+ "Deferred spawn: handle exists but unhealthy, skipping (may be starting)"
+ );
+ return;
+ }
+ }
+ // No handle, not healthy — trigger restart via invoke
+ info!("Deferred spawn: server dead, triggering restart");
+ is_starting.store(false, Ordering::SeqCst);
+ // Reset cooldown so the deferred spawn isn't blocked again
+ last_spawn_epoch.store(0, Ordering::SeqCst);
+ let _ = app_handle.emit("request-server-restart", ());
+ });
return Ok(());
}