diff options
author | Stan Hu <stanhu@gmail.com> | 2018-07-12 21:06:36 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-07-13 02:21:30 +0300 |
commit | fd392cd7255cba80bc6ee0ce3139db3732392c19 (patch) | |
tree | 9ac18f7a22c866aa00bbabfd484d11e0177fb21d /lib/gitlab/popen.rb | |
parent | 226d4d0eaa09a035ccb4cd8e83f92ffd9af9b40c (diff) |
Avoid process deadlock in popen by consuming input pipes
A process that spews a lot of output to stderr or stdout could stall out
due to the pipe buffer being full. As described in https://bugs.ruby-lang.org/issues/9082,
we can use the trick used in Ruby's capture3 function to read the pipes in separate
threads.
Closes https://gitlab.com/gitlab-org/gitlab-ee/issues/6895
Diffstat (limited to 'lib/gitlab/popen.rb')
-rw-r--r-- | lib/gitlab/popen.rb | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/gitlab/popen.rb b/lib/gitlab/popen.rb index b9832a724c4..d0cb7c1a7cf 100644 --- a/lib/gitlab/popen.rb +++ b/lib/gitlab/popen.rb @@ -34,11 +34,16 @@ module Gitlab start = Time.now Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr| + # stderr and stdout pipes can block if stderr/stdout aren't drained: https://bugs.ruby-lang.org/issues/9082 + # Mimic what Ruby does with capture3: https://github.com/ruby/ruby/blob/1ec544695fa02d714180ef9c34e755027b6a2103/lib/open3.rb#L257-L273 + out_reader = Thread.new { stdout.read } + err_reader = Thread.new { stderr.read } + yield(stdin) if block_given? stdin.close - cmd_stdout = stdout.read - cmd_stderr = stderr.read + cmd_stdout = out_reader.value + cmd_stderr = err_reader.value cmd_status = wait_thr.value end |