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(pi-pool): cap at 20 sessions, never evict mid-stream, surface evictions to UI

Louis Beaumont9fd0c6d2f910
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index e1dcc6a7a..deb53c99e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -1011,8 +1011,15 @@ fn resolve_screenpipe_model(requested: &str, provider: &str) -> String {
base.to_string()
}
-/// Maximum number of concurrent Pi sessions before evicting old ones.
-const MAX_PI_SESSIONS: usize = 4;
+/// Soft cap on concurrent Pi sessions. Each session is its own bun + node
+/// subprocess holding ~150–300 MB RSS plus a live LLM connection, so we
+/// guard against accidental fork-bombs (a misbehaving caller spawning
+/// hundreds of sessions). Originally 4, raised to 20 on 2026-04-24 because
+/// 4 was too small for normal multi-tab chat use — opening a 5th tab would
+/// silently kill the least-recently-active session mid-stream, which was
+/// confusing UX. 20 leaves enough headroom that real users won't hit it
+/// while still preventing a runaway loop from melting the machine.
+const MAX_PI_SESSIONS: usize = 20;
/// Core Pi start logic — callable from both Tauri commands and Rust boot code.
pub async fn pi_start_inner(
@@ -1086,19 +1093,47 @@ pub async fn pi_start_inner(
// Only kill orphans when pool has no live sessions (app startup scenario)
kill_orphan_pi_processes(any_alive);
- // Evict least-recently-active non-"chat" session if at capacity
+ // Evict least-recently-active idle session if at capacity. Two safety
+ // properties beyond the prior LRU-only scheme:
+ // 1. Skip sessions with in-flight RPC responses — those are mid-turn
+ // (streaming a reply, running a tool). Killing them mid-stream is
+ // a worse UX than refusing to open a new session.
+ // 2. Emit `pi_session_evicted` so the UI can reflect the loss instead
+ // of the chat tab silently going dark. Frontend listens, marks the
+ // tab as closed and explains why.
+ // The "chat" key (legacy singleton chat session) and the requesting sid
+ // remain exempt — same as before.
if pool.sessions.len() >= MAX_PI_SESSIONS && !pool.sessions.contains_key(&sid) {
let evict_key = pool
.sessions
.iter()
- .filter(|(k, _)| k.as_str() != "chat" && k.as_str() != sid.as_str())
+ .filter(|(k, m)| {
+ k.as_str() != "chat"
+ && k.as_str() != sid.as_str()
+ && m.pending_responses
+ .lock()
+ .map(|r| r.is_empty())
+ .unwrap_or(true)
+ })
.min_by_key(|(_, m)| m.last_activity)
.map(|(k, _)| k.clone());
if let Some(key) = evict_key {
- info!("Evicting Pi session '{}' to make room for '{}'", key, sid);
+ info!("Evicting idle Pi session '{}' to make room for '{}'", key, sid);
if let Some(mut m) = pool.sessions.remove(&key) {
m.stop();
}
+ let _ = app.emit(
+ "pi_session_evicted",
+ serde_json::json!({ "session": key, "reason": "pool_full" }),
+ );
+ } else {
+ // Every session in the pool is busy. Refuse rather than kill a
+ // streaming session — caller surfaces a "too many active chats"
+ // toast, user can close one manually.
+ return Err(format!(
+ "pi pool full ({} active sessions, all busy) — close one before opening a new chat",
+ MAX_PI_SESSIONS
+ ));
}
}