Skip to content

Commit

Permalink
ref #1 Use POSIX::Spawn::Child instead of #popen3 to avoid deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nachokb committed Oct 4, 2018
1 parent 45653f9 commit 28bae8e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
34 changes: 28 additions & 6 deletions lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions wicked_pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 28bae8e

Please sign in to comment.