@devries-cameron20 / t3code Mirror · read-only / commits / 43ba03c82a
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 22:15 UTC

fix(server): preview_wait_for stops at its timeout when the page stops answering (#32846)

Entrpi committed Nov 12, 2026 at 10:07 UTC 43ba03c82a8460d972f34d99a6021bbc1165cc20
2 files changed +41 -10
apps/server/src/preview/ServerBrowserPage.test.ts
+20
@@ -533,6 +533,26 @@ describe("server browser evaluate", () => {
533 });
534 });
535
536 +describe("server browser wait", () => {
537 + it("ends at its timeout when a check never answers", async () => {
538 + // As on a desktop tab whose Playwright utility world never answers.
539 + const page = {
540 + locator: () => ({ count: () => new Promise<number>(() => {}) }),
541 + } as unknown as Page;
542 + vi.useFakeTimers();
543 + try {
544 + let failure: unknown;
545 + void ServerBrowserPage.waitFor(page, { selector: "button", timeoutMs: 300 }).catch(
546 + (error: unknown) => (failure = error),
547 + );
548 + await vi.advanceTimersByTimeAsync(300);
549 + expect(failure).toMatchObject({ tag: "PreviewAutomationTimeoutError" });
550 + } finally {
551 + vi.useRealTimers();
552 + }
553 + });
554 +});
555 +
556 describe("server browser net errors", () => {
557 it.each([
558 ["net::ERR_CONNECTION_REFUSED", "ERR_CONNECTION_REFUSED", -102],
apps/server/src/preview/ServerBrowserPage.ts
+21 -10
@@ -945,7 +945,6 @@ export const evaluate = async (
945
946 export const waitFor = async (page: Page, input: PreviewAutomationWaitForInput) => {
947 const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
948 - const deadline = Date.now() + timeoutMs;
948 const locator = targetLocator(page, input);
949 const { text, urlIncludes } = input;
950 const checks: Array<() => Promise<boolean>> = [];
@@ -960,16 +959,28 @@ export const waitFor = async (page: Page, input: PreviewAutomationWaitForInput)
959 );
960 }
961 if (urlIncludes !== undefined) checks.push(async () => page.url().includes(urlIncludes));
963 - for (;;) {
964 - const results = await Promise.all(checks.map((check) => check()));
965 - if (results.every(Boolean)) return;
966 - if (Date.now() >= deadline) {
967 - throw new ServerBrowserOperationError({
968 - tag: "PreviewAutomationTimeoutError",
969 - message: `Waited ${timeoutMs}ms without every condition holding.`,
970 - });
962 + // `count()` and `evaluate()` take no timeout; a stalled check would hold the tab's control queue.
963 + let timer: ReturnType<typeof setTimeout> | undefined;
964 + const expired = new Promise<never>((_, reject) => {
965 + timer = setTimeout(
966 + () =>
967 + reject(
968 + new ServerBrowserOperationError({
969 + tag: "PreviewAutomationTimeoutError",
970 + message: `Waited ${timeoutMs}ms without every condition holding.`,
971 + }),
972 + ),
973 + timeoutMs,
974 + );
975 + });
976 + try {
977 + for (;;) {
978 + const results = await Promise.race([Promise.all(checks.map((check) => check())), expired]);
979 + if (results.every(Boolean)) return;
980 + await Promise.race([new Promise((resolve) => setTimeout(resolve, WAIT_POLL_MS)), expired]);
981 }
972 - await new Promise((resolve) => setTimeout(resolve, WAIT_POLL_MS));
982 + } finally {
983 + clearTimeout(timer);
984 }
985 };
986