[Bug] unhandledRejection (Target.attachToTarget)
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:19 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
This should now be fixed through #555: