question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] unhandledRejection (Target.attachToTarget)

See original GitHub issue

Describe the bug

Hey there, reproducing my code snippet will lead to an unhandledRejection.

It basically opens a Browser instance and closes it before creating a new page, therefore, it fails as expected. The problem is that the error is not correctly catched.

If I reproduce the same code snippet commenting puppeteer.use(pluginStealth()), the error will be correctly catched so I guess the problem comes from puppeteer-extra rather than from puppeteer itself.

Code Snippet

const puppeteer = require('puppeteer-extra')
const pluginStealth = require('puppeteer-extra-plugin-stealth')

puppeteer.use(pluginStealth())

puppeteer.launch({
	headless: false,
	args: [
		'--window-position=0,0',
		'--disable-infobars',
		'--disable-extensions',
		'--disable-accelerated-2d-canvas',
		'--disable-gpu',
		'--window-size=1920x1080',
		'--disable-dev-shm-usage',
		'--disable-setuid-sandbox',
		'--no-first-run',
		'--no-sandbox',
		'--no-zygote',
		'--single-process',
		'--deterministic-fetch',
		'--enable-features=NetworkService',
		'--ignore-certificate-errors'
	]
})
.then((browser) => {
    browser.close()

	return browser.newPage()
})
.catch((err) => {
    console.log(err)
})

What is happening

(node:18424) [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.
(node:18424) UnhandledPromiseRejectionWarning: Error: Protocol error (Target.attachToTarget): No target with given id found
    at C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:71:63
    at new Promise (<anonymous>)
    at Connection.send (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:70:16)
    at Connection.createSession (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:141:42)
    at Target._sessionFactory (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Browser.js:178:91)
    at Target.page (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Target.js:72:38)
    at Plugin._onTargetCreated (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer-extra-plugin\dist\index.cjs.js:489:39)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:18424) 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(). (rejection id: 2)

What should happen

Error: Protocol error (Target.attachToTarget): No target with given id found
    at C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:71:63
    at new Promise (<anonymous>)
    at Connection.send (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:70:16)
    at Connection.createSession (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:141:42)
    at Target._sessionFactory (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Browser.js:178:91)
    at Target.page (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer\lib\cjs\puppeteer\common\Target.js:72:38)
    at Plugin._onTargetCreated (C:\Users\xxxx\Desktop\lol\node_modules\puppeteer-extra-plugin\dist\index.cjs.js:489:39)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  message: 'Protocol error (Target.attachToTarget): No target with given id found'
}

Versions

System: OS: Windows 10 10.0.19042 CPU: (6) x64 Intel® Core™ i5-8400 CPU @ 2.80GHz Memory: 4.24 GB / 7.92 GB Binaries: Node: 12.13.1 - C:\Program Files\nodejs\node.EXE npm: 6.12.1 - C:\Program Files\nodejs\npm.CMD

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
wintrdotcomcommented, Apr 7, 2021

Hey again, thanks for this awesome answers, I already tested a similar solution using process.on(“unhandledRejection”… previously

It seems that it is catching some of the unhandledRejections but still throws a node error sometimes, I’m going to give it another shot using @clouedoc solution.

I’m running a 5m+ various headless requests a day so I have enough data to sort this out quickly, I’ll give you an update on this in a couple of days.

Thanks again!

1reaction
berstendcommented, Sep 21, 2021

This should now be fixed through #555:

Successfully published:
 - puppeteer-extra-plugin-adblocker@2.12.0
 - puppeteer-extra-plugin-anonymize-ua@2.3.0
 - puppeteer-extra-plugin-block-resources@2.3.0
 - puppeteer-extra-plugin-click-and-wait@2.3.0
 - puppeteer-extra-plugin-devtools@2.3.0
 - puppeteer-extra-plugin-flash@2.3.0
 - puppeteer-extra-plugin-font-size@2.3.0
 - puppeteer-extra-plugin-recaptcha@3.5.0
 - puppeteer-extra-plugin-repl@2.3.0
 - puppeteer-extra-plugin-stealth@2.8.0
 - puppeteer-extra-plugin-user-data-dir@2.3.0
 - puppeteer-extra-plugin-user-preferences@2.3.0
 - puppeteer-extra-plugin@3.2.0
 - puppeteer-extra@3.2.0
Read more comments on GitHub >

github_iconTop Results From Across the Web

Protocol error (Target.createTarget): Target closed. #1947
Randomly returns Protocol error (Target.createTarget): Target closed. The text was updated successfully, but these errors were encountered: ...
Read more >
[Bug] unhandledRejection (Target.attachToTarget) - Codesti
It basically opens a Browser instance and closes it before creating a new page, therefore, it fails as expected. The problem is that...
Read more >
Puppeteer - Protocol error (Page.navigate): Target closed
The Target closed exception is thrown when you are trying to run a function, but the target (tab) was already closed.
Read more >
1671725 - Perma puppeteer TEST-UNEXPECTED-ERROR Cookie ...
The error stack refers to browsercontext.spec.ts:103:35 where the test starts an async operation to get a CDP target about 30 seconds before we...
Read more >
Error - Target closed - Checkly
Our script can return the sometimes cryptic “Target closed” error. Knowing that the “target” is normally our browser, the context or tab that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found