Skip to content

Commit

Permalink
Avoid blocking on #read_nonblock
Browse files Browse the repository at this point in the history
Co-authored-by: MSP-Greg <MSP-Greg@users.noreply.github.com>
  • Loading branch information
Jesus and MSP-Greg committed Jul 30, 2019
1 parent 0b584ab commit 39bc48f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
31 changes: 15 additions & 16 deletions lib/puma/minissl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ def read_nonblock(size, *_)
output = engine_read_all
return output if output

begin
data = @socket.read_nonblock(size, exception: false)
if data == :wait_readable || data == :wait_writable
if @socket.to_io.respond_to?(data)
@socket.to_io.__send__(data)
elsif data == :wait_readable
IO.select([@socket.to_io])
else
IO.select(nil, [@socket.to_io])
end
elsif !data
return nil
else
break
end
end while true
data = @socket.read_nonblock(size, exception: false)
if data == :wait_readable || data == :wait_writable
# It would make more sense to let @socket.read_nonblock raise
# EAGAIN if necessary but it seems like it'll misbehave on Windows.
# I don't have a Windows machine to debug this so I can't explain
# exactly whats happening in that OS. Please let me know if you
# find out!
#
# In the meantime, we can emulate the correct behavior by
# capturing :wait_readable & :wait_writable and raising EAGAIN
# ourselves.
raise IO::EAGAINWaitReadable
elsif data.nil?
return nil
end

@engine.inject(data)
output = engine_read_all
Expand Down
27 changes: 23 additions & 4 deletions test/test_puma_server_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class TestPumaServerSSL < Minitest::Test

def setup
return if DISABLE_SSL
port = UniquePort.call
host = "127.0.0.1"
@port = UniquePort.call
@host = "127.0.0.1"

app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }

Expand All @@ -53,10 +53,10 @@ def setup

@events = SSLEventsHelper.new STDOUT, STDERR
@server = Puma::Server.new app, @events
@ssl_listener = @server.add_ssl_listener host, port, ctx
@ssl_listener = @server.add_ssl_listener @host, @port, ctx
@server.run

@http = Net::HTTP.new host, port
@http = Net::HTTP.new @host, @port
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
Expand All @@ -80,6 +80,25 @@ def test_url_scheme_for_https
assert_equal "https", body
end

def test_request_wont_block_thread
# Open a connection and give enough data to trigger a read, then wait
ctx = OpenSSL::SSL::SSLContext.new
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
socket = OpenSSL::SSL::SSLSocket.new TCPSocket.new(@host, @port), ctx
socket.write "x"
sleep 0.1

# Capture the amount of threads being used after connecting and being idle
thread_pool = @server.instance_variable_get(:@thread_pool)
busy_threads = thread_pool.spawned - thread_pool.waiting

socket.close

# The thread pool should be empty since the request would block on read
# and our request should have been moved to the reactor.
assert busy_threads.zero?, "Our connection is monopolizing a thread"
end

def test_very_large_return
giant = "x" * 2056610

Expand Down

0 comments on commit 39bc48f

Please sign in to comment.