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(recording): skip own PID in kill_process_on_port, wait for port release

Louis Beaumont4a3313d3c5cf
diff --git a/Cargo.lock b/Cargo.lock
index a2b1d226d..57302167d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6626,7 +6626,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"anyhow",
"bytemuck",
@@ -6679,7 +6679,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -6730,7 +6730,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"anyhow",
"chrono",
@@ -6754,7 +6754,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"anyhow",
"chrono",
@@ -6772,7 +6772,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"anyhow",
"image",
@@ -6787,7 +6787,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"anyhow",
"async-trait",
@@ -6856,7 +6856,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.85"
+version = "0.3.93"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/Cargo.toml b/Cargo.toml
index 795a5a0d5..c258d7912 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,7 +6,7 @@ exclude = ["apps/screenpipe-app-tauri/src-tauri"]
resolver = "2"
[workspace.package]
-version = "0.3.93"
+version = "0.3.94"
authors = ["louis030195 <[email protected]>"]
description = ""
repository = "https://github.com/screenpipe/screenpipe"
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index b7b1a24da..33122aaf1 100755
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -9007,7 +9007,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.0.298"
+version = "2.0.299"
dependencies = [
"anyhow",
"async-stream",
@@ -9089,7 +9089,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"anyhow",
"bytemuck",
@@ -9137,7 +9137,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -9188,7 +9188,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"anyhow",
"chrono",
@@ -9212,7 +9212,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"anyhow",
"chrono",
@@ -9228,7 +9228,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"anyhow",
"image 0.25.9",
@@ -9243,7 +9243,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"anyhow",
"async-trait",
@@ -9307,7 +9307,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.92"
+version = "0.3.93"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index 4f47963a3..7380ea37e 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.299"
+version = "2.0.300"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
index 97bc906f1..a5725af9d 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
@@ -160,11 +160,27 @@ pub async fn spawn_screenpipe(
}
// No handle — either cold start, or stop_screenpipe() already cleared it.
- // Kill whatever is on the port. We never "reuse" a handleless server because
- // it may be a dying process from stop_screenpipe() that hasn't released the
- // port yet. Always start fresh.
+ // Kill orphaned processes (but never our own PID).
kill_process_on_port(port).await;
+ // Wait for port to be fully released (our own server threads may still be
+ // shutting down after handle.shutdown() or stop_screenpipe()).
+ for i in 0..10 {
+ match tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await {
+ Ok(_) => {
+ debug!("Port {} is free after {}ms", port, i * 200);
+ break;
+ }
+ Err(_) => {
+ if i == 9 {
+ warn!("Port {} still in use after 2s, proceeding anyway", port);
+ } else {
+ tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
+ }
+ }
+ }
+ }
+
// Check permissions before starting
let permissions_check = do_permissions_check(false);
let store = SettingsStore::get(&app).ok().flatten().unwrap_or_default();
@@ -269,6 +285,8 @@ pub async fn spawn_screenpipe(
/// from previous crashes, CLI runs, etc. Safe because we already verified the port
/// is NOT serving a healthy screenpipe (that case returns early above).
async fn kill_process_on_port(port: u16) {
+ let my_pid = std::process::id().to_string();
+
#[cfg(unix)]
{
// lsof -ti:PORT gives PIDs of processes using that port
@@ -279,13 +297,18 @@ async fn kill_process_on_port(port: u16) {
{
Ok(output) if output.status.success() => {
let pids = String::from_utf8_lossy(&output.stdout);
- let pids: Vec<&str> = pids.trim().split('\n').filter(|s| !s.is_empty()).collect();
+ let pids: Vec<&str> = pids
+ .trim()
+ .split('\n')
+ .filter(|s| !s.is_empty() && *s != my_pid)
+ .collect();
if pids.is_empty() {
+ debug!("No orphaned processes on port {} (only our own PID)", port);
return;
}
warn!(
- "Found {} process(es) on port {}: {:?}. Killing to free port.",
- pids.len(), port, pids
+ "Found {} orphaned process(es) on port {}: {:?}. Killing to free port (our pid: {}).",
+ pids.len(), port, pids, my_pid
);
for pid in &pids {
let _ = tokio::process::Command::new("kill")
@@ -305,6 +328,7 @@ async fn kill_process_on_port(port: u16) {
#[cfg(windows)]
{
+ let my_pid_num: u32 = std::process::id();
// netstat -ano | findstr :PORT
match tokio::process::Command::new("cmd")
.args(["/C", &format!("netstat -ano | findstr :{}", port)])
@@ -318,18 +342,19 @@ async fn kill_process_on_port(port: u16) {
// Lines look like: TCP 0.0.0.0:3030 ... LISTENING 12345
if let Some(pid) = line.split_whitespace().last() {
if let Ok(pid_num) = pid.parse::<u32>() {
- if pid_num > 0 {
+ if pid_num > 0 && pid_num != my_pid_num {
pids.insert(pid_num);
}
}
}
}
if pids.is_empty() {
+ debug!("No orphaned processes on port {} (only our own PID)", port);
return;
}
warn!(
- "Found {} process(es) on port {}: {:?}. Killing to free port.",
- pids.len(), port, pids
+ "Found {} orphaned process(es) on port {}: {:?}. Killing to free port (our pid: {}).",
+ pids.len(), port, pids, my_pid_num
);
for pid in &pids {
let _ = tokio::process::Command::new("taskkill")