Alex Blex
Alex Blex

Reputation: 37018

Puppeteer Error: Protocol error (Page.captureScreenshot): Target closed

I have this error running [email protected] on node:8-slim container.

The full error:

Error: Protocol error (Page.captureScreenshot): Target closed.
    at Promise (/app/node_modules/puppeteer/lib/Connection.js:183:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/app/node_modules/puppeteer/lib/Connection.js:182:12)
    at Page._screenshotTask (/app/node_modules/puppeteer/lib/Page.js:903:39)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
  -- ASYNC --
    at Page.<anonymous> (/app/node_modules/puppeteer/lib/helper.js:108:27)
    at /app/test.js:9:15
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

The js file (inspired by GoogleChrome/puppeteer/examples/screenshot.js):

const puppeteer = require('puppeteer');
(async() => {
         const browser = await puppeteer.launch({
                headless: true,
                args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
         });
          const page = await browser.newPage();
          await page.goto('http://google.com');
          await page.screenshot({path: 'example.png'});
          await browser.close();
})();

The Dockerfile (inspired by troubleshooting.md#running-puppeteer-in-docker):


FROM node:8-slim

RUN apt-get update && apt-get install -yq libgconf-2-4

RUN apt-get update && apt-get install -y wget --no-install-recommends \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get purge --auto-remove -y curl \
    && rm -rf /src/*.deb

WORKDIR /app
RUN chown node: /app

user node

COPY ./ ./
RUN npm i --unsafe-perm=true

CMD ["node", "test.js"]

The same test.js makes high quality screenshots on the host OS, but fails in a container.

Is there any magic parameter to run it in node-slim container? I am happy with any version of puppeteer that works.

Version of google-chrome-unstable installed from Dockerfile is "73.0.3683.20 dev". Version of chrome installed by npm is "73.0.3679.0".

UPDATE:

I have tried to add await page.close() as Cody G. suggested:

const puppeteer = require('puppeteer');
(async() => {
         const browser = await puppeteer.launch({
                headless: true,
                args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
         });
          const page = await browser.newPage();
          await page.goto('http://google.com');
          await page.screenshot({path: 'example.png'});
          await page.close();
          await browser.close();
})();

It didn't make much difference. The error is still thrown at line 9 await page.screenshot so I guess it didn't reach the added line.

UPDATE 2:

I have added event loggers like this:

const puppeteer = require('puppeteer');
(async() => {
        try{
         const browser = await puppeteer.launch({
                headless: true,
                args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
         });
          const page = await browser.newPage();
          const eventHandler = e=>(...args)=>console.log({e,args});
          for(let e of [
                  'close',
                  'console',
                  'dialog',
                  'domcontentloaded',
                  'error',
                  'frameattached',
                  'framedetached',
                  'framenavigated',
                  'load',
                  'metrics',
                  'pageerror',
                  'popup',
                  'request',
                  'requestfailed',
                  'requestfinished',
                  'response',
                  'workercreated',
                  'workerdestroyed'
          ]) {
                page.on(e, eventHandler(e));
          };
          await page.goto('http://google.com');
          await page.screenshot({path: 'example.png'});
          await page.close();
          await browser.close();
        } catch(e){
                console.error({e});
        }
})();

The full log is quite long and is available at https://gist.github.com/blex18/10ae4eed389ca818d72218baad3dfad2.

Essentially, after few redirects it landed at https://www.google.com/?gws_rd=ssl followed by resource and xhr requests.

UPDATE 3:

The package.json:

{
  "name": "pup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "puppeteer": "^1.12.2"
  }
}

Docker info 1 (doesn't work):

Containers: 19
 Running: 3
 Paused: 0
 Stopped: 16
Images: 118
Server Version: 18.09.1
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 205
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 96ec2177ae841256168fcf76954f7177af9446eb
init version: fec3683
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.15.0-45-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.5GiB
Name: u4
ID: 3ZO4:OYYQ:K2X6:5QDB:ZNYO:FYNG:6YKT:HMDT:W3LX:UOY3:IEVU:3FCY
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Username: alex
Registry: https://index.docker.io/v1/
Labels:
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

WARNING: No swap limit support

Docker info 2 (works like a charm)

Containers: 15
 Running: 6
 Paused: 0
 Stopped: 9
Images: 56
Server Version: 18.09.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 96ec2177ae841256168fcf76954f7177af9446eb
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.125-linuxkit
Operating System: Docker Desktop
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.952GiB
Name: docker-desktop
ID: E3X5:BZUG:4QYZ:Z6EA:TBT4:Y36Z:ZQ5Y:ACZS:QAS2:7I6M:LGN7:J3GH
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 73
 Goroutines: 82
 System Time: 2019-02-13T10:04:59.482848Z
 EventsListeners: 2
HTTP Proxy: gateway.docker.internal:3128
HTTPS Proxy: gateway.docker.internal:3129
Registry: https://index.docker.io/v1/
Labels:
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

Upvotes: 18

Views: 12672

Answers (3)

Gaurav Seth
Gaurav Seth

Reputation: 205

I am able to replicate this issue.

Here is my code

browser = await puppeteer.launch({
      defaultViewport:{width:1920,height:6000},
      headless: true,
      executablePath: await chromium.executablePath,
      args: chromium.args,
      ignoreDefaultArgs: ["--hide-scrollbars"],
    });

let siteName = urlArray[urlIndex].split('://')[1];
      const page = await browser.newPage();
      await page.goto(urlArray[urlIndex], {
        waitUntil: ['domcontentloaded', 'networkidle0', 'networkidle2'],
      });
      
      const buffer = await page.screenshot({
          clip: { x: 0, y: 0, width:1920, height: 6000}
      });
      result = await page.title();

Error

START RequestId: 51ec6b89-6c49-408d-95e1-f09492e1ecee Version: $LATEST 2021-06-04T14:12:05.738Z 51ec6b89-6c49-408d-95e1-f09492e1ecee ERROR Error: Navigation failed because browser has disconnected! at CDPSession.LifecycleWatcher._eventListeners.helper.addEventListener (/opt/nodejs/node_modules/puppeteer-core/lib/LifecycleWatcher.js:46:107) at CDPSession.emit (events.js:198:13)

OR

Error: Protocol error (Page.captureScreenshot): Target closed

Solution I was facing issue because of the screenshot height and width. My page is too long so I need to increase height of screenshot to 6000 which caused browser disconnection or protocol error. Moreover, I was using the same browser object to take screenshot of 4 websites which was causing the above issue.

So, I re-initialized the browser object before taking the screenshot like this:

for (let urlIndex = 0; urlIndex < urlArray.length; urlIndex++) {
    try {
      browser = await puppeteer.launch({
        defaultViewport:{width:1920,height:6000},
        headless: true,
        executablePath: await chromium.executablePath,
        args: chromium.args,
        ignoreDefaultArgs: ["--hide-scrollbars"],
      });

It resolved my issue.

Upvotes: 1

Jan Garaj
Jan Garaj

Reputation: 28626

Your issue is not reproducible. It works on my Centos 7, your Mac environment, so it seems to be a problem with the Docker configuration and not with the code. Possible problems:

  1. Used docker storage driver: aufs can be a problem, especially for older kernels and write/delete operations in the container; if it is possible try default overlay2
  2. Used security profiles: apparmor and seccomp are enabled, you may try to run with --security-opt seccomp=unconfined and check dmesg, maybe security is blocking something

Upvotes: 3

user10960419
user10960419

Reputation:

I can provide you with a solution

    FROM node:8-slim

# See https://crbug.com/795759
RUN apt-get update && apt-get install -yq libgconf-2-4

# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
RUN apt-get update && apt-get install -y wget --no-install-recommends \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get purge --auto-remove -y curl \
    && rm -rf /src/*.deb

# It's a good idea to use dumb-init to help prevent zombie chrome processes.
ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64 /usr/local/bin/dumb-init
RUN chmod +x /usr/local/bin/dumb-init

# Uncomment to skip the chromium download when installing puppeteer. If you do,
# you'll need to launch puppeteer with:
#     browser.launch({executablePath: 'google-chrome-unstable'})
# ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# Install puppeteer so it's available in the container.
RUN npm i puppeteer

# Add user so we don't need --no-sandbox.
RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
    && mkdir -p /home/pptruser/Downloads \
    && chown -R pptruser:pptruser /home/pptruser \
    && chown -R pptruser:pptruser /node_modules

# Run everything after as non-privileged user.
USER pptruser

ENTRYPOINT ["dumb-init", "--"]
CMD ["google-chrome-unstable"]

Here is the best mirror I have found so far for google-chrome-stable for RPM-based systems.

http://orion.lcg.ufrj.br/RPMS/myrpms/google/

Upvotes: 3

Related Questions