Skip to content

Commit c230934

Browse files
fix(desktop): native Browser controls leave a page whose script never yields
Visible server tabs in the desktop's primary environment are driven by the desktop's own navigation calls, not the server's viewer handlers, so the address bar, Back, Forward, Reload and hard reload still stayed on a hung page there. The desktop preview manager now sends the same bounded probe through the tab's debugger session before each of those navigations and terminates a script that does not answer within one second. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 04dc1c7 commit c230934

2 files changed

Lines changed: 106 additions & 4 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,74 @@ describe("PreviewManager", () => {
745745
),
746746
);
747747

748+
effectIt.effect.each([
749+
"navigate",
750+
"reload",
751+
"goBack",
752+
"goForward",
753+
"refresh",
754+
"hardReload",
755+
] as const)("%s stops a page script that never yields before it navigates", (operation) =>
756+
withManager((manager) =>
757+
Effect.gen(function* () {
758+
const preview = makeFaviconWebContents();
759+
// As in Chromium: the hung renderer answers nothing, and a terminate reaches it only
760+
// while no navigation is pending.
761+
let stopped = false;
762+
const probed = yield* Deferred.make<void>();
763+
const navigatedAfterStop: Array<boolean> = [];
764+
const navigated = () => {
765+
navigatedAfterStop.push(stopped);
766+
};
767+
const sendCommand = async (method: string) => {
768+
if (method === "Runtime.evaluate") {
769+
Deferred.doneUnsafe(probed, Effect.void);
770+
await new Promise(() => {});
771+
}
772+
if (method === "Runtime.terminateExecution") stopped = true;
773+
};
774+
Object.assign(preview.webContents, {
775+
getURL: () => "http://localhost:5173/busy",
776+
reload: navigated,
777+
reloadIgnoringCache: navigated,
778+
loadURL: async () => navigated(),
779+
navigationHistory: {
780+
canGoBack: () => true,
781+
canGoForward: () => true,
782+
goBack: navigated,
783+
goForward: navigated,
784+
},
785+
debugger: {
786+
isAttached: () => false,
787+
attach: vi.fn(),
788+
sendCommand,
789+
on: vi.fn(),
790+
off: vi.fn(),
791+
},
792+
});
793+
fromId.mockReturnValue(preview.webContents);
794+
yield* manager.createTab("tab_busy");
795+
yield* manager.registerWebview("tab_busy", 42);
796+
797+
const leaving = yield* (
798+
operation === "navigate"
799+
? manager.navigate("tab_busy", "http://localhost:5173/next")
800+
: operation === "reload"
801+
? manager.navigate("tab_busy", "http://localhost:5173/busy")
802+
: manager[operation]("tab_busy")
803+
).pipe(Effect.forkChild);
804+
const probing = yield* Effect.raceFirst(
805+
Deferred.await(probed).pipe(Effect.as(true)),
806+
Fiber.await(leaving).pipe(Effect.as(false)),
807+
);
808+
if (probing) yield* TestClock.adjust(1_000);
809+
yield* Fiber.join(leaving);
810+
811+
expect(navigatedAfterStop).toEqual([true]);
812+
}),
813+
),
814+
);
815+
748816
effectIt.effect("keeps mouse back and forward buttons inside the preview tab", () =>
749817
withManager((manager) =>
750818
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ const PICTURE_IN_PICTURE_JPEG_QUALITY = 80;
136136
const CAPTURE_PAGE_RETRY_ATTEMPTS = 3;
137137
const CAPTURE_PAGE_RETRY_DELAY_MS = 120;
138138
const CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS = 1_000;
139+
const BUSY_PAGE_PROBE_MS = 1_000;
139140
const PICTURE_IN_PICTURE_INITIAL_WIDTH = 480;
140141
const PICTURE_IN_PICTURE_INITIAL_HEIGHT = 320;
141142
const PICTURE_IN_PICTURE_MIN_WIDTH = 240;
@@ -2034,6 +2035,28 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20342035
);
20352036
});
20362037

2038+
/**
2039+
* A same-site navigation reuses the renderer, so it never commits while a page script holds it.
2040+
* Runs before the navigation starts: Chromium does not deliver a terminate once one is pending.
2041+
*/
2042+
const leaveBusyPage = Effect.fn("PreviewManager.leaveBusyPage")(function* (
2043+
wc: Electron.WebContents,
2044+
) {
2045+
const control = (yield* SynchronizedRef.get(controlSessionsRef)).get(wc.id);
2046+
if (!control) return;
2047+
const answered = yield* Effect.promise(() =>
2048+
control.debugger.sendCommand("Runtime.evaluate", { expression: "0" }).then(
2049+
() => undefined,
2050+
() => undefined,
2051+
),
2052+
).pipe(Effect.timeoutOption(BUSY_PAGE_PROBE_MS));
2053+
if (Option.isSome(answered)) return;
2054+
// Not awaited: a renderer blocked on something other than script may never acknowledge it.
2055+
yield* Effect.sync(() => {
2056+
void control.debugger.sendCommand("Runtime.terminateExecution").catch(() => undefined);
2057+
});
2058+
});
2059+
20372060
const navigate = Effect.fn("PreviewManager.navigate")(function* (tabId: string, rawUrl: string) {
20382061
const url = yield* attempt({ operation: "navigate.normalizeUrl", tabId }, () =>
20392062
normalizePreviewUrl(rawUrl),
@@ -2116,6 +2139,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21162139
);
21172140
return;
21182141
}
2142+
yield* leaveBusyPage(wc);
21192143
if (wc.getURL() === url) {
21202144
yield* attempt({ operation: "navigate.reload", tabId, webContentsId: wc.id }, () =>
21212145
wc.reload(),
@@ -2151,18 +2175,28 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21512175
yield* attempt({ operation, tabId, webContentsId: wc.id }, () => use(wc));
21522176
});
21532177

2178+
const leavePage = Effect.fn("PreviewManager.leavePage")(function* (
2179+
operation: string,
2180+
tabId: string,
2181+
use: (wc: Electron.WebContents) => void,
2182+
) {
2183+
const wc = yield* requireWebContents(tabId);
2184+
yield* leaveBusyPage(wc);
2185+
yield* attempt({ operation, tabId, webContentsId: wc.id }, () => use(wc));
2186+
});
2187+
21542188
const goBack = (tabId: string) =>
2155-
withWebContents("goBack", tabId, (wc) => {
2189+
leavePage("goBack", tabId, (wc) => {
21562190
if (wc.navigationHistory.canGoBack()) wc.navigationHistory.goBack();
21572191
});
21582192
const goForward = (tabId: string) =>
2159-
withWebContents("goForward", tabId, (wc) => {
2193+
leavePage("goForward", tabId, (wc) => {
21602194
if (wc.navigationHistory.canGoForward()) wc.navigationHistory.goForward();
21612195
});
2162-
const refresh = (tabId: string) => withWebContents("refresh", tabId, (wc) => wc.reload());
2196+
const refresh = (tabId: string) => leavePage("refresh", tabId, (wc) => wc.reload());
21632197
const stopLoading = (tabId: string) => withWebContents("stopLoading", tabId, (wc) => wc.stop());
21642198
const hardReload = (tabId: string) =>
2165-
withWebContents("hardReload", tabId, (wc) => wc.reloadIgnoringCache());
2199+
leavePage("hardReload", tabId, (wc) => wc.reloadIgnoringCache());
21662200
const findInPage = (
21672201
tabId: string,
21682202
text: string,

0 commit comments

Comments
 (0)