From a6a77c4bdb0fd43931f33c57fd33f3fb219d6bca Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Sat, 21 Nov 2026 18:26:05 -0800 Subject: [PATCH 1/3] test(apidocs-astro): print a census of live and detached elements in the navigation leak test Diagnostic only, to read the growth that fails on CI (nodes 1058 to 1448 with the listener count flat) by container and by detached element. Signed-off-by: Sam Gammon --- apps/apidocs-astro/test/browser.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/apps/apidocs-astro/test/browser.test.ts b/apps/apidocs-astro/test/browser.test.ts index 6daa03d..6649e20 100644 --- a/apps/apidocs-astro/test/browser.test.ts +++ b/apps/apidocs-astro/test/browser.test.ts @@ -244,12 +244,35 @@ describe("behaviors across navigations", () => { await afterLoad(page, () => page.evaluate(href => (document.querySelector(`a[href="${href}"]`) as HTMLAnchorElement).click(), i % 2 ? TYPE : OTHER)); } }; + // Which elements the page holds, live and detached (kept alive by something), to read a growth from. + const census = async () => { + const { result } = await cdp.send("Runtime.evaluate", { expression: "Element.prototype", objectGroup: "census" }); + const { objects } = await cdp.send("Runtime.queryObjects", { prototypeObjectId: result.objectId!, objectGroup: "census" }); + const detached = await cdp.send("Runtime.callFunctionOn", { + objectId: objects.objectId!, + functionDeclaration: `function () { + const tally = {}; + for (const el of this) try { if (!el.isConnected) { const k = el.tagName + (typeof el.className === "string" && el.className ? "." + el.className.split(" ")[0] : ""); tally[k] = (tally[k] || 0) + 1; } } catch {} + return JSON.stringify(Object.entries(tally).sort((a, b) => b[1] - a[1]).slice(0, 12)); + }`, + returnByValue: true, + }); + await cdp.send("Runtime.releaseObjectGroup", { objectGroup: "census" }); + const live = await page.evaluate(() => ({ + elements: document.querySelectorAll("*").length, + containers: Array.from(document.body.children).map(c => `${c.tagName.toLowerCase()}.${(c as HTMLElement).className}:${c.querySelectorAll("*").length}`).join(" "), + head: Array.from(document.head.children).map(c => c.tagName.toLowerCase()).join(","), + })); + return { ...live, detached: detached.result.value ?? detached.exceptionDetails?.text }; + }; await loop(4); await page.waitForTimeout(300); const before = await measure(); + console.log(" census before:", JSON.stringify(await census())); await loop(20); await page.waitForTimeout(300); const after = await measure(); + console.log(" census after:", JSON.stringify(await census())); console.log(" listeners and nodes, after 4 and after 24 navigations:", before, after); expect(after.listeners).toBeLessThanOrEqual(before.listeners); expect(after.nodes).toBeLessThanOrEqual(before.nodes * 1.02); From a1faf963e05b23ecf191970762ceec30bd940fa3 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Sat, 21 Nov 2026 18:44:29 -0800 Subject: [PATCH 2/3] test(apidocs-astro): print the node series across navigations (diagnostic) Signed-off-by: Sam Gammon --- apps/apidocs-astro/test/browser.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/apidocs-astro/test/browser.test.ts b/apps/apidocs-astro/test/browser.test.ts index 6649e20..c064f61 100644 --- a/apps/apidocs-astro/test/browser.test.ts +++ b/apps/apidocs-astro/test/browser.test.ts @@ -269,7 +269,12 @@ describe("behaviors across navigations", () => { await page.waitForTimeout(300); const before = await measure(); console.log(" census before:", JSON.stringify(await census())); - await loop(20); + for (let cycle = 1; cycle <= 4; cycle++) { + await loop(4); + await page.waitForTimeout(300); + console.log(` series after ${4 + cycle * 4}:`, JSON.stringify(await measure()), JSON.stringify(await census()).replace(/"head":"[^"]*",/, "")); + } + await loop(4); await page.waitForTimeout(300); const after = await measure(); console.log(" census after:", JSON.stringify(await census())); From 7cc51f3b1275e25e39fe1ca2c853e57090a00883 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Sat, 21 Nov 2026 19:04:24 -0800 Subject: [PATCH 3/3] fix(apidocs): pin where the navigation leak test finds a growth, and count the page's elements The Vitest job on main failed "twenty navigations leave listener and node counts flat" once (nodes 1058 to 1448, listeners and documents flat). I could not reproduce it: not locally with the production build under wrangler dev, with CPU throttled up to 20x, with every request delayed up to 800 ms, with animations slowed 20x, with navigations fired 0 to 150 ms apart (interrupting the previous page), with two cores shared with busy loops, nor in eight runs of the job on GitHub with a census in the test (live elements 644 and no detached elements reachable from script, at 8, 12, 16, 20 and 24 navigations, every time). The client code in src/astro/client.ts replaces the navigator's rows and owns every listener with one AbortController per page, so I found nothing to fix there. The cause of the one failure is not known: the Chromium "Nodes" metric also counts nodes awaiting collection, which the test cannot tell from a leak. So the test now reads the growth itself. It counts the page's live elements and the detached elements still reachable from script (Runtime.queryObjects), and requires the live count and the per-container counts to stay equal and no detached element to remain. These assertions pass on the page as it is. The existing assertions are unchanged. When any fails, the test prints the census before and after, by top-level container, so a growth is located at once. Checked by mutation: with the navigator's old rows no longer removed, the test fails and the census shows div.layout going from 1027 to 3417 elements (listeners 375 to 1175). Also seen once, and not changed: "layout stability > a navigation shifts nothing" read a shift of 4e-7 on a GitHub runner. Signed-off-by: Sam Gammon --- apps/apidocs-astro/test/browser.test.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/apidocs-astro/test/browser.test.ts b/apps/apidocs-astro/test/browser.test.ts index c064f61..cb6a1ad 100644 --- a/apps/apidocs-astro/test/browser.test.ts +++ b/apps/apidocs-astro/test/browser.test.ts @@ -268,20 +268,24 @@ describe("behaviors across navigations", () => { await loop(4); await page.waitForTimeout(300); const before = await measure(); - console.log(" census before:", JSON.stringify(await census())); - for (let cycle = 1; cycle <= 4; cycle++) { - await loop(4); - await page.waitForTimeout(300); - console.log(` series after ${4 + cycle * 4}:`, JSON.stringify(await measure()), JSON.stringify(await census()).replace(/"head":"[^"]*",/, "")); - } - await loop(4); + const censusBefore = await census(); + await loop(20); await page.waitForTimeout(300); const after = await measure(); - console.log(" census after:", JSON.stringify(await census())); + const censusAfter = await census(); console.log(" listeners and nodes, after 4 and after 24 navigations:", before, after); + const grew = after.listeners > before.listeners || after.nodes > before.nodes * 1.02 || after.documents > before.documents; + // The counts alone cannot say where a growth sits: print what the page holds, live and detached, when it fails. + if (grew || censusAfter.elements !== censusBefore.elements || censusAfter.detached !== "[]") { + console.log(" census before:", JSON.stringify(censusBefore), "\n census after: ", JSON.stringify(censusAfter)); + } expect(after.listeners).toBeLessThanOrEqual(before.listeners); expect(after.nodes).toBeLessThanOrEqual(before.nodes * 1.02); expect(after.documents).toBeLessThanOrEqual(before.documents); + // The page's own elements are the same in number, and nothing detached stays reachable from script. + expect(censusAfter.elements).toBe(censusBefore.elements); + expect(censusAfter.containers).toBe(censusBefore.containers); + expect(censusAfter.detached).toBe("[]"); await page.context().close(); }, 120_000); });