Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol error (Target.createTarget): Target closed. #1947

Closed
pbarbiero opened this issue Feb 1, 2018 · 60 comments
Closed

Protocol error (Target.createTarget): Target closed. #1947

pbarbiero opened this issue Feb 1, 2018 · 60 comments

Comments

@pbarbiero
Copy link

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.

@GregRos
Copy link

GregRos commented Feb 5, 2018

I've also encountered this error.

@pbarbiero
Copy link
Author

@GregRos which version of puppeteer are you using that you also see this in?

@pbarbiero
Copy link
Author

We are now up to 48 of these over ~6000 requests.

@JoelEinbinder
Copy link
Collaborator

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.

@pbarbiero
Copy link
Author

Error: Protocol error (Target.createTarget): Target closed.
  at Connection._onClose (/.../node_modules/puppeteer/node6/Connection.js:165:23)
  at emitTwo (events.js:106:13)
  at WebSocket.emit (events.js:191:7)
  at WebSocket.emitClose (/.../node_modules/ws/lib/WebSocket.js:213:10)
  at _receiver.cleanup (/.../node_modules/ws/lib/WebSocket.js:195:41)
  at Receiver.cleanup (/.../node_modules/ws/lib/Receiver.js:520:15)
  at WebSocket.finalize (/.../node_modules/ws/lib/WebSocket.js:195:22)
  at emitNone (events.js:91:20)
  at Socket.emit (events.js:185:7)
  at endReadableNT (_stream_readable.js:974:12)
  at _combinedTickCallback (internal/process/next_tick.js:80:11)
  at process._tickCallback (internal/process/next_tick.js:104:9)

@pbarbiero
Copy link
Author

Anything else I can provide to help you trace this? We've had another 21 failures in the the last 7 days alone over an additional ~2000 requests. The error rate seems variable.

The code is as you see, we don't interact with puppeteer anywhere else on this server, so I don't see how we could be calling browser.close() while its processing as its only explicitly called if we get an error and the browser is still running.

@pbarbiero
Copy link
Author

Over the last 60 days, out of ~17,000 requests exactly 376 have triggered this failure scenario, about a 2% failure rate.

Still hoping for some insight into if this is a puppeteer issue or something on our end.

@mahnunchik
Copy link

Faced with Error: Protocol error (Target.closeTarget): Target closed. too 😭

@mxab
Copy link

mxab commented May 8, 2018

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.

@roberthluo
Copy link

roberthluo commented Jul 2, 2018

Did anyone figure out a work around? I cannot use await browser.close(); I am generating new tabs to prevent the browser constantly opening and closing. Here is my stack trace below:

[2018-07-02T18:56:25.946] [DEBUG] default - { Error: Protocol error (Target.closeTarget): Target closed.

    at Promise (/app/node_modules/puppeteer/lib/Connection.js:86:56)
    at new Promise (<anonymous>)
    at Connection.send (/app/node_modules/puppeteer/lib/Connection.js:85:12)
    at Page.close (/app/node_modules/puppeteer/lib/Page.js:888:38)
    at html_to_pdf (/app/convert_to_pdf.js:50:16)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
  message: 'Protocol error (Target.closeTarget): Target closed.' }

@JoelEinbinder
Copy link
Collaborator

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.

@Jmille65
Copy link

@mxab thank you, I was also able to fix this error (running on an AWS Lambda) by adding a call await browser.close().

@1600
Copy link

1600 commented Jan 24, 2019

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.

unfortunately await all page.close() didn't eradicate this problem or even help it in my case.

@vongohren
Copy link

This is very dependent on what environment you run in.
I had this problem running in an alpine docker ndoe 10.15.1.
That was because alpine doesnt support that high chromium version as default suggests.
So this comment on a issue sovled it for me #1793 (comment)

@ftomassetti
Copy link

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

@imhashir
Copy link

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

This was exactly my problem on Lambdas. Thanks a lot @JoelEinbinder

@JorisVanEijden
Copy link

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.

dbjorge added a commit to microsoft/accessibility-insights-web that referenced this issue Oct 27, 2020
…3540)

#### Description of changes

This was originally motivated by wanting to audit all the `browser.close` instances to see if we were ever closing a browser while there might still be an unawaited page.close in progress, which per [this puppeteer issue](puppeteer/puppeteer#1947) might explain [failures like what we're seeing in the e2e windows jobs here](https://dev.azure.com/accessibility-insights/accessibility-insights-web/_build/results?buildId=15197&view=results).

I didn't find anything to suggest that that's an accurate root cause for our issue, but I thought this update made stuff more readable regardless.

#### Pull request checklist
<!-- If a checklist item is not applicable to this change, write "n/a" in the checkbox -->
- [n/a] Addresses an existing issue: #0000
- [x] Ran `yarn fastpass`
- [n/a] Added/updated relevant unit test(s) (and ran `yarn test`)
- [n/a] Verified code coverage for the changes made. Check coverage report at: `<rootDir>/test-results/unit/coverage`
- [x] PR title *AND* final merge commit title both start with a semantic tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See `CONTRIBUTING.md`.
- [n/a] (UI changes only) Added screenshots/GIFs to description above
- [n/a] (UI changes only) Verified usability with NVDA/JAWS
@WasabiThumb
Copy link

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.

Yep, only getting this in pipe mode

@oskery
Copy link

oskery commented Jan 1, 2021

What worked for me, as the number of requests grew in size, was to split the requests into different functions and executing those functions asynchronously:

const scrape1 = async () => {...}
const scrape2 = async () => {...}

const run = async () => {
  await scrape1()
  await scrape2()
}

run()

@misantronic
Copy link

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'
    ]
  });

I also tried this - I am using chrome-aws-lambda. Didn’t work that way.

@daverickdunn
Copy link

This will happen if Chromium doesn't have enough memory on startup.

I was running puppeteer-core with chrome-aws-lambda on Lambda (as opposed to Google Cloud Functions...) using 256MB of RAM, I upped the memory to 1024MB and that fixed it for me.

You might get away with allocating less than 1024MB, but I haven't tried.

@misantronic
Copy link

This will happen if Chromium doesn't have enough memory on startup.

I was running puppeteer-core with chrome-aws-lambda on Lambda (as opposed to Google Cloud Functions...) using 256MB of RAM, I upped the memory to 1024MB and that fixed it for me.

You might get away with allocating less than 1024MB, but I haven't tried.

That's interesting! I am running all my pdf-lambda functions on 3008 MB ever since, so that doesn‘t work for me.

@z9fr
Copy link

z9fr commented Oct 18, 2021

/projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:217
            this._callbacks.set(id, { resolve, reject, error: new Error(), method });
                                                              ^

Error: Protocol error (Page.navigate): Target closed.
    at /projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:217:63
    at new Promise (<anonymous>)
    at CDPSession.send (/projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:216:16)
    at navigate (/projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:109:47)
    at FrameManager.navigateFrame (/projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:92:13)
    at Frame.goto (/projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:417:41)
    at Page.goto (/projects/moodle-bot/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:1156:53)
    at run (/projects/moodle-bot/index.js:17:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I'm still having the same issue. its surely not an issue with the code I tried to run the same code on another machine it worked fine I'm running 5.13.19-2-MANJARO and every single time I run puppeteer it breaks like this, anyone found a fix for this ? I'm so annoyed by this error tbh :(

@durasj
Copy link

durasj commented Nov 9, 2021

@z9fr it's possible you are facing #7661 since you are on Linux and Manjaro was likely already running systemd 249 on Oct 18th.

@KhaoulaSoussi

This comment has been minimized.

@z9fr
Copy link

z9fr commented Nov 30, 2021

oh thanks ill look in to it i manage to fix the issue by mentioning chrome path and using puppeteer-lite. I will this again!

@abdullah-kasim
Copy link

For those that are on heroku and ruby/rails, make sure to disable jemalloc. I'm putting it here for those that's coming in from google.

I've been hit by this and has dug so deep until I saw that another developer decided to just test out all his buildpacks:

Ref: Studiosity/grover#80

@davidbielik
Copy link

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)

@fro-oo
Copy link

fro-oo commented Jan 10, 2022

Same pb here @davidbielik

(node:731) UnhandledPromiseRejectionWarning: Error: Protocol error (Target.createTarget): Target closed.
    at /realx/node_modules/puppeteer/lib/Connection.js:74:56
    at new Promise (<anonymous>)
    at Connection.send (/realx/node_modules/puppeteer/lib/Connection.js:73:12)
    at Browser._createPageInContext (/realx/node_modules/puppeteer/lib/Browser.js:174:47)
    at BrowserContext.newPage (/realx/node_modules/puppeteer/lib/Browser.js:367:26)
    at Browser.newPage (/realx/node_modules/puppeteer/lib/Browser.js:166:33)
    at Browser.<anonymous> (/realx/node_modules/puppeteer/lib/helper.js:112:23)
    at main (/realx/src/index.js:224:48)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:731) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:731) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@KevinVR
Copy link

KevinVR commented Jan 15, 2022

I'm getting the same issues on M1 Mac, same code works fine on Windows 10

Protocol error (Page.navigate): Target closed.   

(and tail of stack trace)

#39 0x00400540005e (/tmp/chromium+0x540005d)
#40 0x0040053ccc78 (/tmp/chromium+0x53ccc77)
#41 0x0040053d8bcd (/tmp/chromium+0x53d8bcc)
#42 0x0040053d88cd (/tmp/chromium+0x53d88cc)
#43 0x0040054008ac (/tmp/chromium+0x54008ab)
#44 0x00400b09740b start_thread
#45 0x00400bceb40f __GI___clone
  r8: 0000004015991164  r9: 000000400bd29148 r10: 0000004015991164 r11: 000000400bd759d0
 r12: 0000004015991220 r13: 0000000000000000 r14: 00002b1600f609d0 r15: 0000000000000000
  di: 0000004015991220  si: 0000004015991220  bp: 000000000000013a  bx: 00002b1600ed2800
  dx: 000000000000013a  ax: 587b37616d930d31  cx: 000000000001a850  sp: 00000040159911b0
  ip: 000000401f60e67f efl: 0000000000000202 cgf: 002b000000000033 erf: 0000000000000004
 trp: ffffffffffffffff msk: 0000000000000000 cr2: 007b37616d930d39
[end of stack trace]
qemu: uncaught target signal 11 (Segmentation fault) - core dumped

This seems related to docker/for-mac#5123

@marcelpanse
Copy link

Same problem with browser.newPage() running docker on an M1 machine.

@davidbielik
Copy link

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

@FridayCandour
Copy link

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

thanks man you saved me

@vicary
Copy link

vicary commented Aug 4, 2022

I am on Apple M1, closing every single page before closing the browser worked for me. See #1947 (comment).

@pengxiao1991
Copy link

I am on centos,using 'browser.createIncognitoBrowserContext().newPage();'. i am having the same problem. using 'browser.newPage()' is worked for me.

@JonathanGuo
Copy link

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.

@fro-oo
Copy link

fro-oo commented Nov 10, 2022

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 ;-)

@JonathanGuo
Copy link

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.

@tapiau
Copy link

tapiau commented Nov 25, 2022

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.

@wujekkiryl
Copy link

@tapiau solution worked for me.

@ddaydd
Copy link

ddaydd commented Sep 5, 2023

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();

@aminaodzak
Copy link

Guys, can anyone help here, I'm stuck on running test in persistent context, and problem is when running tests in workers, where we preserve state per worker.

Single test run works fine, but any other test added fails with:

Error: browserContext.newPage: Protocol error (Target.createTarget): Failed to open a new tab

and this is my configuration in fixture:

export function createTestWithExtension(storagePath = 'user') {
	return base.extend<WithExtenstion>({
		browser: async (use) => {
			const browser = await chromium.launch()
			await use(browser)
			await browser.close() // Close the browser when the fixture is finished
		},
		context: async ({ browserName }, use) => {
			const browserTypes = { chromium }
			const launchOptions = {
				headless: false,
				args: [
					
					`--disable-extensions-except=${extensionPath}`,
					'--no-sandbox',
					'--disable-setuid-sandbox',
					'--disable-dev-shm-usage',
					'--disable-accelerated-2d-canvas',
					'--no-first-run',
					'--no-zygote',
					// '--single-process',
					'--disable-gpu',
				],
			}
			const context = await browserTypes[browserName].launchPersistentContext(
				`/tmp/${storagePath}`,
				launchOptions
			)

			const pages = await context.pages()
			const blankPage = pages.find(p => p.url() === 'about:blank')
			await blankPage?.close()
			await use(context)
		},

		page: async ({ context }, use) => {
			const page = await context.newPage()
			await use(page)
		},
		
		users: [
			{
				defaultUserEmail: '',
				defaultUserPwd: '',
				
			},
			{ option: true },
		],
		// Define a fixture. Note that it can use built-in fixture "page"
		onboardingFlow: async ({ page }, use) => {
			await use(new OnboardingFlow(page))
		}
	})



@mmaestrini
Copy link

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 :(

@stephen-ls
Copy link

I still have this issue. In my case the error is thrown by .pdf() function.

ProtocolError: Protocol error (Page.printToPDF): Target closed
    at new Callback (/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:61:35)
    at CallbackRegistry.create (/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:106:26)
    at Connection._rawSend (/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:216:26)
    at CDPSessionImpl.send (/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:419:78)
    at CDPPage.createPDFStream (/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Page.js:741:88)
    at CDPPage.pdf (/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Page.js:767:37)
    at FilesService.generatePdf (/var/task/files/src/files.service.js:222:42)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I described the problem on stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests