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: properly wait for UI recorder tasks before exit
Louis Beaumont1b36f62df6b6
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rsindex 3d0c02a90..5d211e0fa 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs@@ -42,6 +42,31 @@ impl EmbeddedServerHandle {}let _ = self.shutdown_tx.send(());}++ /// Signal shutdown AND wait for the UI recorder tasks to finish.+ /// This prevents the crash where the runtime is torn down while+ /// the tree walker or event processor is still running.+ pub async fn shutdown_and_wait(mut self) {+ info!("Shutting down embedded screenpipe server (waiting for tasks)");+ // Signal stop first+ if let Some(ref ui_handle) = self.ui_recorder_handle {+ ui_handle.stop();+ }+ let _ = self.shutdown_tx.send(());++ // Now wait for UI recorder tasks to actually finish+ if let Some(ui_handle) = self.ui_recorder_handle.take() {+ info!("Waiting for UI recorder tasks to finish...");+ // Timeout so we don't hang forever if a task is stuck+ match tokio::time::timeout(+ Duration::from_secs(5),+ ui_handle.join(),+ ).await {+ Ok(()) => info!("UI recorder tasks finished cleanly"),+ Err(_) => warn!("UI recorder tasks did not finish within 5s, proceeding with exit"),+ }+ }+ }}/// Start the embedded screenpipe serverdiff --git a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs b/apps/screenpipe-app-tauri/src-tauri/src/recording.rsindex e5ff9839d..27727453f 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs@@ -119,10 +119,10 @@ pub async fn stop_screenpipe(let mut handle_guard = state.handle.lock().await;if let Some(handle) = handle_guard.take() {- handle.shutdown();+ handle.shutdown_and_wait().await;state.is_starting.store(false, Ordering::SeqCst);// Wait for the old server to fully release port 3030- tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;+ tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;info!("Screenpipe server stopped");Ok(())} else {@@ -204,8 +204,7 @@ pub async fn spawn_screenpipe(}// Shut it down — we're restarting intentionallyif let Some(handle) = handle_guard.take() {- handle.shutdown();- tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;+ handle.shutdown_and_wait().await;}}}diff --git a/apps/screenpipe-app-tauri/src-tauri/src/tray.rs b/apps/screenpipe-app-tauri/src-tauri/src/tray.rsindex f8ab7255a..42ed61962 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/tray.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/tray.rs@@ -540,10 +540,10 @@ fn handle_menu_event(app_handle: &AppHandle, event: tauri::menu::MenuEvent) {if let Some(recording_state) = app_handle_clone.try_state::<RecordingState>() {let mut handle_guard = recording_state.handle.lock().await;if let Some(handle) = handle_guard.take() {- handle.shutdown();- // Give the tree walker and other tasks time to notice the stop- // signal and exit cleanly before we tear down the runtime.- tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;+ // Wait for UI recorder tasks to actually finish before exiting.+ // This prevents the crash where the runtime tears down while+ // the tree walker is still mid-DB-write.+ handle.shutdown_and_wait().await;info!("Screenpipe recording stopped successfully");} else {debug!("No recording running to stop");