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

feat: avoid double server running (#900)

ologbonowiwi5520a2dafbba
diff --git a/screenpipe-server/Cargo.toml b/screenpipe-server/Cargo.toml
index 48af8b9f2..b7ef30f12 100644
--- a/screenpipe-server/Cargo.toml
+++ b/screenpipe-server/Cargo.toml
@@ -123,6 +123,7 @@ fastrand = "2.1.1"
sqlite-vec = "0.1.3"
libsqlite3-sys = { version = "0.26", features = ["bundled"] }
zerocopy = { version = "0.7.32" }
+port_check = "0.2.1"
[dev-dependencies]
tempfile = "3.3.0"
diff --git a/screenpipe-server/src/bin/screenpipe-server.rs b/screenpipe-server/src/bin/screenpipe-server.rs
index b55bbf7fd..a6e56aaae 100644
--- a/screenpipe-server/src/bin/screenpipe-server.rs
+++ b/screenpipe-server/src/bin/screenpipe-server.rs
@@ -15,6 +15,7 @@ use colored::Colorize;
use crossbeam::queue::SegQueue;
use dirs::home_dir;
use futures::pin_mut;
+use port_check::is_local_ipv4_port_free;
use screenpipe_audio::{
default_input_device, default_output_device, list_audio_devices, parse_audio_device,
AudioDevice, DeviceControl,
@@ -133,6 +134,16 @@ async fn main() -> anyhow::Result<()> {
debug!("starting screenpipe server");
let cli = Cli::parse();
+ if !is_local_ipv4_port_free(cli.port) {
+ error!(
+ "you're likely already running screenpipe instance in a different environment, e.g. terminal/ide, close it and restart or use different port"
+ );
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::AddrInUse,
+ "Address in use",
+ ));
+ }
+
let local_data_dir = get_base_dir(&cli.data_dir)?;
let local_data_dir_clone = local_data_dir.clone();