Skip to content

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

fix(pane): check a dial's ENOENT against the socket path

Jack Rudenko63859b52f9ad
diff --git a/packages/cli/src/pane/pane-registry.test.ts b/packages/cli/src/pane/pane-registry.test.ts
index ce1ca42b..5896b702 100644
--- a/packages/cli/src/pane/pane-registry.test.ts
+++ b/packages/cli/src/pane/pane-registry.test.ts
@@ -10,10 +10,10 @@ import { type ChildProcess, execFileSync, spawn } from "node:child_process";
import {
chmodSync,
existsSync,
+ lstatSync,
mkdirSync,
readFileSync,
readdirSync,
- renameSync,
rmSync,
writeFileSync,
} from "node:fs";
@@ -101,15 +101,25 @@ 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.)
+ * A socket inode nothing listens on, made the way a magmux leaves one: a child binds it and
+ * is SIGKILLed, so no close() runs and the kernel keeps the file. A dial is then refused
+ * with ECONNREFUSED. Closing an in-process server instead is not portable: Bun 1.3.10 (the
+ * CI pin) removes the file, and the dial answers ENOENT. (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()));
+ const child = Bun.spawn(
+ [
+ process.execPath,
+ "-e",
+ `require("node:net").createServer().listen(${JSON.stringify(path)}, () => console.log("up"));`,
+ ],
+ { stdout: "pipe", stderr: "inherit" }
+ );
+ await child.stdout.getReader().read(); // "up": bound and listening
+ child.kill("SIGKILL");
+ await child.exited;
+ expect(lstatSync(path).isSocket()).toBe(true);
}
function deadPid(): number {
@@ -945,8 +955,9 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
}
}, 30_000);
- test("a dead owner's recordless socket without a table: only ENOENT removes it", async () => {
- // no table names its magmux, so ECONNREFUSED is inconclusive here
+ test("a dead owner's recordless socket without a table: kept, whatever the dial reports", async () => {
+ // no table names its magmux, so a refusal is inconclusive; an ENOENT while the socket
+ // is still on disk is a refusal too (old Bun reports ENOENT for every failed connect)
for (const code of [...INCONCLUSIVE, ...STALE]) {
const t = env();
const root = ensureSockRoot(t.sockRoot);
@@ -956,17 +967,53 @@ describe("with the process table unavailable (a sandbox refuses ps): owners judg
const r = await withDialError(code, () =>
withProcessTableUnavailable(() => sweepOrphanPanes(root))
);
- const removed = code === "ENOENT";
- expect({ code, kept: r.kept, cleaned: r.cleaned }).toEqual({
- code,
- kept: removed ? [] : [id],
- cleaned: removed ? [id] : [],
- });
- expect(`${code} ${existsSync(sock)}`).toBe(`${code} ${!removed}`);
+ expect({ code, kept: r.kept, cleaned: r.cleaned }).toEqual({ code, kept: [id], cleaned: [] });
+ expect(`${code} ${existsSync(sock)}`).toBe(`${code} true`);
}
}, 30_000);
});
+describe("a dial that reports ENOENT is checked against the path (Bun < 1.4 reports it for every failure)", () => {
+ // replays Bun 1.3.10 on any Bun: every failed unix connect answers ENOENT
+ async function probeWithEnoent(path: string, magmuxMayLive: boolean) {
+ const prev = setSocketDialForTest(() =>
+ Promise.reject(new MagmuxConnectError("connect ENOENT", "ENOENT"))
+ );
+ try {
+ return await probeSocket(path, magmuxMayLive);
+ } finally {
+ setSocketDialForTest(prev);
+ }
+ }
+
+ test("missing → stale; plain file → ENOTSOCK; mode 000 → EACCES; any other socket → a refusal", async () => {
+ const t = env();
+ const root = ensureSockRoot(t.sockRoot);
+ const refused = join(root, "magmux-refused.sock");
+ await staleSocket(refused);
+ const locked = join(root, "magmux-locked.sock");
+ await staleSocket(locked);
+ chmodSync(locked, 0o000);
+ const plain = join(root, "magmux-plain.sock");
+ writeFileSync(plain, "");
+ expect(await probeWithEnoent(join(root, "magmux-none.sock"), true)).toEqual({
+ state: "stale",
+ code: "ENOENT",
+ });
+ expect(await probeWithEnoent(plain, false)).toEqual({
+ state: "inconclusive",
+ code: "ENOTSOCK",
+ });
+ expect(await probeWithEnoent(locked, false)).toEqual({ state: "inconclusive", code: "EACCES" });
+ // a refusal is stale only with the pane's magmux proven dead
+ expect(await probeWithEnoent(refused, true)).toEqual({
+ state: "inconclusive",
+ code: "ECONNREFUSED",
+ });
+ expect(await probeWithEnoent(refused, false)).toEqual({ state: "stale", code: "ECONNREFUSED" });
+ }, 20_000);
+});
+
describe("the pane-socket probe: ENOENT, or ECONNREFUSED with magmux proven dead, is stale", () => {
test("real sockets: live → answers; refused or missing → stale; mode 000 or a plain file → inconclusive", async () => {
const t = env();
diff --git a/packages/cli/src/pane/pane-registry.ts b/packages/cli/src/pane/pane-registry.ts
index a93a546d..524f2f74 100644
--- a/packages/cli/src/pane/pane-registry.ts
+++ b/packages/cli/src/pane/pane-registry.ts
@@ -16,7 +16,10 @@
*/
import {
+ constants,
+ accessSync,
existsSync,
+ lstatSync,
readFileSync,
readdirSync,
renameSync,
@@ -563,7 +566,7 @@ export async function probeSocket(sockPath: string, magmuxMayLive: boolean): Pro
);
return { state: "inconclusive", code: "ETIMEDOUT" };
} catch (e) {
- const code = (e as { errno?: string | null; code?: string }).errno ?? "unknown";
+ const code = dialErrno((e as { errno?: string | null }).errno ?? "unknown", sockPath);
const stale = code === "ENOENT" || (code === "ECONNREFUSED" && !magmuxMayLive);
return stale ? { state: "stale", code } : { state: "inconclusive", code };
} finally {
@@ -571,6 +574,31 @@ export async function probeSocket(sockPath: string, magmuxMayLive: boolean): Pro
}
}
+/**
+ * A failed dial's errno, with ENOENT checked against the path itself. Bun before 1.4
+ * reports ENOENT for EVERY failed unix connect (measured, 1.3.10 on darwin: a refused
+ * socket, a mode-000 socket and a plain file all answer ENOENT), so ENOENT alone would
+ * read a live pane's socket as gone. The path decides instead: missing → ENOENT; not a
+ * socket → ENOTSOCK; not writable → EACCES; any other socket → ECONNREFUSED, which is
+ * stale only with pid evidence that its magmux is dead (`probeSocket`).
+ */
+function dialErrno(reported: string, sockPath: string): string {
+ if (reported !== "ENOENT") return reported;
+ let isSocket: boolean;
+ try {
+ isSocket = lstatSync(sockPath).isSocket();
+ } catch {
+ return "ENOENT";
+ }
+ if (!isSocket) return "ENOTSOCK";
+ try {
+ accessSync(sockPath, constants.W_OK);
+ } catch {
+ return "EACCES";
+ }
+ return "ECONNREFUSED";
+}
+
/**
* 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