diff --git a/rb/lib/selenium/webdriver/common/child_process.rb b/rb/lib/selenium/webdriver/common/child_process.rb index 2fa61ce5842d7..31875c4f68b1c 100644 --- a/rb/lib/selenium/webdriver/common/child_process.rb +++ b/rb/lib/selenium/webdriver/common/child_process.rb @@ -33,6 +33,7 @@ class TimeoutError < StandardError POLL_INTERVAL = 0.1 attr_accessor :detach, :env + attr_reader :status attr_writer :io def self.build(*command) diff --git a/rb/lib/selenium/webdriver/common/service_manager.rb b/rb/lib/selenium/webdriver/common/service_manager.rb index bd3ef61ff27db..3194c234c2a36 100644 --- a/rb/lib/selenium/webdriver/common/service_manager.rb +++ b/rb/lib/selenium/webdriver/common/service_manager.rb @@ -57,6 +57,9 @@ def start find_free_port start_process connect_until_stable + rescue StandardError + stop_process + raise end end @@ -139,6 +142,11 @@ def connect_until_stable deadline = current_time + START_TIMEOUT loop do + if process_exited? + raise Error::WebDriverError, + "#{cannot_connect_error_text}: process exited (#{@process.status || 'status unknown'})" + end + error = check_connection_error return unless error diff --git a/rb/sig/lib/selenium/webdriver/common/child_process.rbs b/rb/sig/lib/selenium/webdriver/common/child_process.rbs index 5c434d32d0003..71f41f2e5611f 100644 --- a/rb/sig/lib/selenium/webdriver/common/child_process.rbs +++ b/rb/sig/lib/selenium/webdriver/common/child_process.rbs @@ -43,6 +43,8 @@ module Selenium attr_accessor env: Hash[String, String?]? + attr_reader status: untyped + attr_writer io: untyped def self.build: (*untyped command) -> untyped diff --git a/rb/spec/unit/selenium/webdriver/common/service_manager_spec.rb b/rb/spec/unit/selenium/webdriver/common/service_manager_spec.rb new file mode 100644 index 0000000000000..799ba082235af --- /dev/null +++ b/rb/spec/unit/selenium/webdriver/common/service_manager_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +# Licensed to the Software Freedom Conservancy (SFC) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The SFC licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require File.expand_path('../spec_helper', __dir__) + +module Selenium + module WebDriver + describe ServiceManager do + unless Platform.windows? + let(:driver_dir) { Dir.mktmpdir } + + before { WebMock.disable! } + + after do + WebMock.enable! + FileUtils.rm_rf(driver_dir) + end + + def manager_for(script) + path = File.join(driver_dir, 'fake_driver') + File.write(path, "#!/bin/sh\n#{script}\n") + File.chmod(0o755, path) + described_class.new(Service.chrome(path: path)) + end + + it 'reports the exit status when the driver exits before accepting connections' do + manager = manager_for('exit 3') + + expect { manager.start }.to raise_error(Error::WebDriverError, /process exited \(pid \d+ exit 3\)/) + end + + it 'stops the driver when it does not accept connections in time' do + stub_const('Selenium::WebDriver::ServiceManager::START_TIMEOUT', 1) + manager = manager_for('exec sleep 30') + + expect { manager.start }.to raise_error(Error::WebDriverError, /ECONNREFUSED/) + expect(manager.instance_variable_get(:@process)).not_to be_alive + end + end + end + end +end