From 36c1589d03cc3c725b3d1015d3980148d816b12b Mon Sep 17 00:00:00 2001 From: CodeFlow Lane Bot Date: Mon, 14 Sep 2026 16:45:14 +0530 Subject: [PATCH] fix(codeflow): Terminal Sessions Memory Leak --- packages/codeflow-store/src/shared/terminal-sessions.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/codeflow-store/src/shared/terminal-sessions.ts b/packages/codeflow-store/src/shared/terminal-sessions.ts index 5c2505b..206ab74 100644 --- a/packages/codeflow-store/src/shared/terminal-sessions.ts +++ b/packages/codeflow-store/src/shared/terminal-sessions.ts @@ -37,6 +37,7 @@ const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 30 min — sessions idle this long ar const EXPIRED_OUTPUT_GRACE_MS = 60 * 1000; // 60 s — after exit, retain snapshot for this long before deletion const PURGE_INTERVAL_MS = 30 * 1000; // 30 s — background sweep cadence const SIGKILL_TIMEOUT_MS = 5 * 1000; // 5 s — SIGKILL escalation if SIGTERM is ignored +const MAX_SESSIONS = 50; // hard cap on concurrent sessions to bound resource use const sessions = new Map(); let sessionCounter = 0; @@ -245,6 +246,14 @@ export const createTerminalSession = async (options?: { cwd?: string; title?: string; }): Promise => { + // Reap idle/expired sessions first so the cap reflects live sessions only. + purgeIdleSessions(); + if (sessions.size >= MAX_SESSIONS) { + throw new Error( + `Terminal session limit reached (${MAX_SESSIONS}). Close an existing session before opening a new one.` + ); + } + const cwd = await resolveInitialCwd(options?.cwd); const shell = getShellPath(); const child = spawn(shell, [], {