crystal-loot / selenium.cr

Selenium library for Crystal
https://crystal-loot.github.io/selenium.cr/
MIT License
24 stars 7 forks source link

Session#delete never release CPU resource when running in the Fiber. #43

Closed zw963 closed 4 months ago

zw963 commented 7 months ago

Following is a simple working script for describe my issue.

# 1.cr

require "selenium"

driver = Selenium::Driver.for(:chrome, base_url: "http://localhost:9515")

def ready?(driver)
  begin
    driver.status.ready?
  rescue Socket::ConnectError
    false
  end
end

def run(driver)
  if !ready?(driver)
    driver_paths = ["/usr/local/bin/chromedriver", "/usr/bin/chromedriver"]

    driver_path = driver_paths.each do |path|
      break path if File.executable? path
    end

    if driver_path.nil?
      abort "#{driver_paths.join(" or ")} not exists! Please install correct version Selenium driver for Chrome before continue, exit ..."
    end

    service = Selenium::Service.chrome(driver_path: driver_path)

    driver = Selenium::Driver.for(:chrome, service: service)
  end

  options = Selenium::Chrome::Capabilities::ChromeOptions.new
  options.args = ["--headless"]

  capabilities = Selenium::Chrome::Capabilities.new
  capabilities.chrome_options = options

  new_session = driver.create_session(capabilities)

  cookie_manager = Selenium::CookieManager.new(command_handler: new_session.command_handler, session_id: new_session.id)
  cookie_manager.delete_all_cookies

  new_session.navigate_to("https://www.bing.com/")

  new_session.delete
end

spawn run(driver)

sleep 5

When i run this script the first time, with crystal run 1.cr, I can ensure the chromedriver started correctly use htop, as following screenshot, chromedriver started with 5 subprocess, we assume this is the expected correct result.

image

Then,when i ran crystal run 1.cr again, a new chromedriver sub-process was created, but never release the resource.

image

One more process created every time when i ran crystal run 1.cr again and again, will cause memory resources run out.

Perhaps this is not a issue about selenium.cr, anyway, i create this issue here for help.

Thanks.

zw963 commented 4 months ago

Assume my code issue, close for now, will create a new issue if i can figure it out what happen.