rubycdp / ferrum

Headless Chrome Ruby API
https://ferrum.rubycdp.com
MIT License
1.69k stars 120 forks source link

Errno::ECONNREFUSED on browser initialization #403

Closed anoam closed 9 months ago

anoam commented 10 months ago
Ferrum::Browser.new(browser_path: "/usr/bin/chromium", headless: true, browser_options: { "no-sandbox": nil, "disable-gpu": nil })

causes

Errno::ECONNREFUSED: Failed to open TCP connection to 127.0.0.1:36913 (Connection refused - connect(2) for "127.0.0.1" port 36913)
from /usr/local/lib/ruby/3.2.0/net/http.rb:1271:in `initialize'
Caused by Errno::ECONNREFUSED: Connection refused - connect(2) for "127.0.0.1" port 36913
from /usr/local/lib/ruby/3.2.0/net/http.rb:1271:in `initialize'

The port is random.

I started getting this after updating chromium to 117.0.5938.62 (same for the latest chrome though).

$ /usr/bin/chromium --version
Chromium 117.0.5938.62 built on Debian 11.7, running on Debian 11.7

It still works fine with manually installed 116.0.5845.96

$ /usr/local/bin/chrome --version
Google Chrome for Testing 116.0.5845.96
Ferrum::Browser.new(browser_path: "/usr/local/bin/chrome", headless: true, browser_options: { "no-sandbox": nil, "disable-gpu": nil })
# => initializes a new instance
$ bundle info ferrum
  * ferrum (0.14)
    Summary: Ruby headless Chrome driver
    Homepage: https://github.com/rubycdp/ferrum
    Documentation: https://github.com/rubycdp/ferrum/blob/main/README.md
    Source Code: https://github.com/rubycdp/ferrum
    Changelog: https://github.com/rubycdp/ferrum/blob/main/CHANGELOG.md
    Bug Tracker: https://github.com/rubycdp/ferrum/issues
    Path: /var/www/vendor/ruby/3.2.0/gems/ferrum-0.14
route commented 9 months ago

Is this /usr/bin/chromium real chromium binary or a wrapper from snap for example? Instead of chromium install stable chrome and try, but I don't test ferrum with chromium, only with chrome.

Closing since like you said it's most likely browser issue, nothing to do with ferrum.

anoam commented 9 months ago

@route
Oh, my apologies. It turned out that this is because chrome does conflict with jmalloc somehow. I should have doublechecked that all before oppenning the issue. I was suspecting that they just had changed API.

Further text is just in case you're interested (or for someome who have faced the same issue and googles it): Once again: the issue has nothing to do with Ferrum The way to reproduce:

FROM ruby:3.2.2-bookworm
# or 3.2.2-bullseye in my case

RUN apt-get update && apt-get upgrade --yes && apt-get install --yes libjemalloc2 chromium
RUN gem install ferrum
ENV LD_PRELOAD /usr/lib/x86_64-linux-gnu/libjemalloc.so.2

In the container:

ruby -e 'require "ferrum"; Ferrum::Browser.new(browser_path: "/usr/bin/chromium", headless: true, browser_options: { "no-sandbox": nil, "disable-gpu": nil })'

So far I just removed jmalloc and everything looks OK so far. My apologies for this offtopic.

ahorner commented 7 months ago

I was running into this issue, as well. Chrome doesn't play well with jemalloc in the environment's LD_PRELOAD. I switched over to tcmalloc (via libgoogle-perftools-dev) since it's the allocator used by Chrome, and its performance characteristics aren't much worse than jemalloc's.