"Protocol error: Target closed" sounds like a network failure. It is not. Puppeteer drives Chrome over the DevTools protocol, a "target" is one tab (or the browser itself), and the error means a command was sent to — or was waiting on — a target that has gone. Almost always something in your own script closed it a moment too early; otherwise Chrome died underneath you. This page reproduces both kinds with Puppeteer 25.7, shows which message each one prints, and how to fix it.
How we tested. On 2026-11-21, Puppeteer 25.7.0 (Chrome for Testing 152) and Playwright 1.64.1 on Node.js 20 in WSL2, against a local test page. Each case below is a few lines you can run yourself. We did not reproduce crashes in AWS Lambda or a memory-limited container; the "Chrome died" case was simulated by killing the Chrome process, and we say below where the platform advice comes from.
The messages, and what each one meant
| What happened | Puppeteer 25.7.0 error |
|---|---|
page.close() while a screenshot was still running |
TargetCloseError: Protocol error (Page.captureScreenshot): Target closed |
browser.close() while page.evaluate() was running |
TargetCloseError: Protocol error (Runtime.callFunctionOn): Target closed |
page.evaluate() or page.goto() on a page after page.close() |
Error: Attempted to use detached Frame '3526…C6'. |
browser.newPage() after browser.close() |
ConnectionClosedError: Connection closed. |
browser.newPage() while browser.close() ran (async forEach) |
ProtocolError: Protocol error (Target.createTarget): Failed to open a new tab — or nothing at all, see below |
Chrome process killed during page.evaluate() |
TargetCloseError: Protocol error (Runtime.callFunctionOn): Target closed |
page.screenshot() after Chrome was killed |
TargetCloseError: Protocol error (Page.captureScreenshot): Session closed. Most likely the page has been closed. |
The name in brackets is the DevTools method that was cut off: Runtime.callFunctionOn is evaluate, $eval and friends; Page.captureScreenshot is screenshot; Page.printToPDF is pdf; Target.createTarget is newPage. It tells you which line of your script was running, not why the target closed.
Cause 1: a call that was not awaited
const page = await browser.newPage();
await page.goto(url);
page.screenshot({ path: 'page.png', fullPage: true }); // no await
await page.close();
// TargetCloseError: Protocol error (Page.captureScreenshot): Target closed
The screenshot was still being taken when close() ran. The same happens with a missing await on evaluate, pdf, click or waitForSelector followed by a close. Fix: await every Puppeteer call, and turn on a linter rule that catches floating promises (@typescript-eslint/no-floating-promises), because these are easy to miss in review.
Cause 2: async forEach, then browser.close()
The most common shape of this bug in scraping and screenshot scripts:
const browser = await puppeteer.launch();
urls.forEach(async (url) => {
const page = await browser.newPage();
await page.goto(url);
await page.screenshot({ path: `${slug(url)}.png` });
});
await browser.close(); // runs immediately: forEach does not wait
forEach ignores the promises its callback returns, so browser.close() runs while the three jobs are only starting. We ran this five times. Four times, all three jobs hung forever and threw nothing — browser.newPage() never settled. Once, each failed with Protocol error (Target.createTarget): Failed to open a new tab. Silent hangs are the worse outcome: the script exits with no screenshots and no error, or a long-running worker leaks jobs that never finish.
The fix is to collect the promises and wait for them before closing:
const results = await Promise.allSettled(urls.map(async (url) => {
const page = await browser.newPage();
try {
await page.goto(url);
await page.screenshot({ path: `${slug(url)}.png` });
} finally {
await page.close();
}
}));
await browser.close(); // 3 of 3 succeeded
Promise.allSettled keeps one failing URL from hiding the others; a plain for…of loop with await does the same one page at a time. For more than a handful of URLs, limit how many tabs are open at once — speeding up Puppeteer screenshots measures how throughput changes with the number of tabs.
Cause 3: using a page or browser after closing it
await page.close();
await page.evaluate(() => document.title);
// Error: Attempted to use detached Frame '…'.
await browser.close();
await browser.newPage();
// ConnectionClosedError: Connection closed.
These usually come from shared state: a page object cached in a module, a finally block that closes the browser while a retry is still using it, a test framework's afterEach running before a timer in the test fires. Make one function own the page from newPage() to close(), and do not hand it to code that may outlive it. Apify's guide to Target closed gives the same advice for crawlers: the page was closed before all the code using it had finished.
Cause 4: Chrome died
When the Chrome process ends on its own — killed for using too much memory, a crash, a container being stopped — every pending call fails with Target closed and every new one with Session closed. We simulated it by killing the process:
browser.on('disconnected', () => console.error('Chrome is gone'));
const job = page.evaluate(() => new Promise((r) => setTimeout(r, 3000)));
browser.process().kill('SIGKILL');
await job;
// TargetCloseError: Protocol error (Runtime.callFunctionOn): Target closed
await page.screenshot();
// TargetCloseError: Protocol error (Page.captureScreenshot): Session closed. Most likely the page has been closed.
The disconnected event fired and browser.connected was false. If you see Target closed without any close call in your code, this is the case, and the cause is outside the script:
- Docker's
/dev/shm. A container gets 64 MB of shared memory by default (df -h /dev/shminnode:24-slimon 2026-11-21:64M), and Chrome renders through it, so large pages can crash the tab. Launch withargs: ['--disable-dev-shm-usage']or run the container with--shm-size=1gb. Running Puppeteer in Docker has a Dockerfile we built and ran. We did not reproduce a shared-memory crash for this page. - Memory limits. A small container, a CI runner or a serverless function with too little memory. Heavy pages, full-page screenshots of very long pages and many open tabs add up. We did not measure the limits on Lambda or Vercel.
- The job runner killing it. A worker timeout that kills the process tree takes Chrome with it.
Handle it by treating a disconnected browser as gone: on disconnected, stop sending it work and launch a new one, rather than retrying calls on the dead object.
Cause 5: popups and tabs that close themselves
A page can close its own tab (window.close() in a popup, an OAuth window that closes after login). Calls on that page then fail the same way. Listen for the page's close event, or take the popup with const popup = await new Promise((r) => page.once('popup', r)) and stop using it once it closes. We did not run a popup case for this page.
Playwright
Playwright prints one message for all of these:
page.screenshot: Target page, context or browser has been closed
page.evaluate: Target page, context or browser has been closed
We got it for a screenshot running while page.close() ran, for evaluate after close, and for evaluate during browser.close(). The fixes are the same: await everything, close last, and replace forEach(async …) with Promise.all.
Checklist
- Read the method in brackets: it names the call that was cut off.
- Search the script for
close()and check that nothing is still running when each one is reached — missingawait,forEach(async, timers, event handlers. - No close in sight? Log the browser's
disconnectedevent and the exit code of the Chrome process; check the container's memory and/dev/shm. - Give every call a timeout, so a dead target fails fast instead of hanging a worker. A navigation that simply never finishes is a different error: navigation timeout of 30000 ms exceeded.
When the job is just a screenshot
A screenshot API takes the browser's lifetime off your hands: our screenshot API runs the browsers on its own servers, and returns status: failed with a reason when a page cannot be captured, with the quota returned. A viewport screenshot is 1 render of the quota (pricing). It takes public URLs only; for logged-in flows keep your own Puppeteer and the fixes above.