class Selenium::WebDriver::ChildProcess
def self.build(*command)
def self.build(*command) new(*command) end
def alive?
def alive? @pid && !exited? end
def exited?
def exited? return false unless @pid WebDriver.logger.debug("Checking if #{@pid} is exited:", id: :process) _, @status = waitpid2(@pid, Process::WNOHANG | Process::WUNTRACED) if @status.nil? return false if @status.nil? exit_code = @status.exitstatus || @status.termsig 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 initialize(*command)
def initialize(*command) @command = command @detach = false @pid = nil @status = nil end
def io
def io @io ||= Platform.null_device end
def kill(pid)
def kill(pid) Process.kill(SIGKILL, pid) end
def poll_for_exit(timeout)
def poll_for_exit(timeout) WebDriver.logger.debug("Polling #{timeout} seconds for exit of #{@pid}", id: :process) end_time = Time.now + timeout sleep POLL_INTERVAL until exited? || Time.now > end_time raise TimeoutError, " -> #{@pid} still alive after #{timeout} seconds" unless exited? end
def start
def start options = {%i[out err] => io} options[:pgroup] = true unless Platform.windows? # NOTE: this is a bug only in Windows 7 WebDriver.logger.debug("Starting process: #{@command} with #{options}", id: :process) @pid = Process.spawn(*@command, options) WebDriver.logger.debug(" -> pid: #{@pid}", id: :process) Process.detach(@pid) if detach end
def stop(timeout = 3)
def stop(timeout = 3) return unless @pid return if exited? 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 terminate(pid)
def terminate(pid) Process.kill(SIGTERM, pid) end
def terminate_and_wait_else_kill(timeout)
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 wait
def wait return if exited? _, @status = waitpid2(@pid) end
def waitpid2(pid, flags = 0)
def waitpid2(pid, flags = 0) Process.waitpid2(pid, flags) end