From 28bae8e86fd74aad570c8b068b2c73ed03100dc2 Mon Sep 17 00:00:00 2001 From: Ignacio Carrera Date: Thu, 10 May 2018 19:36:20 -0300 Subject: [PATCH] ref #1 Use POSIX::Spawn::Child instead of #popen3 to avoid deadlocks --- lib/wicked_pdf.rb | 34 ++++++++++++++++++++++++++++------ wicked_pdf.gemspec | 1 + 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/wicked_pdf.rb b/lib/wicked_pdf.rb index e1ead7d2..23e489a5 100644 --- a/lib/wicked_pdf.rb +++ b/lib/wicked_pdf.rb @@ -5,10 +5,14 @@ require 'digest/md5' require 'rbconfig' -if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9') - require 'win32/open3' +if ENV['WICKED_POPEN'] == 'ruby' + if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9') + require 'win32/open3' + else + require 'open3' + end else - require 'open3' + require 'posix/spawn' end begin @@ -75,8 +79,13 @@ def pdf_from_url(url, options = {}) print_command(command.inspect) if in_development_mode? - err = Open3.popen3(*command) do |_stdin, _stdout, stderr| - stderr.read + err = if ENV['WICKED_POPEN'] == 'ruby' + Open3.popen3(*command) do |_stdin, _stdout, stderr| + stderr.read + end + else + child = POSIX::Spawn::Child.new(*command) + child.err end if options[:return_file] return_file = options.delete(:return_file) @@ -110,7 +119,20 @@ def print_command(cmd) end def retrieve_binary_version - _stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V') + command = [ + @exe_path, + '-V' + ] + + stdout = if ENV['WICKED_POPEN'] == 'ruby' + Open3.popen3(*command) do |_stdin, stdout, _stderr| + stdout.read + end + else + child = POSIX::Spawn::Child.new(*command) + child.out + end + @binary_version = parse_version(stdout.gets(nil)) rescue StandardError DEFAULT_BINARY_VERSION diff --git a/wicked_pdf.gemspec b/wicked_pdf.gemspec index 927fa895..a401b79b 100644 --- a/wicked_pdf.gemspec +++ b/wicked_pdf.gemspec @@ -27,6 +27,7 @@ DESC spec.requirements << 'wkhtmltopdf' spec.add_dependency 'activesupport' + spec.add_dependency 'posix-spawn', '~> 0.3.11' spec.add_development_dependency 'rails' spec.add_development_dependency 'bundler', '~> 1.3'