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

[rb] Handle graceful webdriver shutdown #14430

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions rb/lib/selenium/webdriver/common/child_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,10 @@ def stop(timeout = 3)
return unless @pid
return if exited?

WebDriver.logger.debug("Sending TERM to process: #{@pid}", id: :process)
terminate(@pid)
poll_for_exit(timeout)

WebDriver.logger.debug(" -> stopped #{@pid}", id: :process)
rescue TimeoutError, Errno::EINVAL
WebDriver.logger.debug(" -> sending KILL to process: #{@pid}", id: :process)
kill(@pid)
wait
WebDriver.logger.debug(" -> killed #{@pid}", id: :process)
terminate_and_wait_else_kill(timeout)
rescue Errno::ECHILD, Errno::ESRCH => e
# Process exited earlier than terminate/kill could catch
WebDriver.logger.debug(" -> process: #{@pid} does not exist (#{e.class.name})", id: :process)
end

def alive?
Expand All @@ -91,6 +85,9 @@ def exited?
WebDriver.logger.debug(" -> exit code is #{exit_code.inspect}", id: :process)

!!exit_code
rescue Errno::ECHILD, Errno::ESRCH
WebDriver.logger.debug(" -> process: #{@pid} already finished", id: :process)
true
end

def poll_for_exit(timeout)
Expand All @@ -110,20 +107,29 @@ def wait

private

def terminate_and_wait_else_kill(timeout)
WebDriver.logger.debug("Sending TERM to process: #{@pid}", id: :process)
terminate(@pid)
poll_for_exit(timeout)

WebDriver.logger.debug(" -> stopped #{@pid}", id: :process)
rescue TimeoutError, Errno::EINVAL
WebDriver.logger.debug(" -> sending KILL to process: #{@pid}", id: :process)
kill(@pid)
wait
WebDriver.logger.debug(" -> killed #{@pid}", id: :process)
end

def terminate(pid)
Process.kill(SIGTERM, pid)
end

def kill(pid)
Process.kill(SIGKILL, pid)
rescue Errno::ECHILD, Errno::ESRCH
# already dead
end

def waitpid2(pid, flags = 0)
Process.waitpid2(pid, flags)
rescue Errno::ECHILD
# already dead
end
end # ChildProcess
end # WebDriver
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common/service_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def stop_process
def stop_server
connect_to_server do |http|
headers = WebDriver::Remote::Http::Common::DEFAULT_HEADERS.dup
WebDriver.logger.debug('Sending shutdown request to server', id: :driver_service)
http.get('/shutdown', headers)
end
end
Expand Down
Loading