A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.
TypeScript
fix(worker): release browser slot when storage persistence fails (#115)
Kevin Rajan3281bc029667
diff --git a/apps/worker/src/browser.ts b/apps/worker/src/browser.tsindex da78783..59f48cf 100644--- a/apps/worker/src/browser.ts+++ b/apps/worker/src/browser.ts@@ -139,8 +139,13 @@ export async function createBrowserManager(options: {const stored = sessions.get(id);if (!stored) throw new WorkerError("SESSION_NOT_FOUND", "Browser session not found.", 404);if (instance) {- await instance.context.storageState({ path: join(directory(id), "storage.json") });- await instance.context.close();+ try {+ await instance.context.storageState({ path: join(directory(id), "storage.json") });+ } catch {+ // Cookie persistence is best-effort during teardown. A crashed or+ // unreachable browser must still release its context and session slot.+ }+ await instance.context.close().catch(() => {});await Promise.allSettled(instance.pending);running.delete(id);}diff --git a/apps/worker/tests/close-resilience.test.ts b/apps/worker/tests/close-resilience.test.tsnew file mode 100644index 0000000..d859ce4--- /dev/null+++ b/apps/worker/tests/close-resilience.test.ts@@ -0,0 +1,67 @@+import assert from "node:assert/strict";+import { randomUUID } from "node:crypto";+import { mkdir, mkdtemp, rm } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import test from "node:test";+import { createBrowserManager } from "../src/browser.ts";++async function candidateUrls(): Promise<string[]> {+ const urls: string[] = [];+ for (const hostname of ["example.com", "example.org"]) {+ try {+ const response = await fetch(`https://1.1.1.1/dns-query?name=${hostname}&type=A`, {+ headers: { accept: "application/dns-json" },+ });+ const body = (await response.json()) as {+ Answer?: { data?: string }[];+ };+ for (const answer of body.Answer ?? []) {+ if (typeof answer.data === "string") urls.push(`http://${answer.data}/`);+ }+ } catch {+ // Other candidates may still resolve.+ }+ }+ return [...new Set(urls)];+}++test("closeSession releases the session slot when cookie persistence fails", {+ timeout: 120_000,+}, async () => {+ const dataDir = await mkdtemp(join(tmpdir(), "openmuse-browser-close-"));+ const browser = await createBrowserManager({ dataDir, maxSessions: 1 });+ const urls = await candidateUrls();+ const firstUrl = urls[0];+ assert.ok(firstUrl, "no candidate navigation URLs resolved");+ const id = randomUUID();+ const next = randomUUID();+ let url = firstUrl;+ let created = false;+ for (const candidate of urls) {+ try {+ await browser.create(id, candidate);+ url = candidate;+ created = true;+ break;+ } catch (error) {+ if ((error as { code?: string })?.code !== "NAVIGATION_FAILED") throw error;+ }+ }+ assert.ok(created, "could not navigate to any candidate URL");+ try {+ await mkdir(join(dataDir, id, "storage.json"), { recursive: true });+ await browser.closeSession(id);+ assert.equal(+ browser.list().find((session) => session.id === id)?.status,+ "closed",+ "the session must read back as closed",+ );+ const reopened = await browser.create(next, url);+ assert.equal(reopened.status, "active");+ await browser.closeSession(next);+ } finally {+ await browser.close();+ await rm(dataDir, { recursive: true, force: true });+ }+});