Skip to content

Commit

Permalink
[Shellout::Unix] Progressive select timeout [chef#245]
Browse files Browse the repository at this point in the history
This intends to strike a balance between quick and longer-running shellouts

Signed-off-by: David Crosby <[email protected]>
  • Loading branch information
dafyddcrosby committed Jun 19, 2024
1 parent 2c768c5 commit 13a7290
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.4
TargetRubyVersion: 2.5

Lint/UnderscorePrefixedVariableName:
Exclude:
Expand All @@ -8,4 +8,4 @@ Lint/UnderscorePrefixedVariableName:
# Configuration parameters: ContextCreatingMethods.
Lint/UselessAccessModifier:
Exclude:
- 'lib/mixlib/shellout/windows/core_ext.rb'
- 'lib/mixlib/shellout/windows/core_ext.rb'
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

0 comments on commit 13a7290

Please sign in to comment.