puppeteer: Protocol error (Target.createTarget): Target closed.

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 0.11.0 (we have it on our roadmap to upgrade to 1.0.0)
  • Platform / OS version: Ubuntu 16
  • Node.js version: 6.11.3

What steps will reproduce the problem? Very random and rare. Out of over 2500 requests, we have 20 of these failures.

Please include code that reproduces the issue.

const args = ['--no-sandbox', '--disable-setuid-sandbox'];
return puppeteer.launch({ args }).then(browser =>
  browser
    .newPage()
    .then(page => page.setContent(html).then(() => page))
    .then(page => page.pdf(pdfParams).then(() => page))
    .then(page => page.setViewport(viewport).then(() => page))
    .then(page => page.screenshot(screenshotParams))
    .then(() => browser.close())
    .catch(e => {
      if (browser) {
        browser.close();
      }
      return Promise.reject(e);
    })
	.then(() => { stuff });
);

html is just a bunch of ejs rendered html.

What is the expected result? No protocol error

What happens instead? Randomly returns Protocol error (Target.createTarget): Target closed.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 9
  • Comments: 60

Commits related to this issue

Most upvoted comments

I’ve also encountered this error.

Not sure if this helps anyone, but I had the issue while running it with aws lambdas. For me it solved the issue that I made sure I closed the chrome properly via await browser.close(); before finishing the lambda. I think otherwise the browser doesn’t cleanup properly and the next session might stumble over some leftovers.

This would definitely happen if you were calling page.close without awaiting it, and then later calling browser.close().

It is unlikely that these problems are related. It would be very helpful if someone could send a script I can run that reproduces this problem.

I am still facing this, even with browser.close.

Can you post the stack trace along with the error? The only usage of Target.createTarget in Puppeteer is in browser.newPage(). This error would happen if you call browser.close() while browser.newPage() has yet to resolve.

I had a similar issue. Sorted by using the following to launch.

const browser = await puppeteer.launch({
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--single-process'
    ]
  });

From my experiments it seems this is caused by the same glibc bug as #2207 (https://sourceware.org/bugzilla/show_bug.cgi?id=19329). Launching chrome a lot of times triggers a race condition in glibc which crashes chrome. When pipe mode is used the crashing chrome triggers the error in this ticket. When websockets are used the error in #2207 is triggered.

I’m having this exact issue on an Apple Silicon machine running a docker image.

(node:9) UnhandledPromiseRejectionWarning: Error: Protocol error (Target.setDiscoverTargets): Target closed.
    at /usr/src/app/node_modules/puppeteer/lib/Connection.js:52:63
    at new Promise (<anonymous>)
    at Connection.send (/usr/src/app/node_modules/puppeteer/lib/Connection.js:51:16)
    at Function.create (/usr/src/app/node_modules/puppeteer/lib/Browser.js:45:26)
    at ChromeLauncher.launch (/usr/src/app/node_modules/puppeteer/lib/Launcher.js:186:53)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async /usr/src/app/dist/index.js:103:21

When deployed to Google Cloud, it works fine. It seems to only be on Apple Silicon. Any ideas?

I’m seeing the same issue as @Joebayld on an M1 macbook. Works fine anywhere else.

This may happens if you use --single-process arg.

Has anyone been able to get browser.newPage() to work on M1 Mac within Docker?

I’m also seeing this error:

ProtocolError: Protocol error (Target.createTarget): Target closed.
    at /home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:75:24
    at new Promise (<anonymous>)
    at Connection.send (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71:16)
    at Browser._createPageInContext (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:261:53)
    at BrowserContext.newPage (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:520:30)
    at Browser.newPage (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:254:37)
    at /home/node/app/src/test.js:12:36
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

This may happens if you use --single-process arg.

I was having this same problem running puppeteer inside a docker container under my Macbook M1 and actually adding this arg make the puppeteer works fine

Also experiencing this error on M1 Macbook. Is this issue stale?

I am getting the same error. Any solution with M1 Macs

docker buildx build --push --platform linux/amd64,aarch64 --tag your.docker.image.repo/your-image:tag .

This solved our problem with development on PC and M1 mac.

I was able to fix this issue on my tests by calling await page.close(); in all tests so that before afterAll(()=>browser.close()); runs, all of the pages should be closed. Thanks to @JoelEinbinder for the fix.

We’ve got the same issue on M1 Macbook, full error is:

Full error: Error: Protocol error (Target.createTarget): Target closed.
closing child process. 140

For anyone else having this problem, this occurred for me consistently whenever I was trying to await page.goto() within a foreach() or map() loop. When I switched to using a for() loop instead it worked fine. I haven’t debugged it to understand why but it’s the only difference that worked for me. Hope this helps someone

i got the same. I read some people had more luck going to v16, so i did but now i get other errors:

Sep 10 14:53:50Z sr-bot app/web.1 [Nest] 20 - 09/10/2023, 2:53:49 PM ERROR [ExceptionHandler] Failed to launch the browser process! Sep 10 14:53:50Z sr-bot app/web.1 [0910/145349.802510:FATAL:zygote_host_impl_linux.cc(117)] No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/main/docs/linux/suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using --no-sandbox.

Frustrating that it works locally but not on heroku 😦

Hello,

for me I had to close the extensions before the browser like this.

      let pages = await browser.pages();
      pages = pages.filter((page) => page.url().includes("chrome-extension"));
      for (let page of pages) {
        await page.close();
      }
      await browser.close();

For me, the M1 problem was that my container’s image only supports AMD64 (x86_64). I’ve rebuilt my container’s base image on my M1, and the problem is solved.

To anyone encountering the issue on M1, you can run uname -m inside your container. If you get x86_64, you will need to ensure your image supports arm64. So when you run uname -m it expects to print aarch64.

Hi @JonathanGuo

Can you tell me what is the image you have used?

Any help will be appreciated 😉

The image I built is based on alpine. What I’ve done was just rebuild the image on an arm64 platform. Since the image I built is the base image that needs to support multiple platforms, I have added and pushed the manifest. You can use buildx to simply it, but the performance to me was too slow. I ended with having it built on AWS code pipeline with 2 code build projects build two platforms in parallel, and another code build project that creates the manifest and push to registry.

Hope this is helpful.

@marcelpanse i opened #7916, I’m having the same problem.

Same problem on M1 mac

same problem here on M1 mac