Skip to content

Commit

Permalink
Ensure CDP socket is closed when driver quits
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Sep 24, 2021
1 parent 2146a95 commit 677b1ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def for(browser, opts = {})

def initialize(bridge: nil, listener: nil, **opts)
@service = nil
@devtools = nil
bridge ||= create_bridge(**opts)
add_extensions(bridge.browser)
@bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge
Expand Down Expand Up @@ -180,6 +181,7 @@ def quit
bridge.quit
ensure
@service&.stop
@devtools&.close
end

#
Expand Down
13 changes: 10 additions & 3 deletions rb/lib/selenium/webdriver/devtools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
module Selenium
module WebDriver
class DevTools
RESPONSE_WAIT_TIMEOUT = 30
RESPONSE_WAIT_INTERVAL = 0.1

autoload :ConsoleEvent, 'selenium/webdriver/devtools/console_event'
autoload :ExceptionEvent, 'selenium/webdriver/devtools/exception_event'
autoload :MutationEvent, 'selenium/webdriver/devtools/mutation_event'
Expand All @@ -37,6 +40,10 @@ def initialize(url:)
start_session
end

def close
socket.close
end

def callbacks
@callbacks ||= Hash.new { |callbacks, event| callbacks[event] = [] }
end
Expand Down Expand Up @@ -100,8 +107,8 @@ def attach_socket_listener
next unless message['method']

callbacks[message['method']].each do |callback|
params = message['params'] # take in current thread!
Thread.new { callback.call(params) }
callback_thread = Thread.new(message['params'], &callback)
callback_thread.abort_on_exception = true
end
end
end
Expand All @@ -121,7 +128,7 @@ def incoming_frame
end

def wait
@wait ||= Wait.new(timeout: 10, interval: 0.1)
@wait ||= Wait.new(timeout: RESPONSE_WAIT_TIMEOUT, interval: RESPONSE_WAIT_INTERVAL)
end

def socket
Expand Down

0 comments on commit 677b1ec

Please sign in to comment.