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(server): bind HTTP port before reporting success, prevent silent AddrInUse

Louis Beaumont0378cab1c7cd
diff --git a/Cargo.lock b/Cargo.lock
index 57302167d..5894278cd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6626,7 +6626,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"anyhow",
"bytemuck",
@@ -6679,7 +6679,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -6730,7 +6730,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"anyhow",
"chrono",
@@ -6754,7 +6754,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"anyhow",
"chrono",
@@ -6772,7 +6772,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"anyhow",
"image",
@@ -6787,7 +6787,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"anyhow",
"async-trait",
@@ -6856,7 +6856,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.93"
+version = "0.3.94"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/Cargo.toml b/Cargo.toml
index c258d7912..f3b685e54 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.94"
+version = "0.3.95"
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 33122aaf1..dee4bf978 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.299"
+version = "2.0.301"
dependencies = [
"anyhow",
"async-stream",
@@ -9089,7 +9089,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.93"
+version = "0.3.95"
dependencies = [
"anyhow",
"bytemuck",
@@ -9137,7 +9137,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.93"
+version = "0.3.95"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -9188,7 +9188,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.93"
+version = "0.3.95"
dependencies = [
"anyhow",
"chrono",
@@ -9212,7 +9212,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.93"
+version = "0.3.95"
dependencies = [
"anyhow",
"chrono",
@@ -9228,7 +9228,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.93"
+version = "0.3.95"
dependencies = [
"anyhow",
"image 0.25.9",
@@ -9243,7 +9243,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.93"
+version = "0.3.95"
dependencies = [
"anyhow",
"async-trait",
@@ -9307,7 +9307,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.93"
+version = "0.3.95"
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 7380ea37e..f39fca19b 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.300"
+version = "2.0.301"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
index fc160503d..b0a7c80ff 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
@@ -535,9 +535,21 @@ pub async fn start_embedded_server(
config.video_quality.clone(),
);
- // Start server in background
+ // Bind the HTTP listener BEFORE returning success.
+ // This ensures port conflicts (AddrInUse) are caught and propagated
+ // instead of silently failing in a background task.
+ let listener = tokio::net::TcpListener::bind(
+ SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), config.port),
+ )
+ .await
+ .map_err(|e| format!("Failed to bind port {}: {}", config.port, e))?;
+
+ info!("HTTP server bound to port {}", config.port);
+
+ // Start serving in background with the pre-bound listener
+ let enable_frame_cache = config.enable_frame_cache;
tokio::spawn(async move {
- if let Err(e) = server.start(config.enable_frame_cache).await {
+ if let Err(e) = server.start_with_listener(listener, enable_frame_cache).await {
error!("Server error: {:?}", e);
}
});
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
index a5725af9d..1c53ec4e1 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/recording.rs
@@ -165,17 +165,19 @@ pub async fn spawn_screenpipe(
// 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 {
+ // The embedded server will fail to bind if the port is still occupied,
+ // so this is critical for restart reliability.
+ for i in 0..20 {
match tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await {
Ok(_) => {
- debug!("Port {} is free after {}ms", port, i * 200);
+ debug!("Port {} is free after {}ms", port, i * 250);
break;
}
Err(_) => {
- if i == 9 {
- warn!("Port {} still in use after 2s, proceeding anyway", port);
+ if i == 19 {
+ warn!("Port {} still in use after 5s, will attempt start anyway", port);
} else {
- tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
+ tokio::time::sleep(tokio::time::Duration::from_millis(250)).await;
}
}
}
diff --git a/crates/screenpipe-server/src/server.rs b/crates/screenpipe-server/src/server.rs
index a16cf4d5d..76df61dc4 100644
--- a/crates/screenpipe-server/src/server.rs
+++ b/crates/screenpipe-server/src/server.rs
@@ -1382,6 +1382,26 @@ impl SCServer {
Ok(())
}
+ /// Start the server with a pre-bound TcpListener.
+ /// Use this when the caller needs to confirm the port is bound before proceeding.
+ pub async fn start_with_listener(
+ self,
+ listener: TcpListener,
+ enable_frame_cache: bool,
+ ) -> Result<(), std::io::Error> {
+ let app = self.create_router(enable_frame_cache).await;
+ info!("Server listening on {}", self.addr);
+
+ serve(
+ listener,
+ app.into_make_service_with_connect_info::<SocketAddr>(),
+ )
+ .await
+ .map_err(std::io::Error::other)?;
+
+ Ok(())
+ }
+
pub async fn create_router(&self, enable_frame_cache: bool) -> Router {
let api_request_count = Arc::new(AtomicUsize::new(0));