Repository navigation
Description
Version
playwright 1.56.0 (also reproduces with the playwright-core 1.61.0-alpha bundled in @playwright/[email protected])
Steps to reproduce
A tab whose CDP targetId differs from its main frame id is never wired up
correctly by connectOverCDP. The most reliable way to create such a tab from
scratch is to activate a prerendered document (Speculation Rules), which
swaps in a new RenderFrameHost and gives the main frame an id that is
different from the target id.
Minimal, self-contained repro (only dependency: playwright):
npm i playwright
node repro.js// repro.js
// connectOverCDP() attaches to an existing Chrome over CDP. A tab that has
// activated a prerendered document has a CDP targetId that differs from its
// main frame id. Playwright never commits that frame, so page.url() stays ""
// and page.title()/page.evaluate() hang forever.
const http = require('http');
const { spawn } = require('child_process');
const { chromium } = require('playwright');
const CDP = 9333;
const TITLE_TIMEOUT = 6000;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const safeUrl = (p) => { try { return p.url(); } catch (e) { return '<err>'; } };
const server = http.createServer((req, res) => {
res.setHeader('content-type', 'text/html');
if (req.url === '/host')
res.end(`<!doctype html><title>HOST</title>
<script type="speculationrules">{"prerender":[{"source":"list","urls":["/prerendered"]}]}</script>
<script>setTimeout(() => { location.href = '/prerendered'; }, 700);</script>host`);
else if (req.url === '/prerendered')
res.end('<!doctype html><title>PRERENDERED</title>prerendered');
else
res.end('<!doctype html><title>NORMAL</title>normal');
});
(async () => {
await new Promise((r) => server.listen(0, '127.0.0.1', r));
const base = `http://127.0.0.1:${server.address().port}`;
const chrome = spawn(chromium.executablePath(), [
`--remote-debugging-port=${CDP}`, '--user-data-dir=/tmp/cdp-prerender-min',
'--no-first-run', '--no-default-browser-check', '--no-sandbox',
'--enable-features=Prerender2,SpeculationRulesPrerender2',
'about:blank',
], { stdio: 'ignore' });
for (let i = 0; i < 100; i++) { try { if ((await fetch(`http://127.0.0.1:${CDP}/json/version`)).ok) break; } catch (e) {} await sleep(100); }
await sleep(400);
await fetch(`http://127.0.0.1:${CDP}/json/new?${base}/normal`, { method: 'PUT' });
await fetch(`http://127.0.0.1:${CDP}/json/new?${base}/host`, { method: 'PUT' });
await sleep(1600); // let the prerender activate
const browser = await chromium.connectOverCDP(`http://127.0.0.1:${CDP}`);
const pages = browser.contexts().flatMap((ctx) => ctx.pages());
for (const p of pages) {
const r = await Promise.race([
p.title().then((t) => ({ ok: true, t })),
sleep(TITLE_TIMEOUT).then(() => ({ ok: false })),
]);
console.log(r.ok
? `OK url=${JSON.stringify(safeUrl(p))} title=${JSON.stringify(r.t)}`
: `HANG url=${JSON.stringify(safeUrl(p))} page.title() never resolves <-- BUG`);
}
const ok = await Promise.race([
Promise.all(pages.map((p) => p.title())).then(() => true),
sleep(TITLE_TIMEOUT).then(() => false),
]);
console.log(`\nPromise.all(pages.map(p => p.title())): ${ok ? 'returned' : 'HANG'}`);
await browser.close().catch(() => {});
chrome.kill('SIGKILL');
server.close();
process.exit(0);
})();Expected behavior
connectOverCDP should attach to every existing tab, including one that has
activated a prerendered document. page.url() should return the activated
URL and page.title() / page.evaluate() should resolve.
Actual behavior
For the prerender-activated tab, page.url() is "" and page.title()
(and any page.evaluate()) never resolves. Output:
OK url="about:blank" title=""
HANG url="" page.title() never resolves <-- BUG
OK url="http://127.0.0.1:.../normal" title="NORMAL"
Promise.all(pages.map(p => p.title())): HANG
The single stuck tab makes any code that awaits all tabs hang. In particular
@playwright/mcp's browser_tabs "list" action does
Promise.all(pages.map(p => p.title())), so listing tabs never returns and the
MCP tool call times out. This is how we originally hit the bug: a real app tab
ended up in the diverged state and browser_tabs list hung indefinitely.
Additional context
Root cause (paths from the playwright-core bundled in @playwright/[email protected],
same logic in chromium/crPage.ts):
When the CDP target id and the main frame id diverge, _sessions is keyed by
targetId but frame events arrive with frameId, so _sessionForFrame cannot
find the main frame's session and throws:
_sessionForFrame(frame) {
while (!this._sessions.has(frame._id)) {
const parent = frame.parentFrame();
if (!parent)
throw new Error(`Frame has been detached.`); // main frame: parent === null -> throws
frame = parent;
}
return this._sessions.get(frame._id);
}_eventBelongsToStaleFrame calls it for every frame event, so the throw
propagates out of the event handlers:
_eventBelongsToStaleFrame(frameId) {
const frame = this._page.frameManager.frame(frameId);
if (!frame) return true;
const session = this._crPage._sessionForFrame(frame); // throws for the diverged main frame
return session && session !== this && !session._swappedIn;
}As a result:
_onFrameNavigatedthrows beforeframeCommittedNewDocumentNavigation, so the
URL is never committed ->page.url() === ""._onExecutionContextCreatedthrows before registering the context, so the
main world is never created ->page.title()/page.evaluate()wait forever.
In other words, a single tab in the targetId !== mainFrameId state is enough to
permanently wedge any Promise.all over all pages.
Suggested fix
_sessionForFrame should fall back to the page's main-frame session (or the
FrameSession whose targetId owns the page) instead of throwing when a
main frame's id is not present in _sessions, so that prerender-activated
(target/frame-diverged) tabs are wired up correctly. Independently,
@playwright/mcp's browser_tabs list could use Promise.allSettled and/or a
per-tab page.title() timeout so one wedged tab cannot hang the whole listing.
Environment
System:
OS: Linux 6.6 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
CPU: (24) arm64 unknown
Memory: 49.22 GB / 63.62 GB
Container: Yes
Binaries:
Node: 24.14.1 - /usr/bin/node
npm: 11.11.0 - /usr/bin/npm
# Reproduced with:
# playwright 1.56.0, and playwright-core 1.61.0-alpha bundled in @playwright/mcp 0.0.76
# Browser: bundled Chromium (Chromium headless shell also reproduces)
Activity
dgozman commentedon Jun 23, 2026
Wonda (@jk4837) This is correct, Playwright has some assumptions about CDP. Normally, prerendering would be disabled by Playwright. I'd recommend to not run with
--enable-features=Prerender2,SpeculationRulesPrerender2. As explained in our docs, passing custom args can disrupt Playwright operation. Closing as outside of Playwright's scope.jk4837 commentedon Jun 24, 2026
Thanks for taking the time to look into this.
To clarify: the
--enable-features=Prerender2,SpeculationRulesPrerender2flag was only something I added to make the bug reproduce deterministically — I did not have it set when I actually hit the issue. After digging in, I found that modern Chrome enables Speculation Rules prerendering by default, so it can still be triggered without any custom flags. I've now switched to--disable-features=Prerender2,SpeculationRulesPrerender2(and also added--disable-back-forward-cache) to turn it off, and I'll keep an eye on whether it still happens.One thing I'd still raise: even if prerendering is out of scope, the failure mode itself seems problematic — the affected tab is left with
page.url() === ""andpage.title()/page.evaluate()that never resolve (an indefinite hang, not a timeout). A single such tab wedges anyPromise.allover pages, e.g.@playwright/mcp'sbrowser_tabs"list", which then hangs forever. So I'd still suggest a timeout / fail-fast here. That said, if you consider it low priority, I'm fine to revisit once I can reproduce it again without the flag.Thanks again!
Browser use: a tab Chrome activated from a prerender answers reads (t…