Skip to content

Commit 2f3109e

Browse files
fix(server): a lookup during crash recovery waits for the replacement page
A tab lookup that ran while a crashed isolated tab's context was still closing could open the replacement page in that context, and the close then took the page and its session down. The recovery is now reserved as the tab's pending open before the dead page leaves, so lookups wait for it. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent b78eff7 commit 2f3109e

1 file changed

Lines changed: 34 additions & 18 deletions

File tree

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -591,29 +591,45 @@ const make = Effect.gen(function* () {
591591
// Playwright cannot reload a crashed page, so a fresh page takes over its tab.
592592
const reopenCrashedTab = async (tab: ServerTab) => {
593593
if (tab.desktop) return dropTab(tab, true);
594-
if (tabs.get(tabKey(tab.threadId, tab.tabId)) !== tab) return;
594+
const key = tabKey(tab.threadId, tab.tabId);
595+
if (tabs.get(key) !== tab) return;
595596
tab.crash = {
596597
url: ServerBrowserPage.withoutDataPayload(tab.page.url()).slice(0, 2048),
597598
code: -2,
598599
description: "Page crashed",
599600
};
600-
// The session must name the crash before a new page reads it, or it would load the URL again.
601-
await runPromise(
602-
manager
603-
.reportStatus({
604-
threadId: tab.threadId,
605-
tabId: tab.tabId,
606-
serverControlled: true,
607-
navStatus: { _tag: "LoadFailed", title: "", ...tab.crash },
608-
canGoBack: false,
609-
canGoForward: false,
610-
})
611-
.pipe(Effect.ignore),
612-
);
613-
dropTab(tab, false, true);
614-
// A context that is still closing would take the new page down with it.
615-
if (tab.isolatedContext) await tab.page.context().close().catch(constVoid);
616-
await runPromise(findTab(tab.threadId, tab.tabId).pipe(Effect.ignore));
601+
const reopening = (async () => {
602+
// The session must name the crash before a new page reads it, or it would load the URL again.
603+
await runPromise(
604+
manager
605+
.reportStatus({
606+
threadId: tab.threadId,
607+
tabId: tab.tabId,
608+
serverControlled: true,
609+
navStatus: { _tag: "LoadFailed", title: "", ...tab.crash },
610+
canGoBack: false,
611+
canGoForward: false,
612+
})
613+
.pipe(Effect.ignore),
614+
);
615+
dropTab(tab, false, true);
616+
// A context that is still closing would take the new page down with it.
617+
if (tab.isolatedContext) await tab.page.context().close().catch(constVoid);
618+
const { sessions } = await runPromise(manager.list({ threadId: tab.threadId }));
619+
const snapshot = sessions.find(
620+
(session) => session.tabId === tab.tabId && session.runtime === "server",
621+
);
622+
if (!snapshot) {
623+
throw new ServerBrowserTabNotFoundError({ threadId: tab.threadId, tabId: tab.tabId });
624+
}
625+
return createTab(snapshot);
626+
})().finally(() => {
627+
pendingTabs.delete(key);
628+
closedPendingTabs.delete(key);
629+
});
630+
// Reserved before the dead page leaves, so a lookup in between waits for its replacement.
631+
pendingTabs.set(key, { agentId: tab.control.agentId, promise: reopening });
632+
await reopening.catch(constVoid);
617633
};
618634

619635
const dialogStatus = (tab: ServerTab) =>

0 commit comments

Comments
 (0)