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: prevent Pi double-start race between frontend and auto-start

Louis Beaumontb42010903ba8
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index 7d352b0f1..37d4e07f6 100755
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -8097,7 +8097,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.0.465"
+version = "2.0.466"
dependencies = [
"anyhow",
"arboard",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index dfaa81dec..5bef9fe42 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "2.0.466"
+version = "2.0.467"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index a6f9ab907..5545613f9 100755
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -1726,6 +1726,15 @@ async fn main() {
let pi_state_clone = pi_state.inner().clone();
// Retry up to 3 times (Pi might still be installing)
for attempt in 1..=3u32 {
+ // Skip if Pi was already started (e.g. by frontend preset change)
+ {
+ let mut guard = pi_state_clone.0.lock().await;
+ if guard.as_mut().map(|m| m.is_running()).unwrap_or(false) {
+ info!("Pi already running, skipping auto-start");
+ break;
+ }
+ }
+
info!("Auto-starting Pi agent (attempt {}/3) with provider: {}, model: {}", attempt, provider_config.provider, provider_config.model);
let result = pi::pi_start_inner(
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index 9a6800035..923c7bca5 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -216,6 +216,19 @@ impl PiManager {
}
let cmd_str = serde_json::to_string(&cmd).map_err(|e| e.to_string())?;
+
+ // Log raw FD and child PID for pipe debugging
+ #[cfg(unix)]
+ {
+ use std::os::unix::io::AsRawFd;
+ let fd = stdin.as_raw_fd();
+ let child_pid = self.child.as_ref().map(|c| c.id());
+ info!("Sending to Pi (req_{}): type={}, stdin_fd={}, child_pid={:?}, bytes={}",
+ self.request_id,
+ cmd.get("type").and_then(|t| t.as_str()).unwrap_or("?"),
+ fd, child_pid, cmd_str.len() + 1);
+ }
+ #[cfg(not(unix))]
info!("Sending to Pi (req_{}): type={}", self.request_id, cmd.get("type").and_then(|t| t.as_str()).unwrap_or("?"));
writeln!(stdin, "{}", cmd_str).map_err(|e| format!("Failed to write to Pi stdin: {}", e))?;
@@ -572,7 +585,8 @@ pub async fn pi_start_inner(
// Stop any existing instance
if let Some(m) = manager_guard.as_mut() {
if m.is_running() {
- info!("Stopping existing pi instance");
+ let old_pid = m.child.as_ref().map(|c| c.id());
+ info!("Stopping existing pi instance (pid {:?}) to start new one", old_pid);
m.stop();
}
}
@@ -595,7 +609,8 @@ pub async fn pi_start_inner(
}
};
- info!("Starting pi from {} in dir: {} with provider: {} model: {}", pi_path, project_dir, pi_provider, pi_model);
+ let bun_path = find_bun_executable().unwrap_or_default();
+ info!("Starting pi from {} in dir: {} with provider: {} model: {} bun: {}", pi_path, project_dir, pi_provider, pi_model, bun_path);
// Build command — use cmd.exe /C wrapper for .cmd files on Windows (Rust 1.77+ CVE fix)
let mut cmd = build_command_for_path(&pi_path);