Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(mcp): use up a held pane reservation when startPaneSession throws
Jack Rudenko8213dd29edb7
diff --git a/packages/cli/src/pane/pane-registry.test.ts b/packages/cli/src/pane/pane-registry.test.tsindex 0dd4257b..c0f4c77b 100644--- a/packages/cli/src/pane/pane-registry.test.ts+++ b/packages/cli/src/pane/pane-registry.test.ts@@ -21,6 +21,7 @@ import {reapAllPanes,releasePaneReservations,reservePanes,+ reservedPaneCount,sweepOrphanPanes,writeRecord,} from "./pane-registry.js";@@ -486,3 +487,34 @@ describe("records", () => {).toBe(rec.paneId);});});++describe("reservations", () => {+ test("a start that throws before it took its reservation still uses up the one held for it", async () => {+ const t = env();+ const before = reservedPaneCount();+ reservePanes(2, t.sockRoot);+ expect(reservedPaneCount()).toBe(before + 2);+ // A claudish subcommand name as the model is refused before anything is spawned.+ for (const label of ["01", "02"]) {+ const err = await startPaneSession({+ kind: "t",+ label,+ callerFlags: [],+ spawnModel: "update",+ cwd: t.cwd,+ sessionUuid: crypto.randomUUID(),+ transcriptPath: t.transcriptPathFor("x"),+ slotEnv: {},+ shape: "one-shot",+ initialPrompt: "x",+ readAvailable: true,+ parentEnv: t.env,+ sockRoot: t.sockRoot,+ decide: () => ({ state: "COMPLETED" }),+ onBlocked: () => "wait",+ }).catch((e: unknown) => e);+ expect((err as { code?: string }).code).toBe("invalid_args");+ }+ expect(reservedPaneCount()).toBe(before);+ });+});diff --git a/packages/cli/src/pane/pane-session.ts b/packages/cli/src/pane/pane-session.tsindex cdadfaea..4fb7f403 100644--- a/packages/cli/src/pane/pane-session.ts+++ b/packages/cli/src/pane/pane-session.ts@@ -1583,21 +1583,32 @@ function preparePane(* `PaneStartError` BEFORE anything is spawned; installs the shutdown hooks (idempotent).*/export async function startPaneSession(o: PaneSessionOptions): Promise<PaneSession> {- if (SUBCOMMAND_WORDS.includes(o.spawnModel))- throw new PaneStartError(- "invalid_args",- `model "${o.spawnModel}" is a claudish subcommand name`- );const parentEnv = o.parentEnv ?? process.env;- const magmux = await assertMagmuxAvailable(o.magmuxBinary);- const root = ensureSockRoot(o.sockRoot ?? sockRootFor(parentEnv));- await ensureSwept(root);- if (!paneShutdownHooksInstalled()) installPaneShutdownHooks();- // a reservation the owner made for this run, else our own (which checks the limit); the- // record is written before the next await, so the pane is never uncounted- if (!takePaneReservation()) {- reservePanes(1, root);- takePaneReservation();+ let took = false;+ let magmux: { binary: string };+ let root: string;+ try {+ if (SUBCOMMAND_WORDS.includes(o.spawnModel))+ throw new PaneStartError(+ "invalid_args",+ `model "${o.spawnModel}" is a claudish subcommand name`+ );+ magmux = await assertMagmuxAvailable(o.magmuxBinary);+ root = ensureSockRoot(o.sockRoot ?? sockRootFor(parentEnv));+ await ensureSwept(root);+ if (!paneShutdownHooksInstalled()) installPaneShutdownHooks();+ // a reservation the owner made for this run, else our own (which checks the limit); the+ // record is written before the next await, so the pane is never uncounted+ took = takePaneReservation();+ if (!took) {+ reservePanes(1, root);+ took = takePaneReservation();+ }+ } catch (e) {+ // A start that fails before it took its reservation still uses up the one its owner+ // made for it, so an owner that reserved N panes never leaks the count.+ if (!took) takePaneReservation();+ throw e;}const p = preparePane(o, root, parentEnv);writeRecord(root, {