Skip to content

Commit

Permalink
Patch Puma::MiniSSL::Socket#read_nonblock to be non-blocking when u…
Browse files Browse the repository at this point in the history
…sing Capybaras Puma server registration - Fix Issue #2227
  • Loading branch information
twalpole committed Aug 3, 2019
1 parent d82b21a commit 8fdc231
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
27 changes: 27 additions & 0 deletions lib/capybara/registrations/patches/puma_ssl.rb
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Puma
module MiniSSL
class Socket
def read_nonblock(size, *_)
loop do
output = engine_read_all
return output if output

data = @socket.read_nonblock(size, exception: false)
raise IO::EAGAINWaitReadable if %i[wait_readable wait_writable].include? data
return nil if data.nil?

@engine.inject(data)
output = engine_read_all

return output if output

while (neg_data = @engine.extract)
@socket.write neg_data
end
end
end
end
end
end
7 changes: 5 additions & 2 deletions lib/capybara/registrations/servers.rb
Expand Up @@ -20,16 +20,19 @@
raise LoadError, 'Capybara requires `puma` version 3.8.0 or higher, please upgrade `puma` or register and specify your own server block'
end
end

# If we just run the Puma Rack handler it installs signal handlers which prevent us from being able to interrupt tests.
# Therefore construct and run the Server instance ourselves.
# Rack::Handler::Puma.run(app, { Host: host, Port: port, Threads: "0:4", workers: 0, daemon: false }.merge(options))
default_options = { Host: host, Port: port, Threads: '0:4', workers: 0, daemon: false, queue_requests: false }
default_options[:queue_requests] = false if options[:Host]&.start_with?('ssl://')
default_options = { Host: host, Port: port, Threads: '0:4', workers: 0, daemon: false}
options = default_options.merge(options)

conf = Rack::Handler::Puma.config(app, options)
events = conf.options[:Silent] ? ::Puma::Events.strings : ::Puma::Events.stdio

puma_ver = Gem::Version.new(Puma::Const::PUMA_VERSION)
require_relative 'patches/puma_ssl' if (Gem::Version.new('4.0.0')...Gem::Version.new('4.1.0')).cover? puma_ver

events.log 'Capybara starting Puma...'
events.log "* Version #{Puma::Const::PUMA_VERSION} , codename: #{Puma::Const::CODE_NAME}"
events.log "* Min threads: #{conf.options[:min_threads]}, max threads: #{conf.options[:max_threads]}"
Expand Down

0 comments on commit 8fdc231

Please sign in to comment.