diff options
author | Stan Hu <stanhu@gmail.com> | 2018-07-12 11:06:36 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-07-12 16:21:30 -0700 |
commit | fd392cd7255cba80bc6ee0ce3139db3732392c19 (patch) | |
tree | 9ac18f7a22c866aa00bbabfd484d11e0177fb21d /lib | |
parent | 226d4d0eaa09a035ccb4cd8e83f92ffd9af9b40c (diff) | |
download | gitlab-ce-fd392cd7255cba80bc6ee0ce3139db3732392c19.tar.gz |
Avoid process deadlock in popen by consuming input pipessh-fix-stderr-pipe-consumption
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')
-rw-r--r-- | lib/gitlab/git/popen.rb | 18 | ||||
-rw-r--r-- | lib/gitlab/popen.rb | 9 |
2 files changed, 20 insertions, 7 deletions
diff --git a/lib/gitlab/git/popen.rb b/lib/gitlab/git/popen.rb index f9f24ecc48d..7426688fc55 100644 --- a/lib/gitlab/git/popen.rb +++ b/lib/gitlab/git/popen.rb @@ -21,6 +21,10 @@ module Gitlab Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr| stdout.set_encoding(Encoding::ASCII_8BIT) + # 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 + err_reader = Thread.new { stderr.read } + yield(stdin) if block_given? stdin.close @@ -32,7 +36,7 @@ module Gitlab cmd_output << stdout.read end - cmd_output << stderr.read + cmd_output << err_reader.value cmd_status = wait_thr.value.exitstatus end @@ -55,16 +59,20 @@ module Gitlab rerr, werr = IO.pipe pid = Process.spawn(vars, *cmd, out: wout, err: werr, chdir: path, pgroup: true) + # 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 { rout.read } + err_reader = Thread.new { rerr.read } begin - status = process_wait_with_timeout(pid, timeout) - # close write ends so we could read them wout.close werr.close - cmd_output = rout.readlines.join - cmd_output << rerr.readlines.join # Copying the behaviour of `popen` which merges stderr into output + status = process_wait_with_timeout(pid, timeout) + + cmd_output = out_reader.value + cmd_output << err_reader.value # Copying the behaviour of `popen` which merges stderr into output [cmd_output, status.exitstatus] rescue Timeout::Error => e 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 |