Skip to content
Snippets Groups Projects
Commit f3c3a682 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin :two:
Browse files

Merge branch 'dcouture-popen-args' into 'master'

Use the array syntax for shelling out

See merge request !93030
parents 8270b53a 4124515f
No related branches found
No related tags found
1 merge request!93030Use the array syntax for shelling out
Pipeline #602629514 passed
......@@ -28,25 +28,26 @@ def self.max_bytes
private
def validate
pgrp = nil
pgrps = nil
valid_archive = true
validate_archive_path
Timeout.timeout(TIMEOUT_LIMIT) do
stdin, stdout, stderr, wait_thr = Open3.popen3(command, pgroup: true)
stdin.close
stderr_r, stderr_w = IO.pipe
stdout, wait_threads = Open3.pipeline_r(*command, pgroup: true, err: stderr_w )
# When validation is performed on a small archive (e.g. 100 bytes)
# `wait_thr` finishes before we can get process group id. Do not
# raise exception in this scenario.
pgrp = begin
pgrps = wait_threads.map do |wait_thr|
Process.getpgid(wait_thr[:pid])
rescue Errno::ESRCH
nil
end
pgrps.compact!
status = wait_thr.value
status = wait_threads.last.value
if status.success?
result = stdout.readline
......@@ -64,20 +65,21 @@ def validate
ensure
stdout.close
stderr.close
stderr_w.close
stderr_r.close
end
valid_archive
rescue Timeout::Error
log_error('Timeout reached during archive decompression')
Process.kill(-1, pgrp) if pgrp
pgrps.each { |pgrp| Process.kill(-1, pgrp) } if pgrps
false
rescue StandardError => e
log_error(e.message)
Process.kill(-1, pgrp) if pgrp
pgrps.each { |pgrp| Process.kill(-1, pgrp) } if pgrps
false
end
......@@ -91,7 +93,7 @@ def validate_archive_path
end
def command
"gzip -dc #{@archive_path} | wc -c"
[['gzip', '-dc', @archive_path], ['wc', '-c']]
end
def log_error(error)
......
......@@ -51,10 +51,11 @@
shared_examples 'logs raised exception and terminates validator process group' do
let(:std) { double(:std, close: nil, value: nil) }
let(:wait_thr) { double }
let(:wait_threads) { [wait_thr, wait_thr] }
before do
allow(Process).to receive(:getpgid).and_return(2)
allow(Open3).to receive(:popen3).and_return([std, std, std, wait_thr])
allow(Open3).to receive(:pipeline_r).and_return([std, wait_threads])
allow(wait_thr).to receive(:[]).with(:pid).and_return(1)
allow(wait_thr).to receive(:value).and_raise(exception)
end
......@@ -67,7 +68,7 @@
import_upload_archive_size: File.size(filepath),
message: error_message
)
expect(Process).to receive(:kill).with(-1, 2)
expect(Process).to receive(:kill).with(-1, 2).twice
expect(subject.valid?).to eq(false)
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment