Search code examples
ruby-on-railscapybarapoltergeist

Capybara Poltergeist IOError: closed stream


I am using Capybara for web page scraping. I noticed a problem in which afterwards Poltergeist hangs around in the memory eating system resources. The Poltergeist Github page notes that there is a memory leak issue with Capybara (https://github.com/teampoltergeist/poltergeist) and they recommend calling

session.driver.quit

That fixes the memory issue for me, but when I want to use Capybara again later I receive the error message:

IOError: closed stream

I believe just refreshing the Capybara session will fix this but can't find a public method that allows me to restart with a brand new session.

1) Is there a method that will fix my issue?

2) Is resetting the session the best way of reopening the stream?


Solution

  • The solution that worked best was to initiate a new Capybara session like this:

    session = Capybara::Session.new(:poltergeist, Rails.application)
    session.visit(...)
    session.driver.quit
    

    For my program I encapsulated it in a WebScraper module like this:

    module WebScraper
      attr_accessor :session
    
      delegate :visit, :save_screenshot, to: :session
    
      def start_session
        @session ||= Capybara::Session.new(:poltergeist, Rails.application)
      end
    
      def end_session
        @session.driver.quit
        @session = nil
      end
    end