lib/mini_magick/shell.rb in mini_magick-4.8.0 vs lib/mini_magick/shell.rb in mini_magick-4.9.0
- old
+ new
@@ -43,15 +43,14 @@
capture_command(in_w, out_r, err_r, subprocess_thread, options)
end
def execute_posix_spawn(command, options = {})
require "posix-spawn"
-
- pid, in_w, out_r, err_r = POSIX::Spawn.popen4(*command)
- subprocess_thread = Process.detach(pid)
-
- capture_command(in_w, out_r, err_r, subprocess_thread, options)
+ child = POSIX::Spawn::Child.new(*command, input: options[:stdin].to_s, timeout: MiniMagick.timeout)
+ [child.out, child.err, child.status]
+ rescue POSIX::Spawn::TimeoutExceeded
+ raise Timeout::Error
end
def capture_command(in_w, out_r, err_r, subprocess_thread, options)
[in_w, out_r, err_r].each(&:binmode)
stdout_reader = Thread.new { out_r.read }
@@ -64,10 +63,14 @@
Timeout.timeout(MiniMagick.timeout) { subprocess_thread.join }
[stdout_reader.value, stderr_reader.value, subprocess_thread.value]
rescue Timeout::Error => error
- Process.kill("TERM", subprocess_thread.pid)
+ begin
+ Process.kill("TERM", subprocess_thread.pid)
+ rescue Errno::ESRCH
+ # ignore if the PID doesn't exist
+ end
raise error
ensure
[out_r, err_r].each(&:close)
end