twalpole / apparition

Capybara driver for Chrome using CDP
MIT License
363 stars 46 forks source link

Fix resource leaks #82

Open clayton-shopify opened 3 years ago

clayton-shopify commented 3 years ago

After quitting a local Chrome session, two threads (one running cleanup_async_responses, and one running process_messages) continue executing forever. This uses up memory and CPU time. If many sessions are started and quit, all available memory will eventually be consumed.

To demonstrate the problem, run this code and keep the Ruby interpreter running:

10.times do
  session = Capybara.current_session
  session.visit("about:blank")
  session.quit
end

Observe (for instance, using top -H) that 20 threads remain.

To fix the problem, I've added exit conditions to the two problematic loops. I'm not sure whether checking the status of @ws is the best approach, but it works in my testing.