@devries-cameron20 / t3code-1 / commits / 350dcf4c04
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): preview tabs that fail during setup no longer leave their page open (#29885)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Alex Southwell committed Nov 3, 2026 at 23:42 UTC 350dcf4c042acf72200d1735bb6bd5e67d0ee982
2 files changed +32 -3
apps/server/src/preview/ServerBrowser.test.ts
+15
@@ -52,6 +52,12 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
52 if (contextFailure) throw contextFailure;
53 await contextGate?.promise;
54 const context = makeContext(this.onClose);
55 + const setupFailure = pageSetupFailure;
56 + if (setupFailure) {
57 + context.newCDPSession = async () => {
58 + throw setupFailure;
59 + };
60 + }
61 contexts.push(context);
62 return context as unknown as BrowserContext;
63 }
@@ -298,6 +304,7 @@ let pageCopy = "";
304 let keyCopy = "";
305 let focusedFrame: ReturnType<typeof makeFrame> | null = null;
306 let contextFailure: Error | null = null;
307 +let pageSetupFailure: Error | null = null;
308 let initialNavigation: (() => Promise<void>) | null = null;
309 let desktopRendersNext = false;
310 let desktopPageUrl = "about:blank";
@@ -442,6 +449,7 @@ beforeEach(() => {
449 contextRequests.length = 0;
450 contextGate = null;
451 contextFailure = null;
452 + pageSetupFailure = null;
453 initialNavigation = null;
454 pageCopy = "";
455 keyCopy = "";
@@ -2284,6 +2292,13 @@ it.live("failed session and browser launches release capacity, as does closing a
2292 // A failed launch must not leave a session a viewer can relaunch past the budget.
2293 expect((yield* manager.list({ threadId: scope.thread.threadId })).sessions).toHaveLength(7);
2294 contextFailure = null;
2295 + pageSetupFailure = new Error("Target page, context or browser has been closed");
2296 + expect(yield* open.pipe(Effect.result)).toMatchObject({ _tag: "Failure" });
2297 + pageSetupFailure = null;
2298 + // Setup that fails after the page exists must not leave the page or its context open.
2299 + expect(contexts.at(-1)!.close).toHaveBeenCalled();
2300 + expect(contexts.at(-1)!.page.isClosed()).toBe(true);
2301 + expect((yield* manager.list({ threadId: scope.thread.threadId })).sessions).toHaveLength(7);
2302 expect((yield* open).available).toBe(true);
2303 expect(yield* open.pipe(Effect.flip)).toMatchObject({ reason: "tabLimit" });
2304 expect(
apps/server/src/preview/ServerBrowser.ts
+17 -3
@@ -784,12 +784,27 @@ const make = Effect.gen(function* () {
784 };
785
786 const createTab = async (snapshot: PreviewSessionSnapshot): Promise<ServerTab> => {
787 + const opened: Array<() => Promise<void>> = [];
788 + try {
789 + return await setUpTab(snapshot, opened);
790 + } catch (cause) {
791 + // An unpublished tab is in no list, so nothing else would ever close it.
792 + for (const close of opened.reverse()) await close().catch(constVoid);
793 + throw cause;
794 + }
795 + };
796 +
797 + const setUpTab = async (
798 + snapshot: PreviewSessionSnapshot,
799 + opened: Array<() => Promise<void>>,
800 + ): Promise<ServerTab> => {
801 const adopted = adoptedPages.get(tabKey(snapshot.threadId, snapshot.tabId));
802 adoptedPages.delete(tabKey(snapshot.threadId, snapshot.tabId));
803 const desktop =
804 adopted === undefined && (await desktopRenders(snapshot))
805 ? await connectDesktop(snapshot)
806 : null;
807 + if (desktop) opened.push(desktop.close);
808 // Otherwise a desktop that attaches late shows its own page, and the agent drives another.
809 if (!desktop && desktopChannel.available) {
810 await runPromise(
@@ -810,9 +825,11 @@ const make = Effect.gen(function* () {
825 snapshot.profileId ?? "default",
826 isolatedContext ? tabKey(snapshot.threadId, snapshot.tabId) : undefined,
827 ));
828 + if (isolatedContext) opened.push(() => context.close());
829 if (adopted?.page.isClosed()) throw new Error("The popup closed before it opened.");
830 if (!desktop) await prepareContext(context);
831 const page = adopted?.page ?? desktop?.page ?? (await context.newPage());
832 + if (!desktop) opened.push(() => page.close());
833 const cdp = await context.newCDPSession(page);
834 await installPdfDocumentNavigation(cdp);
835 // The desktop's page is already a normal browser. A popup's first request
@@ -986,9 +1003,6 @@ const make = Effect.gen(function* () {
1003 const key = tabKey(tab.threadId, tab.tabId);
1004 if (closedPendingTabs.delete(key)) {
1005 await control.close().catch(constVoid);
989 - if (desktop) await desktop.close().catch(constVoid);
990 - else await page.close().catch(constVoid);
991 - if (isolatedContext) await context.close().catch(constVoid);
1006 throw new ServerBrowserTabNotFoundError({ threadId: tab.threadId, tabId: tab.tabId });
1007 }
1008 page.on("close", () => dropTab(tab, true));