YusukeIwaki / puppeteer-ruby

A Ruby port of Puppeteer
Apache License 2.0
290 stars 41 forks source link

Memory leak in rails apps using Puppeteer.connect(browser_ws_endpoint: '...') do |browser| .. end #338

Open preston opened 1 month ago

preston commented 1 month ago

Step To Reproduce / Observed behavior

Using puppeteer in a Rails 7.2.1 application with external browserless Chrome container connection with Puppeteer.connect(..) do |browser| .. end. Memory usage slowly creeps up. When built into a Docker image, any hard limit will eventually be hit despite the ruby VM trying to garbage collect. I am "ensure"ing a browser.close and browser.disconnect within the block. Here's the exact block...

      Puppeteer.connect(browser_ws_endpoint: ENV['WEBSOCKET_CHROME_URL']) do |browser|
        Rails.logger.debug "Attempting to capture screenshot of: + #{uri}"
        begin
          page = browser.new_page
          page.viewport = Puppeteer::Viewport.new(width: 1280, height: 1280)
          page.goto(uri.to_s, timeout: 5000) # , wait_until: 'domcontentloaded')
          self.http_screenshot = page.screenshot
        rescue StandardError => e
          # Errors can be thrown due to a number of things: DNS, timeout, etc.
          Rails.logger.debug 'Failed to capture screenshot.'
          Rails.logger.debug e
        ensure
          Rails.logger.debug 'Closing browser.'
          browser.close
          browser.disconnect
        end
      end

Expected behavior

Memory to remain fairly stable.

Environment

macOS with rvm

Paste the output of ruby --version

ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23]

preston commented 1 month ago

Also note that the method running this is within an ActiveRecord model class. I don't think that should matter.. unless it does. :)