From 6f5010b60961e27e5f6621c2714ce5864939daf1 Mon Sep 17 00:00:00 2001 From: dcrosby Date: Tue, 12 Nov 2024 10:39:35 -0800 Subject: [PATCH 1/3] [tests] Use rspec's ruby executable for run This fixes the assumption that `ruby` is always available from PATH. Signed-off-by: David Crosby --- spec/mixlib/shellout_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/mixlib/shellout_spec.rb b/spec/mixlib/shellout_spec.rb index e4ac5c6..fb7c062 100644 --- a/spec/mixlib/shellout_spec.rb +++ b/spec/mixlib/shellout_spec.rb @@ -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| "#{Gem.ruby} -e '#{code}'" } } context "when instantiating" do subject { shell_cmd } @@ -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 From bcdcd64be70a675eda10d6f8c7ec69ccd8dee29f Mon Sep 17 00:00:00 2001 From: dcrosby Date: Tue, 12 Nov 2024 12:36:59 -0800 Subject: [PATCH 2/3] Only use Ruby.gem on Unix Signed-off-by: David Crosby --- spec/mixlib/shellout_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/mixlib/shellout_spec.rb b/spec/mixlib/shellout_spec.rb index fb7c062..c4ba555 100644 --- a/spec/mixlib/shellout_spec.rb +++ b/spec/mixlib/shellout_spec.rb @@ -18,7 +18,7 @@ let(:ruby_code) { raise "define let(:ruby_code)" } let(:options) { nil } - let(:ruby_eval) { lambda { |code| "#{Gem.ruby} -e '#{code}'" } } + let(:ruby_eval) { lambda { |code| "#{unix? ? Gem.ruby : 'ruby'} -e '#{code}'" } } context "when instantiating" do subject { shell_cmd } From 594d1fe3531d3c6b1ecf0967f2f6a9c11a844da2 Mon Sep 17 00:00:00 2001 From: dcrosby Date: Tue, 12 Nov 2024 12:30:07 -0800 Subject: [PATCH 3/3] [Shellout::Unix] Progressive select timeout [#245] This intends to strike a balance between quick and longer-running shellouts Signed-off-by: David Crosby --- lib/mixlib/shellout/unix.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/mixlib/shellout/unix.rb b/lib/mixlib/shellout/unix.rb index e3d0992..1ab50e4 100644 --- a/lib/mixlib/shellout/unix.rb +++ b/lib/mixlib/shellout/unix.rb @@ -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 @@ -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)