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 duplicate pipe execution with PID file lock

Louis Beaumont48b1c6fb5a8a
diff --git a/Cargo.lock b/Cargo.lock
index d52ee552b..f08c0001a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6737,7 +6737,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"audiopipe",
@@ -6797,7 +6797,7 @@ dependencies = [
[[package]]
name = "screenpipe-config"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"dirs 6.0.0",
"serde",
@@ -6810,7 +6810,7 @@ dependencies = [
[[package]]
name = "screenpipe-connect"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"async-trait",
@@ -6839,7 +6839,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -6885,7 +6885,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"chrono",
@@ -6911,7 +6911,7 @@ dependencies = [
[[package]]
name = "screenpipe-engine"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"async-trait",
@@ -6981,7 +6981,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"chrono",
@@ -6999,7 +6999,7 @@ dependencies = [
[[package]]
name = "screenpipe-screen"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"accessibility-sys",
"anyhow",
@@ -7039,7 +7039,7 @@ dependencies = [
[[package]]
name = "screenpipe-vault"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"argon2",
"chacha20poly1305",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index ed3072a43..5d6c63373 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.24"
+version = "2.3.26"
dependencies = [
"anyhow",
"arboard",
@@ -9744,7 +9744,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"audiopipe",
@@ -9799,7 +9799,7 @@ dependencies = [
[[package]]
name = "screenpipe-config"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"dirs 6.0.0",
"serde",
@@ -9811,7 +9811,7 @@ dependencies = [
[[package]]
name = "screenpipe-connect"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"async-trait",
@@ -9840,7 +9840,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -9885,7 +9885,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"chrono",
@@ -9910,7 +9910,7 @@ dependencies = [
[[package]]
name = "screenpipe-engine"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"async-trait",
@@ -9973,7 +9973,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"anyhow",
"chrono",
@@ -9989,7 +9989,7 @@ dependencies = [
[[package]]
name = "screenpipe-screen"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"accessibility-sys",
"anyhow",
@@ -10025,7 +10025,7 @@ dependencies = [
[[package]]
name = "screenpipe-vault"
-version = "0.3.261"
+version = "0.3.263"
dependencies = [
"argon2",
"chacha20poly1305",
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 40ae7fac1..2799c02e4 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -1552,11 +1552,37 @@ impl PipeManager {
{
let mut running = self.running.lock().await;
if running.contains_key(name) {
- return Err(anyhow!("pipe '{}' is already running", name));
+ return Err(anyhow!(
+ "pipe '{}' is already running — you may already be executing inside this pipe. \
+ Do NOT run `screenpipe pipe run` from within a pipe.",
+ name
+ ));
}
running.insert(name.to_string(), ExecutionHandle { pid: 0 });
}
+ // Defense-in-depth: check PID file (cross-process lock)
+ if let Some(existing_pid) = read_pid_file(&self.pipes_dir, name) {
+ if is_process_alive(existing_pid) {
+ let mut running = self.running.lock().await;
+ running.remove(name);
+ return Err(anyhow!(
+ "pipe '{}' is already running (pid {}) — another process is executing this pipe. \
+ Do NOT run `screenpipe pipe run` from within a pipe.",
+ name, existing_pid
+ ));
+ } else {
+ info!(
+ "start_pipe_background: pipe '{}' has stale PID file (pid {}), cleaning up",
+ name, existing_pid
+ );
+ remove_pid_file(&self.pipes_dir, name);
+ }
+ }
+
+ // Write a pre-emptive PID file to claim the lock before spawn
+ write_pid_file(&self.pipes_dir, name, std::process::id());
+
// Resolve preset
let (run_model, run_provider, run_provider_url, run_api_key, preset_prompt) =
if let Some(preset_id) = config.preset.first() {
@@ -1952,12 +1978,39 @@ impl PipeManager {
{
let mut running = self.running.lock().await;
if running.contains_key(name) {
- return Err(anyhow!("pipe '{}' is already running", name));
+ return Err(anyhow!(
+ "pipe '{}' is already running — you may already be executing inside this pipe. \
+ Do NOT run `screenpipe pipe run` from within a pipe.",
+ name
+ ));
}
// Placeholder handle; real PID comes via SharedPid
running.insert(name.to_string(), ExecutionHandle { pid: 0 });
}
+ // Defense-in-depth: check PID file (cross-process lock)
+ if let Some(existing_pid) = read_pid_file(&self.pipes_dir, name) {
+ if is_process_alive(existing_pid) {
+ // Undo the running insert since we're bailing out
+ let mut running = self.running.lock().await;
+ running.remove(name);
+ return Err(anyhow!(
+ "pipe '{}' is already running (pid {}) — another process is executing this pipe. \
+ Do NOT run `screenpipe pipe run` from within a pipe.",
+ name, existing_pid
+ ));
+ } else {
+ info!(
+ "run_pipe: pipe '{}' has stale PID file (pid {}), cleaning up",
+ name, existing_pid
+ );
+ remove_pid_file(&self.pipes_dir, name);
+ }
+ }
+
+ // Write a pre-emptive PID file to claim the lock before spawn
+ write_pid_file(&self.pipes_dir, name, std::process::id());
+
let started_at = Utc::now();
let pipe_dir = self.pipes_dir.join(name);
@@ -2937,6 +2990,9 @@ impl PipeManager {
r.insert(name.clone(), ExecutionHandle { pid: 0 });
}
+ // Write pre-emptive PID file to claim the lock before spawn
+ write_pid_file(&pipes_dir, name, std::process::id());
+
// Resolve preset → model/provider overrides (same as run_pipe)
let (model, provider, provider_url, api_key, preset_prompt) = if let Some(
preset_id,
@@ -3563,7 +3619,7 @@ fn render_pipe_system_prompt(body: &str, api_port: u16, system_prompt: Option<&s
}
sys.push_str(&format!(
- "OS: {os}\nOutput directory: ./output/\nScreenpipe API: http://localhost:{api_port}\nPrefer bun/TypeScript for scripts. Python may not be installed.\nSend notifications via POST http://localhost:11435/notify with {{\"title\": \"...\", \"body\": \"...\"}}. Body supports markdown. File links MUST use absolute paths (e.g. [View log](/Users/me/file.md)), never relative paths like ./output/file.md — relative paths break the notification link handler.\n\n"
+ "CRITICAL: You ARE this pipe. You are already running inside it. NEVER run `screenpipe pipe run` — that would create a recursive duplicate. Execute the task directly using the tools available to you (bash, file I/O, HTTP requests, etc.).\n\nOS: {os}\nOutput directory: ./output/\nScreenpipe API: http://localhost:{api_port}\nPrefer bun/TypeScript for scripts. Python may not be installed.\nSend notifications via POST http://localhost:11435/notify with {{\"title\": \"...\", \"body\": \"...\"}}. Body supports markdown. File links MUST use absolute paths (e.g. [View log](/Users/me/file.md)), never relative paths like ./output/file.md — relative paths break the notification link handler.\n\n"
));
sys.push_str(body);
sys
@@ -3613,7 +3669,7 @@ Pipe name: {}
prompt.push_str(ctx);
}
- prompt.push_str("\nExecute the pipe now.");
+ prompt.push_str("\nDo the work described above now. Do NOT re-run this pipe via CLI.");
prompt
}
@@ -4245,7 +4301,7 @@ mod tests {
let prompt = render_prompt_with_port(&config, "body text", 3031, None, None);
// User prompt contains time range and the "Execute" instruction
assert!(prompt.contains("Time range:"));
- assert!(prompt.contains("Execute the pipe now."));
+ assert!(prompt.contains("Do the work described above now."));
// Port / body go into system prompt, not user prompt
let sys = render_pipe_system_prompt("body text", 3031, None);
assert!(sys.contains("http://localhost:3031"));
@@ -4325,6 +4381,13 @@ mod tests {
assert!(sys.contains("body text"));
}
+ #[test]
+ fn test_system_prompt_contains_anti_recursion_warning() {
+ let sys = render_pipe_system_prompt("task body", 3030, None);
+ assert!(sys.contains("NEVER run `screenpipe pipe run`"));
+ assert!(sys.contains("You ARE this pipe"));
+ }
+
// -- PipeExecution / SchedulerState serde roundtrip ----------------------
#[test]