Skip to content

Commit 18317d1

Browse files
committed
Adding timeout to run_command
1 parent b057d05 commit 18317d1

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

chef/lib/chef/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class Config
7777
:queue_user => "",
7878
:queue_password => "",
7979
:queue_host => "localhost",
80-
:queue_port => 61613
80+
:queue_port => 61613,
81+
:run_command_stdout_timeout => 120,
82+
:run_command_stderr_timeout => 120
8183
}
8284

8385
class << self

chef/lib/chef/mixin/command.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,24 @@ def run_command(args={})
111111

112112
exec_processing_block = lambda do |pid, stdin, stdout, stderr|
113113
stdin.close
114-
115-
stdout_string = stdout.gets(nil)
116-
if stdout_string
117-
command_stdout = stdout_string
118-
Chef::Log.debug("---- Begin #{args[:command]} STDOUT ----")
119-
Chef::Log.debug(stdout_string.strip)
120-
Chef::Log.debug("---- End #{args[:command]} STDOUT ----")
114+
115+
Timeout.timeout(Chef::Config[:run_command_stdout_timeout]) do
116+
stdout_string = stdout.gets(nil)
117+
if stdout_string
118+
command_stdout = stdout_string
119+
Chef::Log.debug("---- Begin #{args[:command]} STDOUT ----")
120+
Chef::Log.debug(stdout_string.strip)
121+
Chef::Log.debug("---- End #{args[:command]} STDOUT ----")
122+
end
121123
end
122-
stderr_string = stderr.gets(nil)
123-
if stderr_string
124-
command_stderr = stderr_string
125-
Chef::Log.debug("---- Begin #{args[:command]} STDERR ----")
126-
Chef::Log.debug(stderr_string.strip)
127-
Chef::Log.debug("---- End #{args[:command]} STDERR ----")
124+
Timeout.timeout(Chef::Config[:run_command_stderr_timeout]) do
125+
stderr_string = stderr.gets(nil)
126+
if stderr_string
127+
command_stderr = stderr_string
128+
Chef::Log.debug("---- Begin #{args[:command]} STDERR ----")
129+
Chef::Log.debug(stderr_string.strip)
130+
Chef::Log.debug("---- End #{args[:command]} STDERR ----")
131+
end
128132
end
129133
end
130134

0 commit comments

Comments
 (0)