ferrum icon indicating copy to clipboard operation
ferrum copied to clipboard

Free up Chrome browser resources after page.go_to exceeds timeout

Open jschroeder opened this issue 1 year ago • 1 comments

If the CDP command Page.navigate in Ferrum::Page#go_to exceeds the timeout but no network traffic is pending, the application flow resumes. However, the browser instance seems to have a lingering connection hanging around.

Without the ability to clean up that connection a server can quickly run out of resources.

To Reproduce

url = 'https://aberdeen.ecrash.interplat.com/'

15.times do |i|
  begin
    puts i
    browser = Ferrum::Browser.new
    browser.go_to url
  ensure
    browser.quit
  end
end

On a Google GCP VM at the 12th iteration the loop gets reliably aborted with error

...ferrum-0.16/lib/ferrum/page.rb:118:in `rescue in go_to': Request to https://aberdeen.ecrash.interplat.com/ reached server, but there are still pending connections: https://aberdeen.ecrash.interplat.com/ (Ferrum::PendingConnectionsError)
...ferrum-0.16/lib/ferrum/client.rb:92:in `send_message': Timed out waiting for response. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the :timeout option to a higher value might help. (Ferrum::TimeoutError)

This problem does not exist if the page load finishes (e.g. url https://google.com instead of 'https://aberdeen.ecrash.interplat.com/').

Running the same example on a local (Mac) workstation, or on a Digital Ocean droplet didn't show the problem, but that might be because there is no such tight limit on "connection resources"??

Expected behavior browser.quit should clean up resources so that it doesn't matter how often a new browser is instantiated in a short period of time.

The same problem exists if a single browser instance crates frequently new contexts. Calling page.close, page.stop, context.dispose or browser.crash did not fix the problem. Having a reliable way of closing out a browser session would be desirable.

jschroeder avatar Dec 04 '24 22:12 jschroeder

Interesting worth investigating

route avatar May 10 '25 14:05 route