puppeteer: [Bug]: await browser.pages() hanging when used with remote connection (puppeteer.connect)

Minimal, reproducible example

import puppeteer from 'puppeteer'

;(async()=>{
  const browser = await puppeteer.connect({
    browserURL: 'http://localhost:9222',
    defaultViewport: null,
  })
  const pages = await browser.pages()
  console.log(pages) // STUCK 😭
})()

Error string

no error

Background

Launch chrome with --remote-debugging-port=9222

chrome --remote-debugging-port=9222

On Mac:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222

OTHER PEOPLE ARE EXPERIENCING THIS ISSUE: https://stackoverflow.com/questions/77540484/browser-pages-does-not-resolve-in-puppeteer-script

Expectation

Return the pages array for all browser contexts. However, I also attempted doing it only for browser.defaultBrowserContext(), but it was the same bug in that case.

Reality

Hanging, stuck, no error messages. browser.pages() works when using puppeteer.launch()

Puppeteer version

21.7.0

Node version

21.5.0

Package manager

npm

Package manager version

10.2.5

Operating system

macOS

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 28 (1 by maintainers)

Commits related to this issue

Most upvoted comments

We have seen success in preventing this on alpine 3.20 by adding a --disable-gpu flag into the launch args, e.g.


(async () => {
  const puppeteer = require("puppeteer");
  // Launch the browser and open a new blank page
  const browser = await puppeteer.launch({args:['--no-sandbox']});
  const page = await browser.newPage();
  const version = await page.browser().version();
  console.log("page browser version: " + version);
  await page.goto('https://bbc.co.uk/news');

  console.log("Page title: " + await page.title());

  await browser.close();
})();

Will time out and fail with ProtocolError: Network.enable timed out

Whereas, adding the flag, e.g.


(async () => {
  const puppeteer = require("puppeteer");
  // Launch the browser and open a new blank page
  const browser = await puppeteer.launch({args:['--no-sandbox --disable-gpu']});
  const page = await browser.newPage();
  const version = await page.browser().version();
  console.log("page browser version: " + version);
  await page.goto('https://bbc.co.uk/news');

  console.log("Page title: " + await page.title());

  const element = await page.waitForSelector('h1')
  let value = await element.evaluate(el => el.textContent)
  console.log('h1 = ' + value);

  await browser.close();
})();

Outputs the expected results, we are currently assuming that chromium updated something GPU related in 127+ but are too busy trying test the fix!

We just started seeing this exact error today, but only on EC2 instances in AWS. We have a dockerized API server that’s pulling in some assets from an S3 bucket. I’ve verified that a curl command DOES work, so it’s not a network problem. And I’ve verified that the EC2 instance AND the docker container on that instance can see the stuff on the S3 bucket. This has been working for years, and just started throwing this error and not producing the expected PDF files last night.

We are not using --single-process, so that wasn’t a fix for us. We ARE in the Alpine docker container, but I don’t have a good way to test switching to Debian as this app is very extensive. I see this issue is closed, but we are still experiencing it, but again, not on local development environments, only on the test/stage/prod environments we have in AWS.

Any ideas?? We’ve tried so many things, even going back to the previous sprint release of our own code and database, which DID work as of yesterday, but today, no dice.

Any help is appreciate.

The fix that @CeeBeeUK suggested fixed my issue. At the time of writing this my Dockerfile uses FROM node:21-alpine and the current alpine version is 3.20.

Adding --disable-gpu fixed this. e.g.

    const browser = await puppeteer.launch({
      args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu'],
    });

We have seen success in preventing this on alpine 3.20 by adding a --disable-gpu flag into the launch args, e.g.

Thanks for mentioning this workaround. While locally and with kvm there were no problems with chrome 127.x (Ubuntu based), k8s pods did show this error (debian based image) - ā€œdisable-gpuā€ did help here too.

related issue from alpine aports

I am able to reliably reproduce the error from original issue using a docker image & pdf rendering on an Alpine 3.20, but not on debian.

Here’s my Dockerfile:

FROM alpine:3.20

WORKDIR /root

RUN apk add chromium npm

RUN npm install puppeteer

COPY <<EOF /root/input.html
<p>Hello, world!</p>
EOF

COPY <<EOF /root/script.js
const puppeteer = require('puppeteer')
 
async function printPDF() {
  const browser = await puppeteer.launch({
    headless: true,
    args: [
      "--no-sandbox",
      "--single-process",
      "--no-zygote",
      "--disable-gpu"
    ],
  });
  const page = await browser.newPage();
  await page.goto('file:///root/input.html', {waitUntil: 'networkidle0'});
  const pdf = await page.pdf({ path: "/root/output.pdf", format: 'A4' });
 
  await browser.close();
  return pdf
}

printPDF()
EOF

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

Then:

sudo docker build --tag docker-chromium-issue .
sudo docker run -it --rm docker-chromium-issue node /root/script.js

The docker run command will hang for 2-3 minutes, then finish render an exception:

/root/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:93
    #error = new Errors_js_1.ProtocolError();
             ^

ProtocolError: Network.enable timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed.
    at <instance_members_initializer> (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:93:14)
    at new Callback (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:97:16)
    at CallbackRegistry.create (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:22:26)
    at Connection._rawSend (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Connection.js:89:26)
    at CdpCDPSession.send (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/CDPSession.js:66:33)
    at NetworkManager.addClient (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/NetworkManager.js:62:20)
    at FrameManager.initialize (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/FrameManager.js:170:38)
    at #initialize (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Page.js:329:36)
    at CdpPage._create (/root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Page.js:95:31)
    at /root/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Target.js:206:42

Node.js v21.7.3

While the command is running, I observe unusually high CPU usage by chromium process.

Updating the Dockerfile to be debian-based fixes the issue:

diff --git a/Dockerfile b/Dockerfile
index f025886..f706b48 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,8 +1,8 @@
-FROM alpine:3.20
+FROM debian:12

 WORKDIR /root

-RUN apk add chromium npm
+RUN apt update; apt install chromium npm --yes

 RUN npm install puppeteer