From 94c8ec99454f145415c4fd8f0cc55640b94f2105 Mon Sep 17 00:00:00 2001 From: Nathan Herald Date: Wed, 22 Jul 2026 17:49:46 +0200 Subject: [PATCH] perf(sessions): skip the list socket probe when the pid already proves life MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `listSessions` computed `socketReachable` unconditionally, so `pty list` did a wasted per-session socket connect for every live-pid entry — the probe result is ignored (running either way) and it adds exactly the socket-connect contention that gets flaky under concurrent multi-agent list load. Probe the control socket ONLY when the pid doesn't already prove life (dead-looking / unreadable), where it is the deciding signal (the #117 reachable-socket rescue). Behavior-preserving: full suite green (1417 passed), typecheck clean. Mirrors the pty-rust port's approach (4fba12f). Genuine defense-in-depth for the hot list path under the concurrent multi-agent load #117 was about. --- src/sessions.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 76be24e..aaaec14 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -335,8 +335,12 @@ export async function listSessions(): Promise { const pid = readPid(name); const pidAlive = pid !== null && isProcessAlive(pid); // A reachable control socket proves the daemon is alive INDEPENDENTLY of - // whether we could read the pidfile this instant. - const socketReachable = await isSocketReachable(socketPath); + // whether we could read the pidfile this instant. Probe it ONLY when the + // pid doesn't already prove life: for a live pid the probe result is + // ignored (running either way), so skipping it avoids a per-session connect + // on the hot path and cuts the socket-connect contention that a `pty list` + // under concurrent multi-agent load would otherwise add. + const socketReachable = pidAlive ? true : await isSocketReachable(socketPath); if (pidAlive || socketReachable) { // Alive: a live process, or a reachable control socket (busy/mid-startup