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: add 5s timeout to lsof calls to prevent zombie CPU drain

Louis Beaumont5a9314880a64
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
index 9b366c2bd..e5ff9839d 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
@@ -386,42 +386,65 @@ async fn kill_process_on_port(port: u16) {
#[cfg(unix)]
{
- // lsof -ti:PORT gives PIDs of processes using that port
- match tokio::process::Command::new("lsof")
+ // lsof -ti:PORT gives PIDs of processes using that port.
+ // lsof can hang indefinitely on macOS — always enforce a timeout
+ // and kill the child if it exceeds it, to avoid zombie lsof processes.
+ let child = match tokio::process::Command::new("lsof")
.args(["-ti", &format!(":{}", port)])
- .output()
- .await
+ .stdout(std::process::Stdio::piped())
+ .stderr(std::process::Stdio::null())
+ .spawn()
{
- 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() && *s != my_pid)
- .collect();
- if pids.is_empty() {
- debug!("No orphaned processes on port {} (only our own PID)", port);
- return;
- }
- warn!(
- "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")
- .args(["-9", pid])
- .output()
- .await;
+ Ok(c) => c,
+ Err(_) => return,
+ };
+
+ let child_id = child.id();
+ let output = match tokio::time::timeout(
+ std::time::Duration::from_secs(5),
+ child.wait_with_output(),
+ )
+ .await
+ {
+ Ok(Ok(o)) => o,
+ _ => {
+ // Kill the hung lsof process by pid
+ if let Some(pid) = child_id {
+ let _ = std::process::Command::new("kill")
+ .args(["-9", &pid.to_string()])
+ .output();
}
- // Wait for port to be released by the kernel after force-kill.
- // 500ms was too short and caused race conditions on fresh installs
- // where the new server tried to bind before FDs were fully cleaned up.
- tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
- info!("Killed orphaned process(es) on port {}", port);
+ warn!("lsof timed out checking port {}, killed", port);
+ return;
}
- _ => {
- // No process on port or lsof not available — fine
+ };
+
+ if output.status.success() {
+ let pids_str = String::from_utf8_lossy(&output.stdout);
+ let pids: Vec<&str> = pids_str
+ .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 {} 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")
+ .args(["-9", pid])
+ .output()
+ .await;
}
+ // Wait for port to be released by the kernel after force-kill.
+ // 500ms was too short and caused race conditions on fresh installs
+ // where the new server tried to bind before FDs were fully cleaned up.
+ tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
+ info!("Killed orphaned process(es) on port {}", port);
}
}
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/server.rs b/apps/screenpipe-app-tauri/src-tauri/src/server.rs
index e36a2e874..96189899b 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/server.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/server.rs
@@ -170,34 +170,59 @@ async fn kill_process_on_port(port: u16) {
#[cfg(unix)]
{
- match tokio::process::Command::new("lsof")
+ // lsof can hang indefinitely on macOS — always enforce a timeout
+ // and kill the child if it exceeds it, to avoid zombie lsof processes.
+ let child = match tokio::process::Command::new("lsof")
.args(["-ti", &format!(":{}", port)])
- .output()
- .await
+ .stdout(std::process::Stdio::piped())
+ .stderr(std::process::Stdio::null())
+ .spawn()
{
- 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() && *s != my_pid)
- .collect();
- if pids.is_empty() {
- return;
- }
- tracing::warn!(
- "found {} orphaned process(es) on port {}: {:?}, killing (our pid: {})",
- pids.len(), port, pids, my_pid
- );
- for pid in &pids {
- let _ = tokio::process::Command::new("kill")
- .args(["-9", pid])
- .output()
- .await;
+ Ok(c) => c,
+ Err(_) => return,
+ };
+
+ let child_id = child.id();
+ let output = match tokio::time::timeout(
+ std::time::Duration::from_secs(5),
+ child.wait_with_output(),
+ )
+ .await
+ {
+ Ok(Ok(o)) => o,
+ _ => {
+ // Kill the hung lsof process by pid
+ if let Some(pid) = child_id {
+ let _ = std::process::Command::new("kill")
+ .args(["-9", &pid.to_string()])
+ .output();
}
- tokio::time::sleep(std::time::Duration::from_millis(500)).await;
+ tracing::warn!("lsof timed out checking port {}, killed", port);
+ return;
+ }
+ };
+
+ if output.status.success() {
+ let pids_str = String::from_utf8_lossy(&output.stdout);
+ let pids: Vec<&str> = pids_str
+ .trim()
+ .split('\n')
+ .filter(|s| !s.is_empty() && *s != my_pid)
+ .collect();
+ if pids.is_empty() {
+ return;
+ }
+ tracing::warn!(
+ "found {} orphaned process(es) on port {}: {:?}, killing (our pid: {})",
+ pids.len(), port, pids, my_pid
+ );
+ for pid in &pids {
+ let _ = tokio::process::Command::new("kill")
+ .args(["-9", pid])
+ .output()
+ .await;
}
- _ => {}
+ tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
}