Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Protocol error (Target.createTarget)" when running puppeteer inside AWS lambda container #6776

Closed
markb-trustifi opened this issue Jan 22, 2021 · 25 comments

Comments

@markb-trustifi
Copy link

markb-trustifi commented Jan 22, 2021

Steps to reproduce

  • Puppeteer version: 5.5.0
  • Platform / OS version: Amazon Linux 2
  • Node.js version: 12.19
  • Lambda memory - 1Gb

What steps will reproduce the problem?
Build docker container and deploy it to the AWS Lambda.
The following dockerfile runs well in a local docker container and doesn't run in AWS Lambda env.
The code also runs in an EC2 t2.micro env.

FROM public.ecr.aws/lambda/nodejs:12
ENV PUPPETEER_DOWNLOAD_PATH=/var/task/chromium
ENV PUPPETEER_EXECUTABLE_PATH=$PUPPETEER_DOWNLOAD_PATH/linux-818858/chrome-linux/chrome
WORKDIR /var/task
ADD sources /var/task
RUN yum install -y atk java-atk-wrapper at-spi2-atk gtk3 libXt libdrm mesa-libgbm
RUN node ./node_modules/puppeteer/install.js
RUN ldd $PUPPETEER_EXECUTABLE_PATH; exit 0
CMD ["index.handler"]

What is the expected result?
Pass all tests:

const puppeteer = require('puppeteer');
let browser = await puppeteer.launch({timeout:60000, headless:true, devtools:true, ignoreHTTPSErrors:true,
          acceptInsecureCerts:true, executablePath:process.env.PUPPETEER_EXECUTABLE_PATH, args:[...]});
let page = await browser.newPage();
let response = await page.goto('https://www.microsoft.com', { waitUntil: 'load' });
await page.close();
await browser.close();

What happens instead?
The puppeteer passes the launch and fails at the creation of a new empty page, despite that the ldd $PUPPETEER_EXECUTABLE_PATH finds all libraries.

puppeteer:launcher Calling /var/task/chromium/linux-818858/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --enable-blink-features=IdleDetection --auto-open-devtools-for-tabs --headless --hide-scrollbars --mute-audio about:blank --proxy-server='direct://' --proxy-bypass-list=* --no-sandbox --log-level=3 --no-default-browser-check --disable-site-isolation-trials --no-experiments --ignore-gpu-blocklist --ignore-certificate-errors --ignore-certificate-errors-spki-list --disable-gpu --disable-extensions --disable-default-apps --enable-features=NetworkService --disable-setuid-sandbox --disable-webgl --disable-threaded-animation --disable-threaded-scrolling --disable-in-process-stack-traces --disable-histogram-customizer --disable-gl-extensions --disable-composited-antialiasing --disable-canvas-aa --disable-3d-apis --disable-accelerated-2d-canvas --disable-accelerated-jpeg-decoding --disable-accelerated-mjpeg-decode --disable-app-list-dismiss-on-blur --disable-accelerated-video-decode --remote-debugging-port=0 --user-data-dir=/tmp/puppeteer_dev_chrome_profile-Dbeu48

puppeteer:protocol:SEND ► {"method":"Target.setDiscoverTargets","params":{"discover":true},"id":1}
puppeteer:protocol:RECV ◀ {"method":"Target.targetCreated","params":{"targetInfo":{"targetId":"165C2B4FCBBB01B63251C074627A92E2","type":"page","title":"","url":"about:blank","attached":false,"canAccessOpener":false,"browserContextId":"0D3BA4EED596C6C4B17EB73B41F82957"}}}
puppeteer:protocol:RECV ◀ {"method":"Target.targetCreated","params":{"targetInfo":{"targetId":"58aecf8a-fb9b-4432-9ec3-8c449708f664","type":"browser","title":"","url":"","attached":false,"canAccessOpener":false}}}
puppeteer:protocol:RECV ◀ {"method":"Target.targetCreated","params":{"targetInfo":{"targetId":"e69acc92-f388-4b74-879a-819d331e91b9","type":"browser","title":"","url":"","attached":true,"canAccessOpener":false}}}
puppeteer:protocol:RECV ◀ {"id":1,"result":{}}
INFO: passed puppeteer.launch

puppeteer:protocol:SEND ► {"method":"Target.createTarget","params":{"url":"about:blank"},"id":2}
puppeteer:protocol:RECV ◀ {"method":"Target.targetCrashed","params":{"targetId":"10C805A527EFD2CC1082463E326BA73D","status":"failed to launch","errorCode":1002}}

ERROR: Protocol error (Target.createTarget): Target closed.\n at /var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71:63\n at new Promise (<anonymous>)\n at Connection.send (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:70:16)\n at Browser._createPageInContext (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:221:53)\n at BrowserContext.newPage (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:500:30)\n at Browser.newPage (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:214:37)\n at ...', message: 'Protocol error (Target.createTarget): Target closed.'

@paambaati
Copy link

paambaati commented Jan 25, 2021

I'm having the same issue (except with Alpine and not Amazon Linux), and I'd also reported this issue in the Chromium issue tracker — see https://bugs.chromium.org/p/chromium/issues/detail?id=1143503#c8

In the interest of adding more context, I'm including that issue here as well —

I'm trying to run Chromium 86.0.4240.111 on Docker (based on Alpine latest stable), and with Puppeteer, the process seems to crash as soon as I create a new Target. Attached is the log; it includes all the flags that were passed on to Chromium as well.

Additional information —

  1. I've tried all combinations of other commonly used flags (--no-zygote, --single-process, --disable-gpu, --use-gl=egl after installing the Mesa EGL packages) without luck.
  2. The Docker image (Dockerfile attached) does work fine on macOS, but when executed as a Lambda container image (see https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/), it crashes as soon as it is launched.

Attachments —

  1. lambda_chromium_log.txt
  2. Dockerfile.txt

Some observations —

  1. From what I can tell, both OP's and my issue seems to stem from GPU crashes (with dumpio, I see similar logs as OP where as soon as I launch, the GPU process starts crashing until it aborts).
  2. The Docker container does work fine on macOS. I've also run my Puppeteer-based app successfully as a plain service on EC2 on the same Alpine image; this makes me think there's something else unique about the Docker container runtime environment on AWS Lambda.

@markb-trustifi
Copy link
Author

Maybe puppeteer tries to open an internal http port inside a container? In Lambda it is impossible.

@paambaati
Copy link

Maybe puppeteer tries to open an internal http port inside a container? In Lambda it is impossible.

Interesting; although from the logs, what I see is that a browser instance is launched; it crashes soon after that because of the lack of a GPU.

From the logs —

GPU process isn’t usable. Goodbye.

@markb-trustifi
Copy link
Author

@paambaati the puppeteer runs with the flag --disable-gpu:
puppeteer:launcher Calling /var/task/chromium/linux-818858/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --enable-blink-features=IdleDetection --auto-open-devtools-for-tabs --headless --hide-scrollbars --mute-audio about:blank --proxy-server='direct://' --proxy-bypass-list=* --no-sandbox --log-level=3 --no-default-browser-check --disable-site-isolation-trials --no-experiments --ignore-gpu-blocklist --ignore-certificate-errors --ignore-certificate-errors-spki-list --disable-gpu --disable-extensions --disable-default-apps --enable-features=NetworkService --disable-setuid-sandbox --disable-webgl --disable-threaded-animation --disable-threaded-scrolling --disable-in-process-stack-traces --disable-histogram-customizer --disable-gl-extensions --disable-composited-antialiasing --disable-canvas-aa --disable-3d-apis --disable-accelerated-2d-canvas --disable-accelerated-jpeg-decoding --disable-accelerated-mjpeg-decode --disable-app-list-dismiss-on-blur --disable-accelerated-video-decode --remote-debugging-port=0 --user-data-dir=/tmp/puppeteer_dev_chrome_profile-Dbeu48

@paambaati
Copy link

@markb-trustifi Thanks, I’m aware (see quoted report from the Chromium issue tracker), and hence the report.

The presence of that flag seems to have no effect when running puppeteer on the AWS Lambda container environment.

@paambaati
Copy link

@mathiasbynens Would you mind taking a look? I see you’re tagged in the related issue in the Chromium bug tracker too.

@alexander-schreiner-seobility

FYI similar issue: #1947

@naro
Copy link

naro commented Mar 20, 2021

Same issue with Puppeteer 8 in a Docker container
I can run it without any issues when the Docker host is OSX@Intel
but the same code fails when the same docker container is used on OSX@M1 (Apple Silicon)

The test code is the example below. It fails on const page = await browser.newPage(); line

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({ path: 'example.png' });

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

Traceback:

/usr/local/puppeteer-pdf/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71
            this._callbacks.set(id, { resolve, reject, error: new Error(), method });
                                                              ^

Error: Protocol error (Target.createTarget): Target closed.
    at /usr/local/puppeteer-pdf/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71:63
    at new Promise (<anonymous>)
    at Connection.send (/usr/local/puppeteer-pdf/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:70:16)
    at Browser._createPageInContext (/usr/local/puppeteer-pdf/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:242:53)
    at BrowserContext.newPage (/usr/local/puppeteer-pdf/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:500:30)
    at Browser.newPage (/usr/local/puppeteer-pdf/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:235:37)
    at /usr/local/puppeteer-pdf/example.js:5:30
    at processTicksAndRejections (node:internal/process/task_queues:94:5)

@sciamp
Copy link

sciamp commented Apr 22, 2021

Same issue with Puppeteer 8 in a Docker container
I can run it without any issues when the Docker host is OSX@Intel
but the same code fails when the same docker container is used on OSX@M1 (Apple Silicon)

I don't have any chance to try it on intel but I'm facing this exact same issue with puppeteer 5.4.1 on apple silicon. Did you sort this out?

Thanks :)

@jackHedaya
Copy link

Still getting the same error running Puppeteer on Apple Silicon.

@kosta1221
Copy link

Getting the same exact issue with my lambda container function.

Protocol error (Target.setDiscoverTargets): Target closed. at /var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71:63 at new Promise (<anonymous>) at Connection.send (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:70:16) at Function.create (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:117:26) at ChromeLauncher.launch (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/node/Launcher.js:103:56) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async PuppeteerExtra.launch (/var/task/node_modules/puppeteer-extra/dist/index.cjs.js:129:25) at async launchBrowser (/var/task/puppeteer/utils/index.js:7:18) at async main (/var/task/index.js:19:13) at async retryWithTimeOut (/var/task/utils/retry.js:17:10) at async Runtime.exports.lambdaHandler [as handler] (/var/task/index.js:110:3

by the way, when I run locally using Lambda Runtime Interface Emulator everything works smoothly.

@davidbielik
Copy link

@markb-trustifi @jackHedaya have you been able to get puppeteer running in Docker on M1 Silicon? is #7546 related?

@artyil
Copy link

artyil commented Apr 2, 2022

Is there any solution or recommended workaround?

@davidbielik
Copy link

Is there any solution or recommended workaround?

https://github.com/microsoft/playwright 😢

@stale
Copy link

stale bot commented Jun 23, 2022

We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days.

@stale stale bot added the unconfirmed label Jun 23, 2022
@Nigrimmist
Copy link

Unfortunetely, it is an active issue. Have the same issue with was lambda and nodejs runtime. It is not reproducible on local, but easy to reproduce deploying it to lambda.

@stale stale bot removed the unconfirmed label Jun 27, 2022
@lucklove
Copy link

lucklove commented Jul 2, 2022

I meet the same problem on AWS lambda, and the flag --single-process helps:

        const browser = await puppeteer.launch({
            headless: true,
            args: ['--use-gl=egl', '--no-sandbox', '--single-process'],
        });

Not sure if it helps in your case.

@stale
Copy link

stale bot commented Aug 31, 2022

We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days.

@stale stale bot added the unconfirmed label Aug 31, 2022
@OrKoN
Copy link
Collaborator

OrKoN commented Sep 5, 2022

Unfortunately, we won't be able to troubleshoot the issues involving AWS Lambda. Could someone provide the CDP logs and dubmpio logs from AWS Lambda using the latest Puppeteer version? Also please try the official Docker image and see if it works: https://github.com/puppeteer/puppeteer/blob/main/docker/Dockerfile (it requires --cap-add=SYS_ADMIN so I am not sure if it will run on AWS Lambda).

With that info, I think we should report the issue to crbug.com again because https://crbug.com/143503 looks like a different issue.

@rapcal
Copy link

rapcal commented Nov 4, 2022

I meet the same problem on AWS lambda, and the flag --single-process helps:

        const browser = await puppeteer.launch({
            headless: true,
            args: ['--use-gl=egl', '--no-sandbox', '--single-process'],
        });

Not sure if it helps in your case.

Worked like a charm! Thanks a lot - life saviour after many days banging my head against the wall

@terrisgit
Copy link

terrisgit commented Aug 3, 2023

Same issue with Alpine running in AWS Fargate. Only --single-process fixes it. This problem doesn't happen on EC2, Cloud9, or even containers built and run on Apple M1.

I'm using Node 20 with Puppeteer version 21 but also saw it on all versions of Puppeteer 20. Dockerfile and package.json are here.

@rohanrajpal
Copy link

Same issue with Alpine running in AWS Fargate. Only --single-process fixes it. This problem doesn't happen on EC2, Cloud9, or even containers built and run on Apple M1.

I'm using Node 20 with Puppeteer version 21 but also saw it on all versions of Puppeteer 20. Dockerfile and package.json are here.

Yep facing the same issue on Fargate, just deployed with --single-process lets hope it works

@muthusk
Copy link

muthusk commented Dec 11, 2023

Facing this on Google Cloud functions using Puppeteer v21.6.0

@incoming-th
Copy link

Having same issue when trying to just open a login page from Microsoft (ex: https://login.microsoftonline.com) when the page check if user logged in or not before too present the form.

@OrKoN
Copy link
Collaborator

OrKoN commented Mar 29, 2024

AWS Lambda environment is limited and I believe the regular browser misses system dependencies that it needs there. Consider trying out the stripped down Chromium builds for AWS Lambda and also see tips for running in the serverless envs https://github.com/Sparticuz/chromium If you see a targetCrashed event in the logs like in this repo, make sure the server environment has all dependencies Chrome needs https://pptr.dev/troubleshooting#chrome-doesnt-launch-on-linux

@OrKoN OrKoN closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests