berstend / puppeteer-extra

💯 Teach puppeteer new tricks through plugins.
https://extra.community
MIT License
6.23k stars 731 forks source link

[Bug] cdpSession.send: Target page, context or browser has been closed #858

Open alucarded opened 6 months ago

alucarded commented 6 months ago

Describe the bug

page.goto: Timeout 10000ms exceeded.
Call log:
  - navigating to "[SOME_URL]", waiting until "commit"

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

cdpSession.send: Target page, context or browser has been closed
    at Object.send ([project_dir]/node_modules/playwright-extra/dist/index.cjs.js:194:46)

I was doing load test of a web server using playwright with playwright-extra to render URLs (once per request). Load test was done with multiple threads hitting an endpoint. Every call to an endpoint spawns a separate chromium instance with playwright. This only started to happen when increasing number of threads to the point where rendering a page with playwright was timing out.

This is critical issue when using playwright in a web service since it crashes it.

Code Snippet

I will create a pull request that fixes the issue.

Versions

System: OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish) CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900K Memory: 30.08 GB / 31.23 GB Container: Yes Shell: 5.1.16 - /bin/bash Binaries: Node: 18.17.1 - /usr/bin/node npm: 10.2.4 - /usr/bin/npm npmPackages: playwright: ^1.39.0 => 1.40.0 playwright-extra: ^4.3.6 => 4.3.6 puppeteer-extra-plugin-stealth: ^2.11.2 => 2.11.2

netbroom commented 4 months ago

This is also impacting default puppeteer/chromium. Getting these errors with the Stealth plugin when using multithreading or scraping multiple websites at once.

it's not tracking when pages and targets are closed properly and not catching all errors when sending data through a CDP session.

When using one browser connecting to multiple pages in different contexts, the stealth plugin crashes fairly often.

Here is one such error, but there are several throughout the code for both Stealth and puppeteer-extra.

Error [ProtocolError]: Protocol error (Network.setUserAgentOverride): Target closed.
    at /opt/scan/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:230:24
    at new Promise (<anonymous>)
    at CDPSession.send (/opt/scan/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:226:16)
    at next (/opt/scan/node_modules/puppeteer-extra-plugin-stealth/evasions/sourceurl/index.js:34:41)
    at CDPSession.send (/opt/scan/node_modules/puppeteer-extra-plugin-stealth/evasions/sourceurl/index.js:67:18)
    at Plugin.onPageCreated (/opt/scan/node_modules/puppeteer-extra-plugin-stealth/evasions/user-agent-override/index.js:182:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Plugin._onTargetCreated (/opt/scan/node_modules/puppeteer-extra-plugin/dist/index.cjs.js:496:21) {
  originalMessage: ''
}