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

[Bug]: Browser gets disconnected in Docker without any errors #12257

Closed
2 tasks
sangn-splunk opened this issue Apr 11, 2024 · 28 comments · Fixed by #12274
Closed
2 tasks

[Bug]: Browser gets disconnected in Docker without any errors #12257

sangn-splunk opened this issue Apr 11, 2024 · 28 comments · Fixed by #12274

Comments

@sangn-splunk
Copy link

sangn-splunk commented Apr 11, 2024

Minimal, reproducible example

const puppeteer = require('puppeteer');

async function start() {
  const browserOptions = {
    headless: false,
    args: [
      '--window-position=0,0',
      '--kiosk',
      '--disable-gpu',
      '--disable-offline-auto-reload',
      '--disable-offline-auto-reload-visible-only',
      '--no-default-browser-check',
      '--disable-domain-reliability',
      '--disable-features=site-per-process',
      '--disable-site-isolation-trials',
      '--start-fullscreen',
      '--process-per-tab',
      '--display=:99',
      '--no-sandbox',
      '--window-size=1366,768',
      '--homepage',
      'about:blank',
      '--enable-chrome-browser-cloud-management'
    ],
    dumpio: true
  };
  const browser = await puppeteer.launch(browserOptions);
  const page = await browser.newPage();

  await page.goto('https://imperial.ac.uk/jobs/search-internal/');
  await new Promise(r => setTimeout(r, 20000));
  console.log("browser connected?", await browser.isConnected());
  console.log("browser page url", await page.url());
  browser.close();
}

start();

Error string

Error: Requesting main frame too early!

Bug behavior

  • Flaky
  • PDF

Background

The example used totally works when running outside a docker container, but fails for Error: Requesting main frame too early! when inside a container.

Chrome 116.0.5845.96-1 and puppeteer 21.1.0 were working for us, but we started seeing this on Chrome 121.0.6167.184-1 and puppeteer 21.9.0. This is also happening on Chrome 123 and puppeteer 22.6.3.

This is the dockerfile used to create the issue. It uses a local .deb file for specific chrome versions. I got this directly from google: wget "http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_123.0.6312.105-1_amd64.deb"

FROM ubuntu:focal
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
    curl \
    xvfb \
    gdebi-core \
    libasound2 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgdk-pixbuf2.0-0 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    xdg-utils \
    wget

COPY google-chrome-stable_123.0.6312.105-1_amd64.deb /tmp/google-chrome-123.deb

RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - \
  && apt-get install -y nodejs \
  && node --version \
  && npm --version

RUN gdebi --non-interactive /tmp/google-chrome-123.deb

RUN groupadd -r pptruser && useradd -m -r -g pptruser -G audio,video pptruser \
  && mkdir -p /home/pptruser/Downloads \
  && chown -R pptruser:pptruser /home/pptruser

USER pptruser
WORKDIR /home/pptruser

RUN npm install puppeteer@22.6.3 --save
COPY test.js .

CMD ["sleep", "infinity"]

Build: docker build -t puppeteer-example
Run: docker run -d --name puppeteer-example puppeteer-example

Then exec into the container and run the example:
docker exec -it SHA_GOES_HERE /bin/sh
$ xvfb-run node test.js

What's interesting is version google-chrome-stable_116.0.5845.96-1 of Chrome and puppeteer 21.1.0 does not have this error.

Expectation

I shouldn't see Error: Requesting main frame too early, but I do. It seems like something is disconnecting puppeteer too early resulting in this error, but I don't know what.

Reality

I see Error: Requesting main frame too early!

Puppeteer configuration file (if used)

No response

Puppeteer version

22.6.3

Node version

20.12.2

Package manager

npm

Package manager version

10.5.0

Operating system

Linux

Copy link

github-actions bot commented Apr 11, 2024

This issue was not reproducible. Please check that your example runs locally and the following:

  • Ensure the script does not rely on dependencies outside of puppeteer and puppeteer-core.
  • Ensure the error string is just the error message.
    • Bad:

      Error: something went wrong
        at Object.<anonymous> (/Users/username/repository/script.js:2:1)
        at Module._compile (node:internal/modules/cjs/loader:1159:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
        at Module.load (node:internal/modules/cjs/loader:1037:32)
        at Module._load (node:internal/modules/cjs/loader:878:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
        at node:internal/main/run_main_module:23:47
    • Good: Error: something went wrong.

  • Ensure your configuration file (if applicable) is valid.
  • If the issue is flaky (does not reproduce all the time), make sure 'Flaky' is checked.
  • If the issue is not expected to error, make sure to write 'no error'.

Once the above checks are satisfied, please edit your issue with the changes and we will
try to reproduce the bug again.


Analyzer run

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

Thanks for the reproducible example. We had a report about it before but without the reproducible example. Can you confirm that the issue is not reproducible without the '--start-fullscreen' and '--kiosk' flags?

@sangn-splunk
Copy link
Author

The error still happens without the --start-fullscreen and --kiosk flags
image

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

For me it only happens with those flags on MacOS, without Docker. I see in the logs that some target detach events are sent by the browser. Could you clarify if any of the other flags are relevant for the reproduction of the issue?

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

@sangn-splunk could you try the version 22.6.4 that was just released?

@sangn-splunk
Copy link
Author

While removing all flags did not help, it did seem like 22.6.4 resolved the issue! 🎉
image

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

@sangn-splunk yeah but you still cannot evaluate or do anything with the page or the browser because it has disconnected.

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

I am not able to reproduce it with default flags only though:

const puppeteer = require('puppeteer');

async function start() {
  const browserOptions = {
    headless: false,
    dumpio: true
  };
  const browser = await puppeteer.launch(browserOptions);
  const page = await browser.newPage();

  await page.goto('https://imperial.ac.uk/jobs/search-internal/');

  await new Promise(r => setTimeout(r, 20000));

  console.log("browser connected?", await browser.isConnected());
  console.log("browser page url", await page.url());
  console.log(await page.evaluate(() => 1 + 2))
  browser.close();
}

start();

@sangn-splunk
Copy link
Author

sangn-splunk commented Apr 11, 2024

Ahh dang I totally missed that the browser is still disconnected :(. I didn't see a stack trace, so I assumed it was all good. This is what I get:

image

Everything seems okay outside a container. The problem is running this inside a container in headful mode.

/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/util/decorators.js:96
                throw new Error(message(this));
                      ^                                                                                       
                                                                                                              
Error: Attempted to use detached Frame '3B7E1B48E43A701D69625F63C4A1F902'.              
    at CdpFrame.<anonymous> (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/util/decorators.js:96:23)
    at CdpPage.evaluate (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Page.js:828:43)                                                                                                                   
    at start (/home/pptruser/test.js:17:26)```

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

can you try using the Docker image we provide? https://github.com/puppeteer/puppeteer/pkgs/container/puppeteer

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

E.g.,

docker run -i --init --rm --cap-add=SYS_ADMIN ghcr.io/puppeteer/puppeteer:22.6.4 node -e "`cat test.js`"

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

although you would probably need headless: true for that but that should behave similar to headful

@OrKoN OrKoN changed the title [Bug]: Requesting main frame too early [Bug]: Browser gets disconnected in the headful mode in Docker Apr 11, 2024
@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

Actually, I am able to reproduce this too with the command above.

@sangn-splunk
Copy link
Author

Sure! Is it possible to run headful mode with the provided docker image? Do I need xvfb?

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

@sangn-splunk you can run headless: true it is the same as regular Chrome (from v22) with only windows not being rendered

@OrKoN OrKoN changed the title [Bug]: Browser gets disconnected in the headful mode in Docker [Bug]: Browser gets disconnected in Docker without any errors Apr 11, 2024
@sangn-splunk
Copy link
Author

Same error, Attempted to use detached Frame, when using headless: true against the provided image:

image

@sangn-splunk
Copy link
Author

For what it's worth, this is what seems to be throwing the disconnected event:

  browser.on('disconnected', async () => {                                                                                                                                                                                  
    Error.stackTraceLimit = 100                                                                                                                                                                                             
    console.log('disconnected caller', (new Error()).stack)                                                                                                                                                                 
  }) 
disconnected caller Error
    at /home/pptruser/test.js:11:41
    at /home/pptruser/node_modules/puppeteer-core/lib/cjs/third_party/mitt/mitt.js:62:7
    at Array.map (<anonymous>)
    at Object.emit (/home/pptruser/node_modules/puppeteer-core/lib/cjs/third_party/mitt/mitt.js:61:20)
    at CdpBrowser.emit (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/common/EventEmitter.js:83:23)
    at #emitDisconnected (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Browser.js:61:14)
    at /home/pptruser/node_modules/puppeteer-core/lib/cjs/third_party/mitt/mitt.js:62:7
    at Array.map (<anonymous>)
    at Object.emit (/home/pptruser/node_modules/puppeteer-core/lib/cjs/third_party/mitt/mitt.js:61:20)
    at Connection.emit (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/common/EventEmitter.js:83:23)
    at #onClose (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Connection.js:160:14)
    at WebSocket.<anonymous> (/home/pptruser/node_modules/puppeteer-core/lib/cjs/puppeteer/node/NodeWebSocketTransport.js:47:30)
    at callListener (/home/pptruser/node_modules/ws/lib/event-target.js:290:14)
    at WebSocket.onClose (/home/pptruser/node_modules/ws/lib/event-target.js:220:9)
    at WebSocket.emit (node:events:514:28)
    at WebSocket.emitClose (/home/pptruser/node_modules/ws/lib/websocket.js:265:10)
    at Socket.socketOnClose (/home/pptruser/node_modules/ws/lib/websocket.js:1289:15)
    at Socket.emit (node:events:514:28)
    at TCP.<anonymous> (node:net:337:12)

Also this did not happen on Chrome 116.0.5845.96-1 and puppeteer 21.1.0. I hope this helps.

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 11, 2024

@sangn-splunk yeah it is the websocket connection being closed by the browser

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

It also seems to be happening on this specific page. E.g., it does not happen with wikipedia.org

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

So it is a Crash and in Chrome and I see in the bugtracker that it was fixed in M124+. I will try it later.

@sangn-splunk
Copy link
Author

sangn-splunk commented Apr 12, 2024

Ah nice! I can find more example sites as well. Here's another one: https://www.imperial.ac.uk/monitoring/saml/attributes-json/

Can you share the link to the bug tracker for this specific issue?

@sangn-splunk
Copy link
Author

I also had a pageerror event registered, but I didn't see any output there, so I didn't think it was a crash.

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

@sangn-splunk it is not possible to get a page error if the whole browser crashes. I have reproduced it with the build that has crash reporting

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

The crash report is not public but the version that is says it is fixed in 124.0.6367.18

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

Unfortunately, it does not look like it has been fixed.

@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

I filed https://crbug.com/333945847

@OrKoN OrKoN added the upstream label Apr 12, 2024
@OrKoN
Copy link
Collaborator

OrKoN commented Apr 12, 2024

As a workaround, you could try

     args: [
      '--disable-features=NetworkServiceInProcess2'
    ]

but it might be important for some Puppeteer features such as network interception.

OrKoN added a commit that referenced this issue Apr 12, 2024
Puppeteer has relied on this flag for a [long time](#3738) but it looks like it is not required anymore and it might lead to crashes in the latest versions of Chrome for desktop. This CL removes the flag.

Issue #12257
@sangn-splunk
Copy link
Author

the --disable-features=NetworkServiceInProcess2 flag seems to work!

OrKoN pushed a commit that referenced this issue Apr 15, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>browsers: 2.2.2</summary>

##
[2.2.2](browsers-v2.2.1...browsers-v2.2.2)
(2024-04-15)


### Bug Fixes

* remove NetworkServiceInProcess2 set by default
([#12261](#12261))
([ff4f70f](ff4f70f)),
closes [#12257](#12257)
</details>

<details><summary>puppeteer: 22.6.5</summary>

##
[22.6.5](puppeteer-v22.6.4...puppeteer-v22.6.5)
(2024-04-15)


### Miscellaneous Chores

* **puppeteer:** Synchronize puppeteer versions


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * puppeteer-core bumped from 22.6.4 to 22.6.5
    * @puppeteer/browsers bumped from 2.2.1 to 2.2.2
</details>

<details><summary>puppeteer-core: 22.6.5</summary>

##
[22.6.5](puppeteer-core-v22.6.4...puppeteer-core-v22.6.5)
(2024-04-15)


### Bug Fixes

* remove NetworkServiceInProcess2 set by default
([#12261](#12261))
([ff4f70f](ff4f70f)),
closes [#12257](#12257)
* use setImmediate to reduce flakiness when processing events
([#12264](#12264))
([73403b3](73403b3))


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * @puppeteer/browsers bumped from 2.2.1 to 2.2.2
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants