diff --git a/rb/lib/selenium/webdriver/common/child_process.rb b/rb/lib/selenium/webdriver/common/child_process.rb index c63b8fa3b7c02..41fd206616da6 100644 --- a/rb/lib/selenium/webdriver/common/child_process.rb +++ b/rb/lib/selenium/webdriver/common/child_process.rb @@ -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? @@ -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) @@ -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 diff --git a/rb/lib/selenium/webdriver/common/service_manager.rb b/rb/lib/selenium/webdriver/common/service_manager.rb index f3e623f7b9532..c93e2d688c03e 100644 --- a/rb/lib/selenium/webdriver/common/service_manager.rb +++ b/rb/lib/selenium/webdriver/common/service_manager.rb @@ -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