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: recover from stale Tauri store resource ID after in-place updates on Windows

Louis Beaumont74c62af4eb1d
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index 216417825..0bdb32cb0 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -9642,7 +9642,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.3.33"
+version = "2.3.34"
dependencies = [
"anyhow",
"arboard",
@@ -9744,7 +9744,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"anyhow",
"audiopipe",
@@ -9799,7 +9799,7 @@ dependencies = [
[[package]]
name = "screenpipe-config"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"dirs 6.0.0",
"serde",
@@ -9811,7 +9811,7 @@ dependencies = [
[[package]]
name = "screenpipe-connect"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"anyhow",
"async-trait",
@@ -9840,7 +9840,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -9873,6 +9873,7 @@ dependencies = [
"serde_json",
"serde_yaml",
"sha2 0.10.9",
+ "tempfile",
"thiserror 1.0.69",
"tokio",
"tracing",
@@ -9885,7 +9886,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"anyhow",
"chrono",
@@ -9910,7 +9911,7 @@ dependencies = [
[[package]]
name = "screenpipe-engine"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"anyhow",
"async-trait",
@@ -9973,7 +9974,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"anyhow",
"chrono",
@@ -9989,7 +9990,7 @@ dependencies = [
[[package]]
name = "screenpipe-screen"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"accessibility-sys",
"anyhow",
@@ -10025,7 +10026,7 @@ dependencies = [
[[package]]
name = "screenpipe-vault"
-version = "0.3.266"
+version = "0.3.267"
dependencies = [
"argon2",
"chacha20poly1305",
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/store.rs b/apps/screenpipe-app-tauri/src-tauri/src/store.rs
index 23bcf3cf6..4f23b1c28 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/store.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/store.rs
@@ -2,57 +2,93 @@ use super::get_base_dir;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use specta::Type;
-use std::sync::{Arc, OnceLock};
+use std::sync::{Arc, Mutex};
use tauri::AppHandle;
use tauri_plugin_store::StoreBuilder;
use tracing::error;
-/// Cached store instance — built once, reused for the lifetime of the process.
-/// Avoids TOCTOU race in StoreBuilder::build() when called multiple times during
-/// startup (settings init, onboarding init, tray setup all call get_store).
-static STORE_CACHE: OnceLock<Arc<tauri_plugin_store::Store<tauri::Wry>>> = OnceLock::new();
-
-pub fn get_store(
- app: &AppHandle,
- _profile_name: Option<String>, // Keep parameter for API compatibility but ignore it
-) -> anyhow::Result<Arc<tauri_plugin_store::Store<tauri::Wry>>> {
- if let Some(cached) = STORE_CACHE.get() {
- return Ok(cached.clone());
- }
+/// Cached store instance — reusable across the process lifetime.
+/// Uses Mutex instead of OnceLock so the cache can be invalidated when the
+/// Tauri resource table drops the underlying store (e.g. after an in-place
+/// update restart on Windows where resource IDs become stale).
+static STORE_CACHE: Mutex<Option<Arc<tauri_plugin_store::Store<tauri::Wry>>>> =
+ Mutex::new(None);
+/// Build (or rebuild) the store, retrying on TOCTOU races and stale resource IDs.
+fn build_store(app: &AppHandle) -> anyhow::Result<Arc<tauri_plugin_store::Store<tauri::Wry>>> {
let base_dir = get_base_dir(app, None)?;
let store_path = base_dir.join("store.bin");
- // Retry with backoff to handle TOCTOU race (EEXIST / os error 17)
- // when multiple instances or threads race to create store.bin.
let mut last_err = None;
- let store = 'retry: {
- for attempt in 0..3u32 {
- match StoreBuilder::new(app, store_path.clone()).build() {
- Ok(s) => break 'retry s,
- Err(e) => {
- let msg = e.to_string();
- if msg.contains("os error 17") || msg.contains("File exists") {
- tracing::warn!(
- "store build race (attempt {}): {}, retrying",
- attempt + 1,
- msg
- );
- std::thread::sleep(std::time::Duration::from_millis(
- 100 * (attempt as u64 + 1),
- ));
- last_err = Some(e);
- continue;
+ for attempt in 0..3u32 {
+ match StoreBuilder::new(app, store_path.clone()).build() {
+ Ok(s) => return Ok(s),
+ Err(e) => {
+ let msg = e.to_string();
+ if msg.contains("os error 17") || msg.contains("File exists") {
+ tracing::warn!(
+ "store build race (attempt {}): {}, retrying",
+ attempt + 1,
+ msg
+ );
+ std::thread::sleep(std::time::Duration::from_millis(
+ 100 * (attempt as u64 + 1),
+ ));
+ last_err = Some(e);
+ continue;
+ }
+ // After cleanup_before_exit or in-place update on Windows, the
+ // resources_table is cleared but StoreState.stores still holds the
+ // old resource ID. Force a fresh store via create_new to evict it.
+ if msg.contains("resource id") && msg.contains("invalid") {
+ tracing::warn!(
+ "store resource stale (attempt {}): {}, rebuilding fresh",
+ attempt + 1,
+ msg
+ );
+ match StoreBuilder::new(app, store_path.clone())
+ .create_new()
+ .build()
+ {
+ Ok(s) => return Ok(s),
+ Err(e2) => {
+ tracing::warn!("fresh store build also failed: {}", e2);
+ last_err = Some(e);
+ continue;
+ }
}
- return Err(anyhow::anyhow!(e));
}
+ return Err(anyhow::anyhow!(e));
}
}
- return Err(anyhow::anyhow!(last_err.unwrap()));
- };
+ }
+ Err(anyhow::anyhow!(last_err.unwrap()))
+}
- // If another thread raced us, use their instance
- Ok(STORE_CACHE.get_or_init(|| store).clone())
+pub fn get_store(
+ app: &AppHandle,
+ _profile_name: Option<String>, // Keep parameter for API compatibility but ignore it
+) -> anyhow::Result<Arc<tauri_plugin_store::Store<tauri::Wry>>> {
+ let mut guard = STORE_CACHE.lock().unwrap_or_else(|e| e.into_inner());
+
+ if let Some(ref cached) = *guard {
+ return Ok(cached.clone());
+ }
+
+ let store = build_store(app)?;
+ *guard = Some(store.clone());
+ Ok(store)
+}
+
+/// Invalidate the cached store so the next `get_store` call rebuilds it.
+/// Called when a "resource id … is invalid" error is detected.
+pub fn invalidate_store_cache() {
+ if let Ok(mut guard) = STORE_CACHE.lock() {
+ if guard.is_some() {
+ tracing::warn!("store cache invalidated — will rebuild on next access");
+ *guard = None;
+ }
+ }
}
#[derive(Serialize, Deserialize, Type, Clone)]
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/tray.rs b/apps/screenpipe-app-tauri/src-tauri/src/tray.rs
index 7644665fe..1d27f77aa 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/tray.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/tray.rs
@@ -952,7 +952,13 @@ pub fn setup_tray_menu_updater(app: AppHandle, update_item: &tauri::menu::MenuIt
break;
}
if let Err(e) = update_menu_if_needed(&app, &update_item).await {
- error!("Failed to update tray menu: {:#}", e);
+ let msg = format!("{:#}", e);
+ error!("Failed to update tray menu: {}", msg);
+ // Tauri resource table can go stale after in-place updates on
+ // Windows — invalidate the cached store so the next tick rebuilds it.
+ if msg.contains("resource id") && msg.contains("invalid") {
+ crate::store::invalidate_store_cache();
+ }
}
}
});