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

Browser randomly fails to start in docker container #3

Open
MMaster opened this issue Jul 20, 2024 · 11 comments
Open

Browser randomly fails to start in docker container #3

MMaster opened this issue Jul 20, 2024 · 11 comments
Labels
bug Something isn't working

Comments

@MMaster
Copy link

MMaster commented Jul 20, 2024

When running the docker container the browser fails to start quite often throwing the following exception:

[INFO] launching the python script
[INFO] launching browser.
Traceback (most recent call last):
  File "/usr/app/src/index.py", line 46, in <module>
    loop().run_until_complete(main())
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/app/src/index.py", line 10, in main
    browser = await start(headless=False)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/nodriver/core/util.py", line 74, in start
    return await Browser.create(config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/nodriver/core/browser.py", line 87, in create
    await instance.start()
  File "/usr/local/lib/python3.12/site-packages/nodriver/core/browser.py", line 343, in start
    raise Exception(
Exception:
                ---------------------
                Failed to connect to browser
                ---------------------
                One of the causes could be when you are running as root.
                In that case you need to pass no_sandbox=True

Exception ignored in atexit callback: <function deconstruct_browser at 0x7fbf3f225580>
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/nodriver/core/util.py", line 124, in deconstruct_browser
    _.stop()
  File "/usr/local/lib/python3.12/site-packages/nodriver/core/browser.py", line 545, in stop
    asyncio.get_event_loop().create_task(self.connection.aclose())
                                         ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'aclose'

Catching the exception and trying again after 3 seconds until it succeeds seems to fix this issue.

@unixfox
Copy link
Member

unixfox commented Jul 20, 2024

How much RAM does your system have?

Could you try to add --shm-size=2G in the docker run command?

@MMaster
Copy link
Author

MMaster commented Jul 20, 2024

Currently the VM has 8GB RAM and half of it is free. I sure can increase the shared memory size for that, but I'm not really sure how that should help. I tried it and even the very first run failed.

The browser starts successfully 1 time and then fails to start 3 times on 4 different runs (it was doing the same thing with headless browser when X was not part of the docker image).

I've fixed the issue by simply doing try except on the start call and doing max 5 retries before giving up and it works, so memory doesn't seem to be the issue here.

@unixfox
Copy link
Member

unixfox commented Jul 20, 2024

Ok. That's strange because I can't replicate the issue. I launched the script 5 times in a row and never had the issue:

Are you sure you are running the latest version of the script?

image

@unixfox
Copy link
Member

unixfox commented Jul 20, 2024

@unixfox unixfox added the bug Something isn't working label Jul 20, 2024
@MMaster
Copy link
Author

MMaster commented Jul 20, 2024

Yeah I am on latest version.

The related issues that you marked are actually 2 separate issues.
The first one happens even if the browser starts successfully and returns the tokens - I suspect it is because the async event loop is not properly stopped, but instead sys.exit is called.
The second one may be related, but the solutions there don't really apply since I don't have any zombie processes, the docker container stops after the run with no leftovers and also the corrupted user data doesn't apply since it's immutable docker image.

Anyway I noticed even on stackoverflow that some people have this issue randomly with nodriver with no reliable solution.

@unixfox
Copy link
Member

unixfox commented Jul 21, 2024

Ok I was able to reproduce the issue on a VM with just 2 cores and 1GB of RAM.

@MMaster
Copy link
Author

MMaster commented Jul 21, 2024

Ok I was able to reproduce the issue on a VM with just 2 cores and 1GB of RAM.

fyi: Yesterday it stopped happening completely on the original VM. But it happened on dedicated machine with 128 GB RAM and 10 cores / 20 threads. It's completely random for me.

@unixfox
Copy link
Member

unixfox commented Jul 21, 2024

Ok I have narrowed down the issue, nodriver doesn't wait enough time before giving up trying to connect chromium instance: https://github.com/ultrafunkamsterdam/nodriver/blob/main/nodriver/core/browser.py#L340-L346

I have pushed a dirty patch in the Dockerfile for waiting more time: 0551c92#diff-dd2c0eb6ea5cfc6c4bd4eac30934e2d5746747af48fef6da689e85b752f39557R22. I validated that it works fine on my VM with 2vCPU and 1GB of RAM.

But I'm waiting for an official implementation, for which I have created a PR:

@markus583
Copy link

Hi, even with the dirty patch and also when increasing the sleep time further, I still get the same error.
VM resources are not the issue, it has much more than 2vCPU and 1GB of RAM.

Did anyone encounter this issues in other settings/has fixes for them? Thanks!

@hashamyounis9

This comment was marked as off-topic.

@markus583

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants