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

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

Open
preston opened this issue Oct 23, 2024 · 1 comment

Comments

@preston
Copy link

preston commented Oct 23, 2024

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
Copy link
Author

preston commented Oct 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant