Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid blocking on SSL's #read_nonblock #1857

Merged
merged 1 commit into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MSP-Greg can you verify/clarify this comment from @Jesus, so we can remove it based on your feedback?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed:

@socket.read_nonblock(size, exception: false)

to

@socket.read_nonblock(size)

and made the other adjustments.

On Windows every job failed, but Travis passed. See:
https://ci.appveyor.com/project/MSP-Greg/puma/build/job/ykqnya8f3y1ehorf
https://travis-ci.org/MSP-Greg/puma/builds/567079580

I'll investigate further when I have more time, it may have something to do with the method returning nil, but I'm not sure.

Regardless, this seems to be a Windows quirk.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JFYI, I quickly checked Ruby's OpenSSL tests, and also WEBrick. Anywhere that read_nonblock could raise an error, exception: false was used. Interesting thing is that many of the commits were done before testing was done on Windows...

#
# 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