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

Progressive select timeout #252

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 9 additions & 4 deletions lib/mixlib/shellout/unix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ def run_command

write_to_child_stdin

select_timeout = 0.001 # 1 millisecond

until @status
ready_buffers = attempt_buffer_read
ready_buffers = attempt_buffer_read(select_timeout)
unless ready_buffers
@execution_time += READ_WAIT_TIME
@execution_time += select_timeout

# 1.3 multiplier is so we get ~10 selects before we hit the 0.01 threshold
select_timeout *= 1.3 unless select_timeout > READ_WAIT_TIME
if @execution_time >= timeout && !@result
# kill the bad proccess
reap_errant_child
Expand Down Expand Up @@ -265,8 +270,8 @@ def write_to_child_stdin
child_stdin.close # Kick things off
end

def attempt_buffer_read
ready = IO.select(open_pipes, nil, nil, READ_WAIT_TIME)
def attempt_buffer_read(select_timeout = READ_WAIT_TIME)
ready = IO.select(open_pipes, nil, nil, select_timeout)
if ready
read_stdout_to_buffer if ready.first.include?(child_stdout)
read_stderr_to_buffer if ready.first.include?(child_stderr)
Expand Down
4 changes: 2 additions & 2 deletions spec/mixlib/shellout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
let(:ruby_code) { raise "define let(:ruby_code)" }
let(:options) { nil }

let(:ruby_eval) { lambda { |code| "ruby -e '#{code}'" } }
let(:ruby_eval) { lambda { |code| "#{unix? ? Gem.ruby : 'ruby'} -e '#{code}'" } }

context "when instantiating" do
subject { shell_cmd }
Expand Down Expand Up @@ -1177,7 +1177,7 @@ def shell_out_cmd

context "on unix", :unix_only do
def ruby_wo_shell(code)
parts = %w{ruby}
parts = [ Gem.ruby ]
parts << "-e"
parts << code
end
Expand Down