-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process.spawn is cross-platform in Ruby 2.7+ and ChildProcess is not actively maintained at the moment. Fixes #11251.
- Loading branch information
Showing
11 changed files
with
250 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# 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. | ||
|
||
module Selenium | ||
module WebDriver | ||
module Common | ||
|
||
# | ||
# @api private | ||
# | ||
|
||
class ChildProcess | ||
TimeoutError = Class.new(StandardError) | ||
|
||
SIGTERM = 'TERM' | ||
SIGKILL = 'KILL' | ||
|
||
POLL_INTERVAL = 0.1 | ||
|
||
attr_accessor :detach | ||
attr_writer :io | ||
|
||
def self.build(*command) | ||
new(*command) | ||
end | ||
|
||
def initialize(*command) | ||
@command = command | ||
@detach = false | ||
@pid = nil | ||
@status = nil | ||
end | ||
|
||
def io | ||
@io ||= Platform.null_device | ||
end | ||
|
||
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}") | ||
@pid = Process.spawn(*@command, options) | ||
WebDriver.logger.debug(" -> pid: #{@pid}") | ||
|
||
Process.detach(@pid) if detach | ||
end | ||
|
||
def stop(timeout = 3) | ||
return unless @pid | ||
return if exited? | ||
|
||
WebDriver.logger.debug("Sending TERM to process: #{@pid}") | ||
Process.kill(SIGTERM, @pid) | ||
poll_for_exit(timeout) | ||
|
||
WebDriver.logger.debug(" -> stopped #{@pid}") | ||
rescue TimeoutError | ||
WebDriver.logger.debug(" -> sending KILL to process: #{@pid}") | ||
Process.kill(SIGKILL, @pid) | ||
wait | ||
WebDriver.logger.debug(" -> killed #{@pid}") | ||
end | ||
|
||
def alive? | ||
@pid && !exited? | ||
end | ||
|
||
def exited? | ||
return unless @pid | ||
|
||
WebDriver.logger.debug("Checking if #{@pid} is exited") | ||
_, @status = Process.waitpid2(@pid, Process::WNOHANG | Process::WUNTRACED) if @status.nil? | ||
return if @status.nil? | ||
|
||
WebDriver.logger.debug(" -> exit code is #{@status.exitstatus}") | ||
@status.exited? | ||
end | ||
|
||
def poll_for_exit(timeout) | ||
WebDriver.logger.debug("Polling #{timeout} seconds for exit of #{@pid}") | ||
|
||
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 wait | ||
return if exited? | ||
|
||
_, @status = Process.waitpid2(@pid) | ||
end | ||
|
||
end # ChildProcess | ||
end # Common | ||
end # WebDriver | ||
end # Selenium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# 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. | ||
|
||
module Selenium | ||
module WebDriver | ||
module Support | ||
|
||
# | ||
# @api private | ||
# | ||
|
||
class ChildProcess | ||
TimeoutError = Class.new(StandardError) | ||
|
||
SIGTERM = 'TERM' | ||
SIGKILL = 'KILL' | ||
|
||
POLL_INTERVAL = 0.1 | ||
|
||
attr_accessor :detach | ||
attr_writer :io | ||
|
||
def self.build(*command) | ||
new(*command) | ||
end | ||
|
||
def initialize(*command) | ||
@command = command | ||
@detach = false | ||
@pid = nil | ||
@status = nil | ||
end | ||
|
||
def io | ||
@io ||= Platform.null_device | ||
end | ||
|
||
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}") | ||
@pid = Process.spawn(*@command, options) | ||
WebDriver.logger.debug(" -> pid: #{@pid}") | ||
|
||
Process.detach(@pid) if detach | ||
end | ||
|
||
def stop(timeout = 3) | ||
return unless @pid | ||
return if exited? | ||
|
||
WebDriver.logger.debug("Sending TERM to process: #{@pid}") | ||
Process.kill(SIGTERM, @pid) | ||
poll_for_exit(timeout) | ||
|
||
WebDriver.logger.debug(" -> stopped #{@pid}") | ||
rescue TimeoutError | ||
WebDriver.logger.debug(" -> sending KILL to process: #{@pid}") | ||
Process.kill(SIGKILL, @pid) | ||
wait | ||
WebDriver.logger.debug(" -> killed #{@pid}") | ||
end | ||
|
||
def alive? | ||
@pid && !exited? | ||
end | ||
|
||
def exited? | ||
return unless @pid | ||
|
||
WebDriver.logger.debug("Checking if #{@pid} is exited") | ||
_, @status = Process.waitpid2(@pid, Process::WNOHANG | Process::WUNTRACED) if @status.nil? | ||
return if @status.nil? | ||
|
||
WebDriver.logger.debug(" -> exit code is #{@status.exitstatus}") | ||
@status.exited? | ||
end | ||
|
||
def poll_for_exit(timeout) | ||
WebDriver.logger.debug("Polling #{timeout} seconds for exit of #{@pid}") | ||
|
||
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 wait | ||
return if exited? | ||
|
||
_, @status = Process.waitpid2(@pid) | ||
end | ||
|
||
end # ChildProcess | ||
end # Support | ||
end # WebDriver | ||
end # Selenium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.