From f0b7a19e02cda87628cfb149c85fc17954315269 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 11:34:58 +0200 Subject: [PATCH 1/4] =?UTF-8?q?test:=20reproduce=20#642=20=E2=80=94=20rout?= =?UTF-8?q?e.fetch=20refused=20when=20the=20gateway's=20listener=20stops?= =?UTF-8?q?=20between=20the=20page's=20interception=20and=20the=20fetch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workers-gateway-stops-mid-fetch.spec.ts | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 e2e/console/workers-gateway-stops-mid-fetch.spec.ts diff --git a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts new file mode 100644 index 000000000..b1cefb805 --- /dev/null +++ b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts @@ -0,0 +1,146 @@ +import type { Locator, Page, Route } from "@playwright/test"; + +import { waitFor } from "../helpers/wait.js"; +import { disposeFleet, expect, type RunningDaemon, signIn, startFleet, test } from "./fixtures.js"; + +/** + * why: reproduces #642. The workers page's response helpers (`answerWithoutLoad`, `answerMemory`) + * answer the page's re-reads of `/v1/workers` by fetching them from the gateway through + * `route.fetch()`, and the page re-reads every second. A re-read intercepted but not yet fetched + * when the test's `finally` disposes the fleet gets a refused connect — a listener destroyed + * mid-request reads the same way as a socket hang up — and Playwright attributes the handler's + * rejection to the test, so the spec dies on the helper's `route.fetch()` line although every + * assertion it made had passed. The lane races that window by accident; this holds it open: the + * next re-read is parked inside the handler before it fetches, the gateway's listener is confirmed + * gone, and only then is the parked fetch let go. The park is the injection; every other line is + * the lane's own. + */ + +const IOS = { availableOsVersions: ["18.4"], knownModels: ["iPhone 16"] }; + +async function openConsole(page: Page, daemon: RunningDaemon): Promise { + await page.goto(daemon.url); + await signIn(page, daemon.tokens.operator); + await page.getByRole("link", { name: "Workers", exact: true }).click(); + await expect(page.getByRole("heading", { level: 1, name: "Workers" })).toBeVisible(); +} + +function workerRows(page: Page): Locator { + return page.getByRole("list", { name: "Workers", exact: true }).getByRole("listitem"); +} + +async function askForWorkerList(page: Page, daemon: RunningDaemon): Promise { + return page.evaluate( + (bearer) => + fetch("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/v1/workers", { headers: { authorization: bearer } }).then( + (response) => response.status, + () => 0, + ), + `Bearer ${daemon.tokens.operator}`, + ); +} + +async function listenerIsGone(daemon: RunningDaemon): Promise { + try { + return (await fetch(new URL("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/v1/healthz", daemon.url))).status >= 400; + } catch { + return true; + } +} + +test.describe("the workers page response helper against a gateway whose listener stops", () => { + test("the re-read whose gateway listener stops between its interception and the helper's fetch is dropped by the helper instead of escaping it", async ({ + page, + }) => { + const fleet = await startFleet([ + { driverScript: { ios: IOS }, label: "worker-a" }, + { driverScript: { ios: IOS }, label: "worker-b" }, + ]); + const escaped: string[] = []; + let answeredAfterInjection = 0; + let parkNext = false; + let parkedSeen = false; + let parkedArrived = { resolve: () => {} }; + let releaseParked = { resolve: () => {} }; + const arrived = new Promise((resolve) => { + parkedArrived = { resolve }; + }); + const released = new Promise((resolve) => { + releaseParked = { resolve }; + }); + let lastListed: { workers: Record[] } | undefined; + // why: the lane's `answerWithoutLoad` verbatim but for the park, so the refused connect is the + // lane's accident and not a helper of this test's own invention. + const answerWithoutLoad = async (route: Route): Promise => { + if (parkNext && !parkedSeen) { + parkedSeen = true; + parkNext = false; + parkedArrived.resolve(); + await released; + } + const response = await route.fetch(); + const listed = (await response.json()) as { workers: Record[] }; + for (const worker of listed.workers) delete worker.load; + lastListed = listed; + await route.fulfill({ json: listed, response }); + }; + await page.route( + (url) => url.pathname === "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/v1/workers", + async (route: Route) => { + try { + if (parkedSeen && lastListed !== undefined) { + // why: later polls are answered from the last payload, so the only fetch made against + // the dead listener is the parked one this test names. + answeredAfterInjection += 1; + await route.fulfill({ json: lastListed }); + return; + } + await answerWithoutLoad(route); + } catch (error: unknown) { + // why: Playwright fails the whole test on this rejection at the handler line; the test + // records it and keeps going so the failure names the refusal, not a timeout. + escaped.push(String(error).split("\n")[0] ?? ""); + await route.abort().catch(() => undefined); + } + }, + ); + + try { + await openConsole(page, fleet.gateway); + await expect(workerRows(page)).toHaveCount(2); + + // why: the same helper answers through this gateway while it is up, so a refusal below is the + // listener being gone and not a route that never worked (testing rule 2). + expect(await askForWorkerList(page, fleet.gateway)).toBe(200); + expect(escaped).toEqual([]); + + parkNext = true; + const asked = askForWorkerList(page, fleet.gateway); + await arrived; + await disposeFleet(fleet); + await waitFor(() => listenerIsGone(fleet.gateway), { + interval: 50, + label: "the gateway's HTTP listener stopped", + timeout: 10_000, + }); + releaseParked.resolve(); + await waitFor(() => answeredAfterInjection > 0, { + interval: 50, + label: "the first poll answered after the injection", + timeout: 10_000, + }); + + // why: the body only waits and re-asserts from here, as the lane's does around its + // `finally`; `asked` is drained to keep the page's promise from outliving the test. + await asked; + await expect(page.getByRole("heading", { level: 1, name: "Workers" })).toBeVisible(); + + expect( + escaped, + "the helper must answer or quietly drop the re-read whose gateway stops mid-request, not let the connect failure escape it", + ).toEqual([]); + } finally { + await disposeFleet(fleet).catch(() => undefined); + } + }); +}); From 8e565b64c68be84dfc96a1d2308ed26cdd20f4a5 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 11:56:40 +0200 Subject: [PATCH 2/4] test: the workers-list lane helpers drop a re-read refused by a stopping gateway (#642) --- e2e/console/workers-answers.ts | 59 ++++++++++++++++++ .../workers-gateway-stops-mid-fetch.spec.ts | 58 ++++-------------- e2e/console/workers.spec.ts | 61 +------------------ 3 files changed, 74 insertions(+), 104 deletions(-) create mode 100644 e2e/console/workers-answers.ts diff --git a/e2e/console/workers-answers.ts b/e2e/console/workers-answers.ts new file mode 100644 index 000000000..60f183ae8 --- /dev/null +++ b/e2e/console/workers-answers.ts @@ -0,0 +1,59 @@ +import type { Page, Route } from "@playwright/test"; + +interface WorkerPayload { + load?: + | { + memory?: { availableBytes?: number; pressure?: string; totalBytes?: number } | undefined; + } + | undefined; +} + +// why: the two fields a colour case has to state; everything else in the reading stays the +// daemon's. +export interface MemoryClaim { + readonly pressure?: string; + readonly usedPercent: number; +} + +/** + * why: only macOS reports a memory pressure, and how full a CI machine's memory happens to be is + * nobody's to predict — so the colour test states the memory reading it wants and this writes just + * those fields into the daemon's own answer, leaving every other field, its headers and its `Date` + * real. The route is asked again per request, so the reading stays as fresh as the daemon's own. + */ +export async function answerMemory(page: Page, claim: () => MemoryClaim): Promise { + await page.route( + (url) => url.pathname === "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/v1/workers", + async (route: Route) => { + const response = await route.fetch(); + const listed = (await response.json()) as { workers: WorkerPayload[] }; + for (const worker of listed.workers) { + const memory = worker.load?.memory; + const total = memory?.totalBytes; + if (memory === undefined || typeof total !== "number" || total <= 0) continue; + const wanted = claim(); + memory.availableBytes = Math.round((total * (100 - wanted.usedPercent)) / 100); + if (wanted.pressure === undefined) delete memory.pressure; + else memory.pressure = wanted.pressure; + } + await route.fulfill({ json: listed, response }); + }, + ); +} + +/** + * why: a machine's own disk or CPU may already be over a threshold, which would turn every square + * the page draws and hide the state a test is about (a drain, a lost link). A test about squares + * answers the page's read of the worker list without the load reading, so only the states are left. + */ +export async function answerWithoutLoad(page: Page): Promise { + await page.route( + (url) => url.pathname === "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/v1/workers", + async (route: Route) => { + const response = await route.fetch(); + const listed = (await response.json()) as { workers: WorkerPayload[] }; + for (const worker of listed.workers) delete worker.load; + await route.fulfill({ json: listed, response }); + }, + ); +} diff --git a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts index b1cefb805..7d500c322 100644 --- a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts +++ b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts @@ -1,6 +1,7 @@ import type { Locator, Page, Route } from "@playwright/test"; import { waitFor } from "../helpers/wait.js"; +import { answerWithoutLoad } from "./workers-answers.js"; import { disposeFleet, expect, type RunningDaemon, signIn, startFleet, test } from "./fixtures.js"; /** @@ -56,8 +57,6 @@ test.describe("the workers page response helper against a gateway whose listener { driverScript: { ios: IOS }, label: "worker-a" }, { driverScript: { ios: IOS }, label: "worker-b" }, ]); - const escaped: string[] = []; - let answeredAfterInjection = 0; let parkNext = false; let parkedSeen = false; let parkedArrived = { resolve: () => {} }; @@ -68,40 +67,20 @@ test.describe("the workers page response helper against a gateway whose listener const released = new Promise((resolve) => { releaseParked = { resolve }; }); - let lastListed: { workers: Record[] } | undefined; - // why: the lane's `answerWithoutLoad` verbatim but for the park, so the refused connect is the - // lane's accident and not a helper of this test's own invention. - const answerWithoutLoad = async (route: Route): Promise => { - if (parkNext && !parkedSeen) { - parkedSeen = true; - parkNext = false; - parkedArrived.resolve(); - await released; - } - const response = await route.fetch(); - const listed = (await response.json()) as { workers: Record[] }; - for (const worker of listed.workers) delete worker.load; - lastListed = listed; - await route.fulfill({ json: listed, response }); - }; + await answerWithoutLoad(page); + // why: Playwright runs the latest-registered route first, so this one parks the next re-read + // before the lane's real helper above sees it, then hands it on with `fallback()`: the helper's + // `route.fetch()` is the only line that meets the dead listener, as in the lane. await page.route( (url) => url.pathname === "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/v1/workers", async (route: Route) => { - try { - if (parkedSeen && lastListed !== undefined) { - // why: later polls are answered from the last payload, so the only fetch made against - // the dead listener is the parked one this test names. - answeredAfterInjection += 1; - await route.fulfill({ json: lastListed }); - return; - } - await answerWithoutLoad(route); - } catch (error: unknown) { - // why: Playwright fails the whole test on this rejection at the handler line; the test - // records it and keeps going so the failure names the refusal, not a timeout. - escaped.push(String(error).split("\n")[0] ?? ""); - await route.abort().catch(() => undefined); + if (parkNext && !parkedSeen) { + parkedSeen = true; + parkNext = false; + parkedArrived.resolve(); + await released; } + await route.fallback(); }, ); @@ -112,7 +91,6 @@ test.describe("the workers page response helper against a gateway whose listener // why: the same helper answers through this gateway while it is up, so a refusal below is the // listener being gone and not a route that never worked (testing rule 2). expect(await askForWorkerList(page, fleet.gateway)).toBe(200); - expect(escaped).toEqual([]); parkNext = true; const asked = askForWorkerList(page, fleet.gateway); @@ -124,21 +102,11 @@ test.describe("the workers page response helper against a gateway whose listener timeout: 10_000, }); releaseParked.resolve(); - await waitFor(() => answeredAfterInjection > 0, { - interval: 50, - label: "the first poll answered after the injection", - timeout: 10_000, - }); - // why: the body only waits and re-asserts from here, as the lane's does around its - // `finally`; `asked` is drained to keep the page's promise from outliving the test. + // why: once the helper drops the re-read the page's own promise settles (0 when aborted); + // a rejection escaping the helper fails the test at its `route.fetch()` line instead. await asked; await expect(page.getByRole("heading", { level: 1, name: "Workers" })).toBeVisible(); - - expect( - escaped, - "the helper must answer or quietly drop the re-read whose gateway stops mid-request, not let the connect failure escape it", - ).toEqual([]); } finally { await disposeFleet(fleet).catch(() => undefined); } diff --git a/e2e/console/workers.spec.ts b/e2e/console/workers.spec.ts index 0e6b8b648..3a819fc8a 100644 --- a/e2e/console/workers.spec.ts +++ b/e2e/console/workers.spec.ts @@ -1,6 +1,7 @@ -import type { Locator, Page, Route } from "@playwright/test"; +import type { Locator, Page } from "@playwright/test"; import { waitFor } from "../helpers/wait.js"; +import { answerMemory, answerWithoutLoad, type MemoryClaim } from "./workers-answers.js"; import { disposeFleet, expect, @@ -42,64 +43,6 @@ interface WorkerView { }; } -interface WorkerPayload { - load?: - | { - memory?: { availableBytes?: number; pressure?: string; totalBytes?: number } | undefined; - } - | undefined; -} - -// why: the two fields a colour case has to state; everything else in the reading stays the -// daemon's. -interface MemoryClaim { - readonly pressure?: string; - readonly usedPercent: number; -} - -/** - * why: only macOS reports a memory pressure, and how full a CI machine's memory happens to be is - * nobody's to predict — so the colour test states the memory reading it wants and this writes just - * those fields into the daemon's own answer, leaving every other field, its headers and its `Date` - * real. The route is asked again per request, so the reading stays as fresh as the daemon's own. - */ -async function answerMemory(page: Page, claim: () => MemoryClaim): Promise { - await page.route( - (url) => url.pathname === "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/v1/workers", - async (route: Route) => { - const response = await route.fetch(); - const listed = (await response.json()) as { workers: WorkerPayload[] }; - for (const worker of listed.workers) { - const memory = worker.load?.memory; - const total = memory?.totalBytes; - if (memory === undefined || typeof total !== "number" || total <= 0) continue; - const wanted = claim(); - memory.availableBytes = Math.round((total * (100 - wanted.usedPercent)) / 100); - if (wanted.pressure === undefined) delete memory.pressure; - else memory.pressure = wanted.pressure; - } - await route.fulfill({ json: listed, response }); - }, - ); -} - -/** - * why: a machine's own disk or CPU may already be over a threshold, which would turn every square - * the page draws and hide the state a test is about (a drain, a lost link). A test about squares - * answers the page's read of the worker list without the load reading, so only the states are left. - */ -async function answerWithoutLoad(page: Page): Promise { - await page.route( - (url) => url.pathname === "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/v1/workers", - async (route: Route) => { - const response = await route.fetch(); - const listed = (await response.json()) as { workers: WorkerPayload[] }; - for (const worker of listed.workers) delete worker.load; - await route.fulfill({ json: listed, response }); - }, - ); -} - interface Status { readonly devices: readonly { readonly id: string; From cf17fb26035bb4ca1d281a9a3991b28474ffeb48 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 12:13:38 +0200 Subject: [PATCH 3/4] fix: the workers-list lane helpers drop a re-read refused by a stopping gateway (#642) 3 failing -> 0 failing --- e2e/console/workers-answers.ts | 22 +++++++++- .../workers-gateway-stops-mid-fetch.spec.ts | 43 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/e2e/console/workers-answers.ts b/e2e/console/workers-answers.ts index 60f183ae8..6c4853c34 100644 --- a/e2e/console/workers-answers.ts +++ b/e2e/console/workers-answers.ts @@ -8,6 +8,22 @@ interface WorkerPayload { | undefined; } +// why: the page re-reads the list every second, so a re-read can be intercepted just as the test's +// `finally` stops the gateway; the refused connect it gets is the fleet going away, not a failure of +// the test, and Playwright would otherwise charge the rejection to it. Only connect-level errors are +// dropped: any other error is a real fault and must still kill the test. +const CONNECT_ERROR = /ECONNREFUSED|ECONNRESET|socket hang up/i; + +async function fetchOrDrop(route: Route): Promise> | undefined> { + try { + return await route.fetch(); + } catch (error) { + if (!(error instanceof Error) || !CONNECT_ERROR.test(error.message)) throw error; + await route.abort().catch(() => undefined); + return undefined; + } +} + // why: the two fields a colour case has to state; everything else in the reading stays the // daemon's. export interface MemoryClaim { @@ -25,7 +41,8 @@ export async function answerMemory(page: Page, claim: () => MemoryClaim): Promis await page.route( (url) => url.pathname === "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/v1/workers", async (route: Route) => { - const response = await route.fetch(); + const response = await fetchOrDrop(route); + if (response === undefined) return; const listed = (await response.json()) as { workers: WorkerPayload[] }; for (const worker of listed.workers) { const memory = worker.load?.memory; @@ -50,7 +67,8 @@ export async function answerWithoutLoad(page: Page): Promise { await page.route( (url) => url.pathname === "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/v1/workers", async (route: Route) => { - const response = await route.fetch(); + const response = await fetchOrDrop(route); + if (response === undefined) return; const listed = (await response.json()) as { workers: WorkerPayload[] }; for (const worker of listed.workers) delete worker.load; await route.fulfill({ json: listed, response }); diff --git a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts index 7d500c322..14e1d77a7 100644 --- a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts +++ b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts @@ -112,3 +112,46 @@ test.describe("the workers page response helper against a gateway whose listener } }); }); + +type Handler = (route: Route) => Promise; + +function captureHandler(answer: (page: Page) => Promise): Promise { + return new Promise((resolve) => { + const page = { route: async (_match: unknown, handler: Handler) => resolve(handler) }; + void answer(page as unknown as Page); + }); +} + +test.describe("what the workers page response helper drops and what it lets through", () => { + test("a fetch that fails for a reason other than the connection going away still rejects out of the helper", async () => { + const handler = await captureHandler(answerWithoutLoad); + let aborted = false; + const route = { + abort: async () => { + aborted = true; + }, + fetch: async () => { + throw new Error("getaddrinfo ENOTFOUND gateway.invalid"); + }, + } as unknown as Route; + + await expect(handler(route)).rejects.toThrow("ENOTFOUND"); + expect(aborted).toBe(false); + }); + + test("a fetch refused at connect is aborted and resolves quietly", async () => { + const handler = await captureHandler(answerWithoutLoad); + let aborted = false; + const route = { + abort: async () => { + aborted = true; + }, + fetch: async () => { + throw new Error("connect ECONNREFUSED 127.0.0.1:1"); + }, + } as unknown as Route; + + await handler(route); + expect(aborted).toBe(true); + }); +}); From 759ebe281b5ae2381ddb4b0e52c24296a7108b94 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 17 Oct 2026 13:16:28 +0200 Subject: [PATCH 4/4] test: pin the helpers' drop of socket hang up and ECONNRESET, and answerMemory's drop path (#642) --- .../workers-gateway-stops-mid-fetch.spec.ts | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts index 14e1d77a7..33c884d73 100644 --- a/e2e/console/workers-gateway-stops-mid-fetch.spec.ts +++ b/e2e/console/workers-gateway-stops-mid-fetch.spec.ts @@ -1,7 +1,7 @@ import type { Locator, Page, Route } from "@playwright/test"; import { waitFor } from "../helpers/wait.js"; -import { answerWithoutLoad } from "./workers-answers.js"; +import { answerMemory, answerWithoutLoad } from "./workers-answers.js"; import { disposeFleet, expect, type RunningDaemon, signIn, startFleet, test } from "./fixtures.js"; /** @@ -139,19 +139,33 @@ test.describe("what the workers page response helper drops and what it lets thro expect(aborted).toBe(false); }); - test("a fetch refused at connect is aborted and resolves quietly", async () => { - const handler = await captureHandler(answerWithoutLoad); - let aborted = false; - const route = { - abort: async () => { - aborted = true; - }, - fetch: async () => { - throw new Error("connect ECONNREFUSED 127.0.0.1:1"); - }, - } as unknown as Route; - - await handler(route); - expect(aborted).toBe(true); - }); + const helpers = [ + ["answerWithoutLoad", answerWithoutLoad], + ["answerMemory", (page: Page) => answerMemory(page, () => ({ usedPercent: 50 }))], + ] as const; + const goneAway = [ + ["refused at connect", "connect ECONNREFUSED 127.0.0.1:1"], + ["destroyed mid-request (socket hang up)", "socket hang up"], + ["reset by the peer", "read ECONNRESET"], + ] as const; + + for (const [helperName, helper] of helpers) { + for (const [how, message] of goneAway) { + test(`${helperName}: a fetch ${how} is aborted and resolves quietly`, async () => { + const handler = await captureHandler(helper); + let aborted = false; + const route = { + abort: async () => { + aborted = true; + }, + fetch: async () => { + throw new Error(message); + }, + } as unknown as Route; + + await handler(route); + expect(aborted).toBe(true); + }); + } + } });