mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
webrick: do not hang acceptor on slow TLS connections
OpenSSL::SSL::SSLSocket#accept may block indefinitely on clients which negotiate the TCP connection, but fail (or are slow) to negotiate the subsequent TLS handshake. This prevents the multi-threaded WEBrick server from accepting other connections. Since the TLS handshake (via OpenSSL::SSL::SSLSocket#accept) consists of normal read/write traffic over TCP, handle it in the per-client thread, instead. Furthermore, using non-blocking accept() is useful for non-TLS sockets anyways because spurious wakeups are possible from select(2). * lib/webrick/server.rb (accept_client): use TCPServer#accept_nonblock and remove OpenSSL::SSL::SSLSocket#accept call * lib/webrick/server.rb (start_thread): call OpenSSL::SSL::SSLSocket#accept * test/webrick/test_ssl_server.rb (test_slow_connect): new test [ruby-core:83221] [Bug #14005] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60172 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e22f35ef2a
commit
feafe07874
2 changed files with 51 additions and 10 deletions
|
@ -251,17 +251,26 @@ module WEBrick
|
||||||
# the client socket.
|
# the client socket.
|
||||||
|
|
||||||
def accept_client(svr)
|
def accept_client(svr)
|
||||||
sock = nil
|
case sock = svr.to_io.accept_nonblock(exception: false)
|
||||||
begin
|
when :wait_readable
|
||||||
sock = svr.accept
|
nil
|
||||||
Utils::set_non_blocking(sock)
|
else
|
||||||
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
|
if svr.respond_to?(:start_immediately)
|
||||||
Errno::EPROTO, Errno::EINVAL
|
sock = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
|
||||||
rescue StandardError => ex
|
sock.sync_close = true
|
||||||
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
# we cannot do OpenSSL::SSL::SSLSocket#accept here because
|
||||||
@logger.error msg
|
# a slow client can prevent us from accepting connections
|
||||||
|
# from other clients
|
||||||
|
end
|
||||||
|
sock
|
||||||
end
|
end
|
||||||
return sock
|
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
|
||||||
|
Errno::EPROTO, Errno::EINVAL
|
||||||
|
nil
|
||||||
|
rescue StandardError => ex
|
||||||
|
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
|
||||||
|
@logger.error msg
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -284,6 +293,11 @@ module WEBrick
|
||||||
@logger.debug "accept: <address unknown>"
|
@logger.debug "accept: <address unknown>"
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
if sock.respond_to?(:sync_close=) && @config[:SSLStartImmediately]
|
||||||
|
WEBrick::Utils.timeout(@config[:RequestTimeout]) do
|
||||||
|
sock.accept # OpenSSL::SSL::SSLSocket#accept
|
||||||
|
end
|
||||||
|
end
|
||||||
call_callback(:AcceptCallback, sock)
|
call_callback(:AcceptCallback, sock)
|
||||||
block ? block.call(sock) : run(sock)
|
block ? block.call(sock) : run(sock)
|
||||||
rescue Errno::ENOTCONN
|
rescue Errno::ENOTCONN
|
||||||
|
|
|
@ -2,6 +2,7 @@ require "test/unit"
|
||||||
require "webrick"
|
require "webrick"
|
||||||
require "webrick/ssl"
|
require "webrick/ssl"
|
||||||
require_relative "utils"
|
require_relative "utils"
|
||||||
|
require 'timeout'
|
||||||
|
|
||||||
class TestWEBrickSSLServer < Test::Unit::TestCase
|
class TestWEBrickSSLServer < Test::Unit::TestCase
|
||||||
class Echo < WEBrick::GenericServer
|
class Echo < WEBrick::GenericServer
|
||||||
|
@ -37,4 +38,30 @@ class TestWEBrickSSLServer < Test::Unit::TestCase
|
||||||
io.close
|
io.close
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_slow_connect
|
||||||
|
poke = lambda do |io, msg|
|
||||||
|
begin
|
||||||
|
sock = OpenSSL::SSL::SSLSocket.new(io)
|
||||||
|
sock.connect
|
||||||
|
sock.puts(msg)
|
||||||
|
assert_equal "#{msg}\n", sock.gets, msg
|
||||||
|
ensure
|
||||||
|
sock&.close
|
||||||
|
io.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
config = {
|
||||||
|
:SSLEnable => true,
|
||||||
|
:SSLCertName => "/C=JP/O=www.ruby-lang.org/CN=Ruby",
|
||||||
|
}
|
||||||
|
Timeout.timeout(10) do
|
||||||
|
TestWEBrick.start_server(Echo, config) do |server, addr, port, log|
|
||||||
|
outer = TCPSocket.new(addr, port)
|
||||||
|
inner = TCPSocket.new(addr, port)
|
||||||
|
poke.call(inner, 'fast TLS negotiation')
|
||||||
|
poke.call(outer, 'slow TLS negotiation')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue