From 719804b5df99feb8f8b871051d64bdd73524511f Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 8 May 2014 01:17:07 +0000 Subject: [PATCH] webrick/httpserver.rb: Stop handling requests on shutdown * lib/webrick/httpserver.rb (WEBrick::HTTPServer#run): stop handling requests on shutdown, even if the socket is readable and IO.select() returns true. [Fixes GH-607] * lib/webrick/server.rb (WEBrick::GenericServer#start): IO.select() raises ENOTSOCK on shutdown on Windows. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45872 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ lib/webrick/httpserver.rb | 4 ++-- lib/webrick/server.rb | 2 +- test/webrick/test_httpserver.rb | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 628f976aa8..978ecfc5e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Thu May 8 10:17:04 2014 Karsten Sperling + + * lib/webrick/httpserver.rb (WEBrick::HTTPServer#run): stop + handling requests on shutdown, even if the socket is readable + and IO.select() returns true. [Fixes GH-607] + + * lib/webrick/server.rb (WEBrick::GenericServer#start): IO.select() + raises ENOTSOCK on shutdown on Windows. + Wed May 7 21:45:00 2014 Tanaka Akira * ext/openssl/lib/openssl/ssl.rb (OpenSSL::SSL::SSLServer#accept): diff --git a/lib/webrick/httpserver.rb b/lib/webrick/httpserver.rb index 7a7b931dad..0618489c53 100644 --- a/lib/webrick/httpserver.rb +++ b/lib/webrick/httpserver.rb @@ -73,10 +73,10 @@ module WEBrick timeout = @config[:RequestTimeout] while timeout > 0 break if IO.select([sock], nil, nil, 0.5) - timeout = 0 if @status != :Running + break if @status != :Running timeout -= 0.5 end - raise HTTPStatus::EOFError if timeout <= 0 + raise HTTPStatus::EOFError if timeout <= 0 || @status != :Running raise HTTPStatus::EOFError if sock.eof? req.parse(sock) res.request_method = req.request_method diff --git a/lib/webrick/server.rb b/lib/webrick/server.rb index 3f5371ba47..bd8a045891 100644 --- a/lib/webrick/server.rb +++ b/lib/webrick/server.rb @@ -180,7 +180,7 @@ module WEBrick end } end - rescue Errno::EBADF, IOError => ex + rescue Errno::EBADF, Errno::ENOTSOCK, IOError => ex # if the listening socket was closed in GenericServer#shutdown, # IO::select raise it. rescue StandardError => ex diff --git a/test/webrick/test_httpserver.rb b/test/webrick/test_httpserver.rb index ffebf7e843..3e19a56d8f 100644 --- a/test/webrick/test_httpserver.rb +++ b/test/webrick/test_httpserver.rb @@ -366,4 +366,27 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase } assert_equal(requested, 1) end + + def test_shutdown_with_busy_keepalive_connection + requested = 0 + config = { + :ServerName => "localhost", + } + TestWEBrick.start_httpserver(config){|server, addr, port, log| + server.mount_proc("/", lambda {|req, res| res.body = "heffalump" }) + Thread.pass while server.status != :Running + + Net::HTTP.start(addr, port) do |http| + req = Net::HTTP::Get.new("/") + http.request(req){|res| assert_equal('Keep-Alive', res['Connection'], log.call) } + server.shutdown + begin + 10.times {|n| http.request(req); requested += 1 } + rescue + # Errno::ECONNREFUSED or similar + end + end + } + assert_equal(0, requested, "Server responded to #{requested} requests after shutdown") + end end