Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(pane): keep pane files when a socket probe is inconclusive

Jack Rudenkodee6a098d98e
diff --git a/packages/cli/src/pane/pane-registry.test.ts b/packages/cli/src/pane/pane-registry.test.ts
index 21336726..5eb21605 100644
--- a/packages/cli/src/pane/pane-registry.test.ts
+++ b/packages/cli/src/pane/pane-registry.test.ts
@@ -13,12 +13,14 @@ import {
mkdirSync,
readFileSync,
readdirSync,
+ renameSync,
rmSync,
writeFileSync,
} from "node:fs";
import { type Server, createServer } from "node:net";
import { join } from "node:path";
import { waitForExit } from "../process-tree.js";
+import { MagmuxConnectError } from "./magmux-client.js";
import {
MIN_MAGMUX_VERSION,
OWNER_START36,
@@ -34,6 +36,7 @@ import {
livePaneCount,
ownerAlive,
ownerStartOfSelf,
+ probeSocket,
readRecord,
readRecords,
reapAllPanes,
@@ -42,6 +45,7 @@ import {
removePaneFiles,
reservePanes,
reservedPaneCount,
+ setSocketDialForTest,
sweepOrphanPanes,
updateRecord,
writeRecord,
@@ -95,6 +99,18 @@ function alive(pid: number): boolean {
}
}
+/**
+ * A socket inode nothing listens on, as a SIGKILLed magmux leaves it: a dial is refused
+ * with ECONNREFUSED. (A plain file there answers ENOTSOCK, which is not proof of a stale
+ * socket.)
+ */
+async function staleSocket(path: string): Promise<void> {
+ const server = createServer();
+ await new Promise<void>((resolve) => server.listen(`${path}.live`, resolve));
+ renameSync(`${path}.live`, path);
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+}
+
function deadPid(): number {
const p = Bun.spawnSync(["/usr/bin/true"]);
return p.pid;
@@ -400,7 +416,7 @@ describe.skipIf(!MAGMUX)(`pane registry, real magmux (${MAGMUX ? "" : NO_MAGMUX_
const t = env();
ensureSockRoot(t.sockRoot);
const stale = join(t.sockRoot, `magmux-c${deadPid()}-zz-tstale-abcdef.sock`);
- writeFileSync(stale, "");
+ await staleSocket(stale);
const id = `c${deadPid()}-zz-tlive-abcdef`;
const mag = spawn(
MAGMUX as string,
@@ -747,10 +763,14 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
/**
* A pane record as another owner's sweep or count would find it: owned by THIS live
* process, or by a pid that is gone (an owner SIGKILLed inside a sandbox, so its
- * record has no ownerStart and nothing removed it). The socket is a plain file: it
- * refuses a connection, as a dead magmux's does.
+ * record has no ownerStart and nothing removed it). The socket is stale (`staleSocket`),
+ * as a dead magmux leaves it.
*/
- function ownerPane(t: PaneTestEnv, label: string, owner: "live" | "dead"): PaneRecord {
+ async function ownerPane(
+ t: PaneTestEnv,
+ label: string,
+ owner: "live" | "dead"
+ ): Promise<PaneRecord> {
const root = ensureSockRoot(t.sockRoot);
const ownerPid = owner === "live" ? process.pid : deadPid();
const paneId = `c${ownerPid}-${OWNER_START36}-t${label}-abcdef`;
@@ -771,7 +791,7 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
turnDir: turn,
createdAt: new Date().toISOString(),
};
- writeFileSync(rec.sockPath, "");
+ await staleSocket(rec.sockPath);
writeRecord(root, rec);
return rec;
}
@@ -786,7 +806,7 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
test("a live owner: counted, and the sweep keeps its record, socket and dirs", async () => {
const t = env();
- const rec = ownerPane(t, "lv", "live");
+ const rec = await ownerPane(t, "lv", "live");
expect(await withProcessTableUnavailable(() => livePaneCount(t.sockRoot))).toBe(1);
const r = await withProcessTableUnavailable(() => sweepOrphanPanes(t.sockRoot));
expect(r).toEqual({ reaped: [], cleaned: [], kept: [] });
@@ -796,7 +816,7 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
test("a dead owner whose magmux is gone: not counted, and the sweep removes its files", async () => {
const t = env();
- const rec = ownerPane(t, "dd", "dead");
+ const rec = await ownerPane(t, "dd", "dead");
expect(await withProcessTableUnavailable(() => livePaneCount(t.sockRoot))).toBe(0);
const r = await withProcessTableUnavailable(() => sweepOrphanPanes(t.sockRoot));
expect(r.cleaned).toEqual([rec.paneId]);
@@ -806,7 +826,8 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
test("dead owners never fill the pane limit", async () => {
const t = env();
- for (let i = 0; i < MAX_LIVE_PANES; i++) ownerPane(t, i.toString(36).padStart(2, "0"), "dead");
+ for (let i = 0; i < MAX_LIVE_PANES; i++)
+ await ownerPane(t, i.toString(36).padStart(2, "0"), "dead");
// 48 records of dead owners: a start must still be admitted, not refused as pane_limit
await withProcessTableUnavailable(() => reservePanes(1, t.sockRoot));
releasePaneReservations(1);
@@ -814,7 +835,7 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
test("a dead owner whose magmux still answers: not counted, files kept, nothing signalled", async () => {
const t = env();
- const rec = ownerPane(t, "dm", "dead");
+ const rec = await ownerPane(t, "dm", "dead");
rmSync(rec.sockPath);
const server = await liveSocket(rec.sockPath);
try {
@@ -830,7 +851,7 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
test("a dead owner whose watcher pid may be alive: kept (the watcher may be mid-job)", async () => {
const t = env();
- const rec = ownerPane(t, "dw", "dead");
+ const rec = await ownerPane(t, "dw", "dead");
updateRecord(t.sockRoot, rec.paneId, { watcherPid: process.pid });
const r = await withProcessTableUnavailable(() => sweepOrphanPanes(t.sockRoot));
expect(r.kept).toEqual([rec.paneId]);
@@ -845,7 +866,7 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
const deadLive = join(root, `magmux-c${gone}-${OWNER_START36}-tdl-abcdef.sock`);
const deadRefused = join(root, `magmux-c${gone}-${OWNER_START36}-tdr-abcdef.sock`);
const servers = [await liveSocket(ownLive), await liveSocket(deadLive)];
- writeFileSync(deadRefused, "");
+ await staleSocket(deadRefused);
try {
const r = await withProcessTableUnavailable(() => sweepOrphanPanes(root));
expect(r.reaped).toEqual([]);
@@ -860,6 +881,118 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
for (const s of servers) s.close();
}
}, 20_000);
+
+ /** Run `fn` with every pane-socket dial failing with `code`. */
+ async function withDialError<T>(code: string, fn: () => Promise<T>): Promise<T> {
+ const prev = setSocketDialForTest(() =>
+ Promise.reject(new MagmuxConnectError(`connect ${code}`, code))
+ );
+ try {
+ return await fn();
+ } finally {
+ setSocketDialForTest(prev);
+ }
+ }
+
+ const INCONCLUSIVE = ["EPERM", "EACCES", "EMFILE", "ENFILE", "ENOTSOCK"];
+ const STALE = ["ECONNREFUSED", "ENOENT"];
+
+ test("a dead owner's record: an inconclusive probe keeps the files, a stale one removes them", async () => {
+ for (const code of [...INCONCLUSIVE, ...STALE]) {
+ const t = env();
+ const rec = await ownerPane(t, "pr", "dead");
+ const r = await withDialError(code, () =>
+ withProcessTableUnavailable(() => sweepOrphanPanes(t.sockRoot))
+ );
+ const removed = STALE.includes(code);
+ expect({ code, r }).toEqual({
+ code,
+ r: { reaped: [], cleaned: removed ? [rec.paneId] : [], kept: removed ? [] : [rec.paneId] },
+ });
+ for (const p of filesOf(rec)) expect(`${code} ${existsSync(p)}`).toBe(`${code} ${!removed}`);
+ }
+ }, 30_000);
+
+ test("a dead owner's recordless socket: an inconclusive probe keeps it, a stale one removes it", async () => {
+ for (const code of [...INCONCLUSIVE, ...STALE]) {
+ const t = env();
+ const root = ensureSockRoot(t.sockRoot);
+ const id = `c${deadPid()}-${OWNER_START36}-trp-abcdef`;
+ const sock = sockPathOf(root, id);
+ await staleSocket(sock);
+ const r = await withDialError(code, () =>
+ withProcessTableUnavailable(() => sweepOrphanPanes(root))
+ );
+ const removed = STALE.includes(code);
+ expect({ code, kept: r.kept, cleaned: r.cleaned }).toEqual({
+ code,
+ kept: removed ? [] : [id],
+ cleaned: removed ? [id] : [],
+ });
+ expect(`${code} ${existsSync(sock)}`).toBe(`${code} ${!removed}`);
+ }
+ }, 30_000);
+});
+
+describe("the pane-socket probe: only ECONNREFUSED and ENOENT prove a socket stale", () => {
+ test("real sockets: live → answers; refused or missing → stale; mode 000 or a plain file → inconclusive", async () => {
+ const t = env();
+ const root = ensureSockRoot(t.sockRoot);
+ const live = join(root, "magmux-live.sock");
+ const locked = join(root, "magmux-locked.sock");
+ const servers = [createServer((c) => c.end()), createServer((c) => c.end())];
+ await new Promise<void>((resolve) => servers[0]?.listen(live, resolve));
+ await new Promise<void>((resolve) => servers[1]?.listen(locked, resolve));
+ chmodSync(locked, 0o000);
+ const refused = join(root, "magmux-refused.sock");
+ await staleSocket(refused);
+ const plain = join(root, "magmux-plain.sock");
+ writeFileSync(plain, "");
+ try {
+ const answers = await probeSocket(live);
+ expect(answers.state).toBe("answers");
+ if (answers.state === "answers") answers.client.close();
+ expect(await probeSocket(refused)).toEqual({ state: "stale", code: "ECONNREFUSED" });
+ expect(await probeSocket(join(root, "magmux-none.sock"))).toEqual({
+ state: "stale",
+ code: "ENOENT",
+ });
+ expect(await probeSocket(locked)).toEqual({ state: "inconclusive", code: "EACCES" });
+ expect(await probeSocket(plain)).toEqual({ state: "inconclusive", code: "ENOTSOCK" });
+ } finally {
+ for (const s of servers) s.close();
+ }
+ }, 20_000);
+
+ test("a dial that never settles is inconclusive", async () => {
+ const prev = setSocketDialForTest(() => new Promise<never>(() => {}));
+ try {
+ expect(await probeSocket("/nonexistent/magmux-x.sock")).toEqual({
+ state: "inconclusive",
+ code: "ETIMEDOUT",
+ });
+ } finally {
+ setSocketDialForTest(prev);
+ }
+ }, 20_000);
+
+ test("with a readable table, a dead owner's recordless socket that refuses access (EACCES) is kept", async () => {
+ // outside any sandbox: the sweep keeps what it cannot probe instead of unlinking it
+ const t = env();
+ const root = ensureSockRoot(t.sockRoot);
+ const id = `c${deadPid()}-${OWNER_START36}-tea-abcdef`;
+ const sock = sockPathOf(root, id);
+ const server = createServer((c) => c.end());
+ await new Promise<void>((resolve) => server.listen(sock, resolve));
+ chmodSync(sock, 0o000);
+ try {
+ const r = await sweepOrphanPanes(root);
+ expect(r).toEqual({ reaped: [], cleaned: [], kept: [id] });
+ expect(existsSync(sock)).toBe(true);
+ } finally {
+ server.close();
+ }
+ }, 20_000);
});
describe("owner identity: one check for the pane limit, the record sweep and the socket sweep", () => {
diff --git a/packages/cli/src/pane/pane-registry.ts b/packages/cli/src/pane/pane-registry.ts
index 19304ad6..38bd91a9 100644
--- a/packages/cli/src/pane/pane-registry.ts
+++ b/packages/cli/src/pane/pane-registry.ts
@@ -514,30 +514,78 @@ async function sweepRecord(rec: PaneRecord, root: string, out: SweepResult): Pro
else out.cleaned.push(rec.paneId);
}
-/** A unix socket at `sockPath` accepts a connection: some magmux still serves it. */
-async function socketAnswers(sockPath: string): Promise<boolean> {
+/**
+ * What one connection attempt says about a pane socket:
+ * - `answers`: the connect succeeded; some magmux serves it (the caller owns `client`);
+ * - `stale`: ECONNREFUSED or ENOENT, nothing listens there;
+ * - `inconclusive`: any other error (EPERM, EACCES, EMFILE, ENFILE, ...) or no answer
+ * within `SOCKET_PROBE_TIMEOUT_MS`; says nothing about the listener, so files stay.
+ *
+ * Known darwin limit (magmux `sockdir.go`): a LIVE listener whose accept queue is full
+ * answers ECONNREFUSED. Every caller probes only after the owner is proven dead, so the
+ * remaining risk is a dead owner's still-running magmux with a full backlog read as stale.
+ */
+export type SocketProbe =
+ | { state: "answers"; client: MagmuxClient }
+ | { state: "stale"; code: string }
+ | { state: "inconclusive"; code: string };
+
+const STALE_SOCKET_CODES = new Set(["ECONNREFUSED", "ENOENT"]);
+const SOCKET_PROBE_TIMEOUT_MS = 2000;
+
+let dialSocket: (sockPath: string) => Promise<MagmuxClient> = (p) => MagmuxClient.dial(p);
+
+/** @internal tests: replace how the sweep dials a pane socket; returns the previous dial. */
+export function setSocketDialForTest(
+ next: (sockPath: string) => Promise<MagmuxClient>
+): (sockPath: string) => Promise<MagmuxClient> {
+ const prev = dialSocket;
+ dialSocket = next;
+ return prev;
+}
+
+/** THE pane-socket probe, for the record sweep and the recordless-socket sweep. */
+export async function probeSocket(sockPath: string): Promise<SocketProbe> {
+ const dial = dialSocket(sockPath);
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const timeout = new Promise<"timeout">((resolve) => {
+ timer = setTimeout(() => resolve("timeout"), SOCKET_PROBE_TIMEOUT_MS);
+ });
try {
- (await MagmuxClient.dial(sockPath)).close();
- return true;
- } catch {
- return false;
+ const won = await Promise.race([dial, timeout]);
+ if (won !== "timeout") return { state: "answers", client: won };
+ void dial.then(
+ (c) => c.close(),
+ () => undefined
+ );
+ return { state: "inconclusive", code: "ETIMEDOUT" };
+ } catch (e) {
+ const code = (e as { errno?: string | null; code?: string }).errno ?? "unknown";
+ return STALE_SOCKET_CODES.has(code)
+ ? { state: "stale", code }
+ : { state: "inconclusive", code };
+ } finally {
+ clearTimeout(timer);
}
}
/**
* A dead owner's record (kill(pid, 0) answered ESRCH) while the table is unavailable.
* No process can be identified, so nothing is signalled. A watcher pid that may be alive
- * may be mid-job, and a socket that still answers is a live magmux: both keep the files,
- * and the pane is left running for a sweep that can read the table. Otherwise the pane's
- * magmux is gone and its files are removed, as the dead-owner path does when nothing is
- * verified alive.
+ * may be mid-job, and a socket that answers (a live magmux) or whose probe is
+ * inconclusive may still be served: each keeps the files, and the pane is left running
+ * for a sweep that can read the table. Only a stale socket (`probeSocket`) means the
+ * pane's magmux is gone; then its files are removed, as the dead-owner path does when
+ * nothing is verified alive.
*/
async function sweepUnidentifiable(rec: PaneRecord, root: string, out: SweepResult) {
if (rec.watcherPid && pidMayBeAlive(rec.watcherPid)) {
out.kept.push(rec.paneId);
return;
}
- if (await socketAnswers(sockPathOf(root, rec.paneId))) {
+ const probe = await probeSocket(sockPathOf(root, rec.paneId));
+ if (probe.state !== "stale") {
+ if (probe.state === "answers") probe.client.close();
out.kept.push(rec.paneId);
return;
}
@@ -564,14 +612,17 @@ async function sweepRecordlessSocket(root: string, name: string, out: SweepResul
const owner = ownerOfSocketId(id);
if (!owner || ownerAlive(owner, table)) return; // no owner in the id: not ours to judge
const sockPath = join(root, name);
- let client: MagmuxClient | null = null;
- try {
- client = await MagmuxClient.dial(sockPath);
- } catch {
+ const probe = await probeSocket(sockPath);
+ if (probe.state === "stale") {
rmSync(sockPath, { force: true });
out.cleaned.push(id);
return;
}
+ if (probe.state === "inconclusive") {
+ out.kept.push(id); // the probe says nothing about the listener
+ return;
+ }
+ const client = probe.client;
if (!table) {
// a live magmux of a dead owner: without the table nothing identifies its processes
client.close();