From 67a2a3242ccab0b9d1eb1c9de4ebef01eacd1842 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 15:02:14 +0100 Subject: [PATCH 1/2] =?UTF-8?q?test:=20reproduce=20#1005=20=E2=80=94=20a?= =?UTF-8?q?=20/v1/events=20body=20the=20Events=20tab=20test's=20reader=20h?= =?UTF-8?q?as=20not=20read=20when=20host.dispose()=20closes=20the=20page?= =?UTF-8?q?=20escapes=20as=20a=20test=20failure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workers-events-read-outlives-page.spec.ts | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 e2e/console/workers-events-read-outlives-page.spec.ts diff --git a/e2e/console/workers-events-read-outlives-page.spec.ts b/e2e/console/workers-events-read-outlives-page.spec.ts new file mode 100644 index 000000000..6f487ed9b --- /dev/null +++ b/e2e/console/workers-events-read-outlives-page.spec.ts @@ -0,0 +1,118 @@ +import type { Response } from "@playwright/test"; + +import { expect, startDaemon, signIn, test } from "./fixtures.js"; + +/** + * why: reproduces #1005. The Events tab test in `workers.spec.ts` reads the body of every + * `/v1/events` response with a `page.on("response")` listener that calls `void response.json()` + * and never awaits it, and the listener stays on for the rest of the test. The single host's + * Events tab, the test's last page, asks for its first older page (`before=…&limit=100`) as it + * opens; the test's last assertion does not need that answer, so the test can reach its `finally` + * while the body is unread. `host.dispose()` closes the page first, the read rejects with "Target + * page, context or browser has been closed", and Playwright fails the test on that rejection + * although every assertion passed. The lane hits that window by accident under load; this holds + * it open: the older-page read is parked before it reads, the host is disposed, and only then is + * the read let go. The park is the injection; the read is the lane's own. + */ + +const IOS = { availableOsVersions: ["18.4"], knownModels: ["iPhone 16"] }; + +test.describe("the workers page Events tab reader against a page closed under a read", () => { + test("a /v1/events body still unread when host.dispose() closes the page is dropped by the Events tab test's reader instead of escaping it", async ({ + page, + }) => { + const host = await startDaemon("worker", { driverScript: { ios: IOS } }); + const escaped: string[] = []; + const answered: { readonly payload?: { readonly workerId?: string } }[] = []; + const reads: Promise[] = []; + let parkOlder = false; + let parkedArrived = (): void => {}; + let releaseParked = (): void => {}; + const arrived = new Promise((resolve) => { + parkedArrived = resolve; + }); + const released = new Promise((resolve) => { + releaseParked = resolve; + }); + // why: the lane's read verbatim, but for the park and for recording the rejection: Playwright + // fails the whole test on it at the read's line, so it is kept to fail on a named assertion. + const read = (response: Response): Promise => + response.json().then((body: { events?: (typeof answered)[number][] }) => { + answered.push(...(body.events ?? [])); + }); + page.on("response", (response) => { + const url = new URL(response.url()); + if (url.pathname !== "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/v1/events") return; + const park = parkOlder && url.searchParams.has("before"); + if (park) parkOlder = false; + reads.push( + (async () => { + if (park) { + parkedArrived(); + await released; + } + await read(response); + })().catch((error: unknown) => { + escaped.push(String(error).split("\n")[0] ?? ""); + }), + ); + }); + + try { + const granted = await host.cli([ + "lease", + "--platform", + "ios", + "--device", + "iPhone 16", + "--os", + "18.4", + "--detach", + "--agent-id", + "console-e2e-host", + ]); + expect(granted.code, granted.stderr).toBe(0); + const listed = await host.cli(["worker", "list", "--json"]); + expect(listed.code, listed.stderr).toBe(0); + const workerId = (listed.json as { workers: readonly { id: string }[] }).workers[0]?.id ?? ""; + expect(workerId).not.toBe(""); + const eventsTab = new URL(`/fleet/${encodeURIComponent(workerId)}?tab=events`, host.url).href; + const forHost = page.getByRole("list", { name: "Events for this worker" }); + + await page.goto(host.url); + await signIn(page, host.tokens.operator); + await expect(page.getByRole("heading", { level: 1, name: "Overview" })).toBeVisible(); + + // why: the same read lands its events while the page is open, so a rejection below is the + // page closing under it and not a read that never worked (testing rule 2). + const older = page.waitForResponse( + (response) => + new URL(response.url()).pathname === "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/v1/events" && + new URL(response.url()).searchParams.has("before"), + ); + await page.goto(eventsTab); + await expect(forHost.getByText("lease.granted").first()).toBeVisible(); + await older; + await Promise.all(reads); + expect(answered.length).toBeGreaterThan(0); + expect(escaped).toEqual([]); + + parkOlder = true; + await page.goto(eventsTab); + await expect(forHost.getByText("lease.granted").first()).toBeVisible(); + await arrived; + await host.dispose(); + expect(page.isClosed()).toBe(true); + releaseParked(); + await Promise.all(reads); + + expect( + escaped, + "the reader must read or quietly drop a /v1/events body the page closed under, not let the rejection escape it", + ).toEqual([]); + } finally { + releaseParked(); + await host.dispose().catch(() => undefined); + } + }); +}); From 2c4e03401447e3beed2e42827081f18bd4343be1 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sun, 25 Oct 2026 15:05:20 +0100 Subject: [PATCH 2/2] fix(e2e): the Events tab test reads every /v1/events body before the page closes (#1005) --- .../workers-events-read-outlives-page.spec.ts | 118 ------------------ e2e/console/workers.spec.ts | 19 ++- 2 files changed, 13 insertions(+), 124 deletions(-) delete mode 100644 e2e/console/workers-events-read-outlives-page.spec.ts diff --git a/e2e/console/workers-events-read-outlives-page.spec.ts b/e2e/console/workers-events-read-outlives-page.spec.ts deleted file mode 100644 index 6f487ed9b..000000000 --- a/e2e/console/workers-events-read-outlives-page.spec.ts +++ /dev/null @@ -1,118 +0,0 @@ -import type { Response } from "@playwright/test"; - -import { expect, startDaemon, signIn, test } from "./fixtures.js"; - -/** - * why: reproduces #1005. The Events tab test in `workers.spec.ts` reads the body of every - * `/v1/events` response with a `page.on("response")` listener that calls `void response.json()` - * and never awaits it, and the listener stays on for the rest of the test. The single host's - * Events tab, the test's last page, asks for its first older page (`before=…&limit=100`) as it - * opens; the test's last assertion does not need that answer, so the test can reach its `finally` - * while the body is unread. `host.dispose()` closes the page first, the read rejects with "Target - * page, context or browser has been closed", and Playwright fails the test on that rejection - * although every assertion passed. The lane hits that window by accident under load; this holds - * it open: the older-page read is parked before it reads, the host is disposed, and only then is - * the read let go. The park is the injection; the read is the lane's own. - */ - -const IOS = { availableOsVersions: ["18.4"], knownModels: ["iPhone 16"] }; - -test.describe("the workers page Events tab reader against a page closed under a read", () => { - test("a /v1/events body still unread when host.dispose() closes the page is dropped by the Events tab test's reader instead of escaping it", async ({ - page, - }) => { - const host = await startDaemon("worker", { driverScript: { ios: IOS } }); - const escaped: string[] = []; - const answered: { readonly payload?: { readonly workerId?: string } }[] = []; - const reads: Promise[] = []; - let parkOlder = false; - let parkedArrived = (): void => {}; - let releaseParked = (): void => {}; - const arrived = new Promise((resolve) => { - parkedArrived = resolve; - }); - const released = new Promise((resolve) => { - releaseParked = resolve; - }); - // why: the lane's read verbatim, but for the park and for recording the rejection: Playwright - // fails the whole test on it at the read's line, so it is kept to fail on a named assertion. - const read = (response: Response): Promise => - response.json().then((body: { events?: (typeof answered)[number][] }) => { - answered.push(...(body.events ?? [])); - }); - page.on("response", (response) => { - const url = new URL(response.url()); - if (url.pathname !== "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/v1/events") return; - const park = parkOlder && url.searchParams.has("before"); - if (park) parkOlder = false; - reads.push( - (async () => { - if (park) { - parkedArrived(); - await released; - } - await read(response); - })().catch((error: unknown) => { - escaped.push(String(error).split("\n")[0] ?? ""); - }), - ); - }); - - try { - const granted = await host.cli([ - "lease", - "--platform", - "ios", - "--device", - "iPhone 16", - "--os", - "18.4", - "--detach", - "--agent-id", - "console-e2e-host", - ]); - expect(granted.code, granted.stderr).toBe(0); - const listed = await host.cli(["worker", "list", "--json"]); - expect(listed.code, listed.stderr).toBe(0); - const workerId = (listed.json as { workers: readonly { id: string }[] }).workers[0]?.id ?? ""; - expect(workerId).not.toBe(""); - const eventsTab = new URL(`/fleet/${encodeURIComponent(workerId)}?tab=events`, host.url).href; - const forHost = page.getByRole("list", { name: "Events for this worker" }); - - await page.goto(host.url); - await signIn(page, host.tokens.operator); - await expect(page.getByRole("heading", { level: 1, name: "Overview" })).toBeVisible(); - - // why: the same read lands its events while the page is open, so a rejection below is the - // page closing under it and not a read that never worked (testing rule 2). - const older = page.waitForResponse( - (response) => - new URL(response.url()).pathname === "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/v1/events" && - new URL(response.url()).searchParams.has("before"), - ); - await page.goto(eventsTab); - await expect(forHost.getByText("lease.granted").first()).toBeVisible(); - await older; - await Promise.all(reads); - expect(answered.length).toBeGreaterThan(0); - expect(escaped).toEqual([]); - - parkOlder = true; - await page.goto(eventsTab); - await expect(forHost.getByText("lease.granted").first()).toBeVisible(); - await arrived; - await host.dispose(); - expect(page.isClosed()).toBe(true); - releaseParked(); - await Promise.all(reads); - - expect( - escaped, - "the reader must read or quietly drop a /v1/events body the page closed under, not let the rejection escape it", - ).toEqual([]); - } finally { - releaseParked(); - await host.dispose().catch(() => undefined); - } - }); -}); diff --git a/e2e/console/workers.spec.ts b/e2e/console/workers.spec.ts index 8a931e65b..b3785ff14 100644 --- a/e2e/console/workers.spec.ts +++ b/e2e/console/workers.spec.ts @@ -1,4 +1,4 @@ -import type { Locator, Page } from "@playwright/test"; +import type { Locator, Page, Response } from "@playwright/test"; import { waitFor } from "../helpers/wait.js"; import { @@ -823,12 +823,16 @@ test.describe("the workers page", () => { const url = new URL(request.url()); if (url.pathname === "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/v1/events") asked.push(url.search); }); - page.on("response", (response) => { + const reads: Promise[] = []; + const readEvents = (response: Response) => { if (new URL(response.url()).pathname !== "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/v1/events") return; - void response.json().then((body: { events?: (typeof answered)[number][] }) => { - answered.push(...(body.events ?? [])); - }); - }); + reads.push( + response.json().then((body: { events?: (typeof answered)[number][] }) => { + answered.push(...(body.events ?? [])); + }), + ); + }; + page.on("response", readEvents); // why: a page of older events holds 100, so more than 100 of worker A's own are needed. for (let round = 0; round < 25; round += 1) { const granted = await fleet.gateway.cli([ @@ -867,6 +871,9 @@ test.describe("the workers page", () => { expect(asked.filter((query) => !query.includes(`worker=${encodeURIComponent(idA)}`))).toEqual( [], ); + // why: a body still unread when host.dispose() closes the page would reject the test. + page.off("response", readEvents); + await Promise.all(reads); expect(answered.length).toBeGreaterThan(100); expect(answered.filter((event) => event.payload?.workerId !== idA)).toEqual([]);