[Bug]: Firefox: actions hang on "waiting for navigation to finish" after a stale-while-revalidate background revalidation

Đang mở
#43,995 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, playwright, typescript

Hướng nghiên cứu

Start with the supplied repro.mjs and run its swr and control modes to confirm the Firefox-specific hang; compare the navigation requests and the 304 revalidation described in the issue. Then trace how Playwright handles Firefox navigation requests and identify why the background revalidation is treated as an unfinished page navigation. Done when the assertion completes in the SWR case and the control modes still pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Version

1.65.0

Steps to reproduce

Repository: https://github.com/stefanprobst/pw-firefox-swr-repro

git clone https://github.com/stefanprobst/pw-firefox-swr-repro && cd pw-firefox-swr-repro
npm install
npx playwright install firefox
node repro.mjs swr 20           # Cache-Control: s-maxage=900, stale-while-revalidate=31535100
node repro.mjs control 20       # Cache-Control: max-age=0, must-revalidate
node repro.mjs swr-pref-off 20  # stale-while-revalidate, with network.http.stale_while_revalidate.enabled = false

repro.mjs:

// Firefox: after page.goto(), actions hang on "waiting for navigation to finish" when the document was cached by
// another page in the same context and is served with stale-while-revalidate.
// Usage: node repro.mjs [swr|control|swr-pref-off] [trials]
import http from "node:http";
import { expect, firefox } from "@playwright/test";

const mode = process.argv[2] ?? "swr";
const trials = Number(process.argv[3] ?? 10);
const cacheControl = mode === "control" ? "max-age=0, must-revalidate" : "s-maxage=900, stale-while-revalidate=31535100";

const server = http.createServer((req, res) => {
	const headers = { "content-type": "text/html; charset=utf-8", "cache-control": cacheControl, etag: '"v1"' };
	if (req.headers["if-none-match"] === '"v1"') {
		res.writeHead(304, headers);
		res.end();
		return;
	}
	res.writeHead(200, headers);
	res.end("<!doctype html><title>t</title><h1>Hello</h1>");
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const url = `http://127.0.0.1:${server.address().port}/`;

const browser = await firefox.launch({
	firefoxUserPrefs: mode === "swr-pref-off" ? { "network.http.stale_while_revalidate.enabled": false } : {},
});
let hangs = 0;
for (let i = 0; i < trials; i++) {
	const context = await browser.newContext();
	const other = await context.newPage();
	await other.goto(url); // caches the document
	const page = await context.newPage();
	await page.goto(url);
	await page.waitForTimeout(1_000);
	try {
		await expect(page.locator("h1")).toHaveText("Hello", { timeout: 5_000 });
	} catch (error) {
		hangs++;
		if (hangs === 1) console.log(String(error).split("\n").slice(0, 10).join("\n") + "\n");
	}
	await context.close();
}
console.log(`${mode}: ${hangs} of ${trials} hung (Playwright ${(await import("playwright/package.json", { with: { type: "json" } })).default.version}, Firefox ${browser.version()})`);
await browser.close();
server.close();
Expected behavior

The assertion passes, as it does in the control modes, and in Chromium and WebKit.

Actual behavior

The assertion on the second page hangs, although the page has loaded:

Error: expect(locator).toHaveText(expected) failed

Locator:  locator('h1')
Expected: "Hello"
Received: ""
Timeout:  5000ms

Call log:
  - Expect "toHaveText" locator('h1') with timeout 5000ms
  - waiting for locator('h1')
    - waiting for "http://127.0.0.1:42039/" navigation to finish...
Mode Hung
swr 19 of 20
control 0 of 20
swr-pref-off 0 of 20
Additional context

Because the other page has cached the document, Firefox serves it from the cache and revalidates it in the background.
Playwright seems to report that revalidation as a navigation of the page, which never finishes:

  • A single page.goto() reports two requests with request.isNavigationRequest() === true: the cached 200, and a
    conditional request with cache-control: max-age=0 that gets a 304. Only one document is loaded.
  • Without the wait, the assertion usually runs before the revalidation is reported, and passes. In a test suite, any
    later action hangs instead, e.g. an assertion after a click.
  • locator.textContent() in place of expect(locator).toHaveText() did not hang (0 of 10).
  • Found in a Next.js app, which sends this Cache-Control header for prerendered pages that revalidate: a Firefox test
    that opens such a page in two tabs, and toggles a setting that syncs between them, failed in 29 of 30 runs, and in 0
    of 30 with the pref disabled.

Possibly related: #42183 (same symptom, closed without a repro) and #42731 (same symptom, triggered by COOP).

Workaround:

launchOptions: {
  firefoxUserPrefs: { "network.http.stale_while_revalidate.enabled": false },
},
Environment
OS: Linux 7.0.0-38-generic
Node.js: 26.13.0
@playwright/test: 1.65.0
Firefox: 159.0
Ngôn ngữ chính
TypeScript
Star
99.1k
Fork
6.8k
Merge trung bình
16 giờ 56 phút
Pull request đã merge (30 ngày)
299

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/playwright

Tất cả issue của microsoft/playwright

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.