Skip to content

Commit fb48330

Browse files
authored
Renderer-crash test: wait for the crash itself, and give the fixture time under load (#2800)
The fixture's tests ran under bun's default 5-second timeout. Under CI's load, three crashes and new pages took longer: bun failed the test for its time, killed the Chromium it had left running, and every fixture test after it failed, which the parent test read as a crash that was not run again (merge-queue run 46925536152). The fixture now gets 60 seconds a test. The harness waited a fixed 100ms after every failure for Playwright's crash event. It now waits for the event itself, only when the failure says its page went away, for 3 seconds at most, and decides at once for any other failure. The fixture's crashRenderer waits for the page's crash event rather than taking goto's failure to mean the renderer is gone: goto fails with ERR_ABORTED about 200ms before the renderer dies, and up to seconds before under load.
1 parent 3730f02 commit fb48330

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎app/tests/renderer-crash.fixture.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ function go(name: string): number {
2121
/** Open a page and crash its renderer, as Chromium does on `chrome://crash`. */
2222
async function crashRenderer() {
2323
const page = await browser.newPage();
24+
const crashed = page.waitForEvent("crash");
25+
// The navigation can end before the renderer dies, or fail as it does: the crash is what is waited for.
2426
await page.goto("chrome://crash").catch(() => undefined);
25-
// The crash is said by now; whatever the test asks of the page next fails as the real ones did.
27+
await crashed;
28+
// Whatever the test asks of the page next fails as the real ones did.
2629
await page.evaluate(() => document.title);
2730
}
2831

‎app/tests/renderer-crash.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ import { join } from "node:path";
1616
*/
1717
const asked = process.env.OPENBOT_APP_BROWSER === "1";
1818

19+
/*
20+
* Each of the fixture's tests gets this long, all its goes together. Three crashes and three new
21+
* pages took over bun's own 5 seconds under CI's load: bun then failed the test for its time, closed
22+
* the browser it had left running, and every test after it failed too.
23+
*/
24+
const FIXTURE_TEST_MS = 60_000;
25+
1926
describe.skipIf(!asked)("a renderer crash in a real-browser test", () => {
2027
test("is run again, up to twice; any other failure is not", async () => {
2128
const run = Bun.spawn(
22-
["bun", "test", join(import.meta.dir, "renderer-crash.fixture.ts")],
29+
[
30+
"bun",
31+
"test",
32+
`--timeout=${FIXTURE_TEST_MS}`,
33+
join(import.meta.dir, "renderer-crash.fixture.ts"),
34+
],
2335
{
2436
cwd: join(import.meta.dir, "..", ".."),
2537
env: { ...process.env, OPENBOT_APP_BROWSER: "1" },
@@ -71,5 +83,5 @@ describe.skipIf(!asked)("a renderer crash in a real-browser test", () => {
7183
// Any other failure of a navigation is the test's, at once.
7284
expect(result("a reload fails some other way")).toBe("fail");
7385
expect(goes("a reload fails some other way")).toBe(1);
74-
}, 120_000);
86+
}, 300_000);
7587
});

‎app/tests/renderer-crash.ts‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ import type { Browser, BrowserContext, Page } from "playwright-core";
2929
/** How many times a page's renderer has crashed in this process, as Playwright reported it. */
3030
let renderersCrashed = 0;
3131

32-
/** Count every crash of a page made in `context`. */
32+
/** Who is waiting for the next crash: each is told the moment Playwright reports one. */
33+
const waitingForCrash = new Set<() => void>();
34+
35+
/** Count every crash of a page made in `context`, and tell whoever is waiting for one. */
3336
function watchContext(context: BrowserContext) {
3437
context.on("page", (page: Page) => {
3538
page.on("crash", () => {
3639
renderersCrashed += 1;
40+
for (const told of waitingForCrash) told();
3741
});
3842
});
3943
return context;
@@ -85,6 +89,39 @@ export function webKitFailedInside(error: unknown): boolean {
8589
return String(error).includes(WEBKIT_INTERNAL_ERROR);
8690
}
8791

92+
/*
93+
* WHETHER A FAILURE WAS A CRASH IS READ FROM PLAYWRIGHT'S OWN CRASH EVENT, NOT FROM A CLOCK.
94+
*
95+
* Playwright reports a crash (`page.on("crash")`) before it fails the calls the crash broke, so a
96+
* failure the crash caused nearly always finds it counted already. A failure that says its page went
97+
* away (Playwright's words in `PAGE_GONE`) and finds no crash counted waits for one, and stops the
98+
* moment it comes, or after `CRASH_REPORT_MS` with none: then it is the test's. Any other failure is
99+
* the test's at once. It used to wait a fixed 100ms for every failure, which a slow machine outran.
100+
*/
101+
const PAGE_GONE =
102+
/Target crashed|Page crashed|page crashed|Target page, context or browser has been closed/;
103+
104+
/** The longest a failure that says its page went away waits for Playwright to report the crash. */
105+
const CRASH_REPORT_MS = 3_000;
106+
107+
/** Whether a page's renderer crashed since the count stood at `before`, as `error` failed. */
108+
async function crashedSince(before: number, error: unknown): Promise<boolean> {
109+
if (renderersCrashed !== before) return true;
110+
if (!PAGE_GONE.test(String(error))) return false;
111+
return new Promise((resolve) => {
112+
const told = () => {
113+
clearTimeout(none);
114+
waitingForCrash.delete(told);
115+
resolve(true);
116+
};
117+
const none = setTimeout(() => {
118+
waitingForCrash.delete(told);
119+
resolve(renderersCrashed !== before);
120+
}, CRASH_REPORT_MS);
121+
waitingForCrash.add(told);
122+
});
123+
}
124+
88125
/**
89126
* Run `body`, and run it again if a page's renderer crashed while it ran, up to `CRASH_RETRIES`
90127
* times. Any other failure, and a crash on the last go, is the test's.
@@ -98,9 +135,7 @@ export async function survivingRendererCrash(
98135
await body();
99136
return;
100137
} catch (error) {
101-
// Playwright says the renderer crashed as the calls waiting on its page fail: let it say so.
102-
await new Promise((done) => setTimeout(done, 100));
103-
const crashed = renderersCrashed !== before;
138+
const crashed = await crashedSince(before, error);
104139
const failedInside = !crashed && webKitFailedInside(error);
105140
if ((!crashed && !failedInside) || go === CRASH_RETRIES) throw error;
106141
console.warn(

0 commit comments

Comments
 (0)