# "Protocol error: Target closed" in Puppeteer: what closed, and how to stop it

**In short:** Target closed means Puppeteer sent a command to a tab or browser that no longer exists: you closed the page or browser before an unawaited call finished, an async forEach let browser.close run first, or Chrome itself died, usually out of memory. Await every call, use Promise.all with map instead of forEach, close the browser last, and listen for the disconnected event.

> TargetCloseError, "Session closed" and "Failed to open a new tab" reproduced with Puppeteer 25.7: what closed the target, and the fix for each cause.

Published 2026-11-21 · https://shareglint.com/en/guides/puppeteer-target-closed

"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

```javascript
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:

```javascript
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:

```javascript
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](https://shareglint.com/en/guides/speed-up-puppeteer-screenshots) measures how throughput changes with the number of tabs.

## Cause 3: using a page or browser after closing it

```javascript
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](https://docs.apify.com/academy/node-js/how_to_fix_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:

```javascript
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](https://pptr.dev/api/puppeteer.browser) 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/shm` in `node:24-slim` on 2026-11-21: `64M`), and Chrome renders through it, so large pages can crash the tab. Launch with `args: ['--disable-dev-shm-usage']` or run the container with `--shm-size=1gb`. [Running Puppeteer in Docker](https://shareglint.com/en/guides/run-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:

```text
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

1. Read the method in brackets: it names the call that was cut off.
2. Search the script for `close()` and check that nothing is still running when each one is reached — missing `await`, `forEach(async`, timers, event handlers.
3. No close in sight? Log the browser's `disconnected` event and the exit code of the Chrome process; check the container's memory and `/dev/shm`.
4. 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](https://shareglint.com/en/guides/puppeteer-navigation-timeout).

## When the job is just a screenshot

A screenshot API takes the browser's lifetime off your hands: our [screenshot API](https://shareglint.com/en/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](https://shareglint.com/en/pricing)). It takes public URLs only; for logged-in flows keep your own Puppeteer and the fixes above.

## FAQ

### What does "Protocol error (Runtime.callFunctionOn): Target closed" mean?

Puppeteer talks to Chrome over the DevTools protocol, one session per tab. The method in brackets (Runtime.callFunctionOn for evaluate, Page.captureScreenshot for screenshot) was still waiting for an answer when the tab or the whole browser closed. Something closed it first: your own close call, a crash, or the process being killed.

### What is the difference between Target closed and Session closed?

In Puppeteer 25.7, Target closed was the error for a command that was already running when the tab or browser went away. "Session closed. Most likely the page has been closed" was the error for a new command sent after Chrome had died. "Attempted to use detached Frame" appeared when we used a page object after page.close().

### Why do I get Target closed when I use forEach with async?

forEach does not wait for async callbacks, so the line after it, usually browser.close(), runs while the jobs are starting. In 5 runs of our test, 4 left all three jobs hanging forever with no error and 1 failed each with "Protocol error (Target.createTarget): Failed to open a new tab". await Promise.all(urls.map(async ...)) fixed it.

### Can Chrome running out of memory cause Target closed?

Yes. When we killed the Chrome process during an evaluate, the call failed with TargetCloseError, the browser's disconnected event fired, and the next screenshot failed with Session closed. An out-of-memory kill in a small container or a serverless function ends the process the same way. In Docker, also check /dev/shm: it is 64 MB by default, and --disable-dev-shm-usage or a larger --shm-size is the usual fix.

### Does Playwright have the same error?

Playwright's wording is "Target page, context or browser has been closed", and the causes are the same. We got it for a screenshot running while page.close() ran, for evaluate after close, and for evaluate during browser.close().
