diff options
Diffstat (limited to 'lib/webrick')
-rw-r--r-- | lib/webrick/server.rb | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/lib/webrick/server.rb b/lib/webrick/server.rb index 47ba4c700f..87a33ffaa1 100644 --- a/lib/webrick/server.rb +++ b/lib/webrick/server.rb @@ -88,20 +88,15 @@ module WEBrick if svrs = IO.select(@listeners, nil, nil, 2.0) svrs[0].each{|svr| @tokens.pop # blocks while no token is there. - sock = svr.accept - sock.sync = true - if @config[:DoNotReverseLookup] - sock.do_not_reverse_lookup = true + if sock = accept_client(svr) + th = start_thread(sock, &block) + th[:WEBrickThread] = true + thgroup.add(th) + else + @tokens.push(nil) end - Utils::set_close_on_exec(sock) - th = start_thread(sock, &block) - th[:WEBrickThread] = true - thgroup.add(th) } end - rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPROTO => ex - # TCP connection was established but RST segment was sent - # from peer before calling TCPServer#accept. rescue Errno::EBADF, IOError => ex # if the listening socket was closed in GenericServer#shutdown, # IO::select raise it. @@ -143,6 +138,22 @@ module WEBrick private + def accept_client(svr) + sock = nil + begin + sock = svr.accept + sock.sync = true + Utils::set_close_on_exec(sock) + rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPROTO => ex + # TCP connection was established but RST segment was sent + # from peer before calling TCPServer#accept. + rescue Exception => ex + msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}" + @logger.error msg + end + return sock + end + def start_thread(sock, &block) Thread.start{ begin @@ -164,6 +175,7 @@ module WEBrick rescue Exception => ex @logger.error ex ensure + @tokens.push(nil) Thread.current[:WEBrickSocket] = nil if addr @logger.debug "close: #{addr[3]}:#{addr[1]}" @@ -172,7 +184,6 @@ module WEBrick end sock.close end - @tokens.push(nil) } end |