Skip to content

Commit 3ddbe9a

Browse files
committed
Using IO.wait and IO.ready? to check on IO streams before we block on them, fixes CHEF-44
1 parent fb69078 commit 3ddbe9a

2 files changed

Lines changed: 13 additions & 26 deletions

File tree

chef/lib/chef/mixin/command.rb

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require 'tmpdir'
2222
require 'fcntl'
2323
require 'etc'
24+
require 'io/wait'
2425

2526
class Chef
2627
module Mixin
@@ -112,44 +113,34 @@ def run_command(args={})
112113
exec_processing_block = lambda do |pid, stdin, stdout, stderr|
113114
stdin.close
114115

115-
begin
116-
Timeout.timeout(Chef::Config[:run_command_stdout_timeout]) do
117-
while stdout.ready? == nil
118-
Chef::Log.debug("Waiting for STDOUT to be ready..")
119-
sleep 1
120-
end
121-
end
122-
rescue Timeout::Error => e
123-
Chef::Log.error("#{args[:command]} timed out reading STDOUT")
124-
else
116+
stdout.sync = true
117+
stderr.sync = true
118+
119+
stdout.wait(Chef::Config[:run_command_stdout_timeout])
120+
if stdout.ready?
125121
stdout_string = stdout.gets(nil)
126122
if stdout_string
127123
command_stdout = stdout_string
128124
Chef::Log.debug("---- Begin #{args[:command]} STDOUT ----")
129125
Chef::Log.debug(stdout_string.strip)
130126
Chef::Log.debug("---- End #{args[:command]} STDOUT ----")
131127
end
128+
else
129+
Chef::Log.debug("Nothing to read on '#{args[:command]}' STDOUT, or #{Chef::Config[:run_command_stdout_timeout]} seconds exceeded.")
132130
end
133131

134-
begin
135-
Timeout.timeout(Chef::Config[:run_command_stderr_timeout]) do
136-
while stderr.ready? == nil
137-
Chef::Log.debug("Waiting for STDERR to be ready..")
138-
sleep 1
139-
end
140-
end
141-
rescue Timeout::Error => e
142-
Chef::Log.error("#{args[:command]} timed out reading STDERR")
143-
else
132+
stderr.wait(Chef::Config[:run_command_stdout_timeout])
133+
if stderr.ready?
144134
stderr_string = stderr.gets(nil)
145135
if stderr_string
146136
command_stderr = stderr_string
147137
Chef::Log.debug("---- Begin #{args[:command]} STDERR ----")
148138
Chef::Log.debug(stderr_string.strip)
149139
Chef::Log.debug("---- End #{args[:command]} STDERR ----")
150140
end
141+
else
142+
Chef::Log.debug("Nothing to read on '#{args[:command]}' STDERR, or #{Chef::Config[:run_command_stderr_timeout]} seconds exceeded.")
151143
end
152-
153144
end
154145

155146
args[:cwd] ||= Dir.tmpdir

example-repository/cookbooks/CHEF-44/recipes/default.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@
1919

2020

2121
execute "sleepy" do
22-
Chef::Config[:run_command_stdout_timeout] = 1
23-
Chef::Config[:run_command_stderr_timeout] = 1
24-
command "sleep 5"
22+
command "echo 'one monster'; sleep 5"
2523
end
2624

2725

2826
execute "sleepy2" do
29-
Chef::Config[:run_command_stdout_timeout] = 120
30-
Chef::Config[:run_command_stderr_timeout] = 120
3127
command "sleep 5"
3228
end
3329

0 commit comments

Comments
 (0)