unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Andrew Hobson <ahobson@gmail.com>
To: mongrel-unicorn@rubyforge.org
Subject: Handling closed clients
Date: Tue, 5 Nov 2013 09:46:49 -0500	[thread overview]
Message-ID: <EEC9EE945FE240D2AB396470D47E8AC4@gmail.com> (raw)

We have a service that clients use to upload files. We have a couple of clients that are on slow links and so their upload times out. We get errors in the logs that I'd like to get rid of.

I was hoping that the recent commit 24b9f66dcdda44378b4053645333ce9ce336b413 would help us, but it does not. After digging in a bit, I have some ideas about why and a patch I'd like comments on.

The commit above attempts to ignore exceptions of types EOFError, Errno::ECONNRESET, Errno::EPIPE, and Errno::ENOTCONN. However, that doesn't address our issue because our exception comes when trying to write the response. It is a generic IOError and thus is not handled.

In addition, I believe that change is unsafe because I believe client code could raise those exceptions. Think about a client with a database connection: that could also raise e.g. EOFError. I believe unicorn needs to check the status of the client connection instead of the type of exception that is raised.

The patch below has two new test cases: one where the client code raises an EOFError and one that replicates the condition we see where the client socket is closed before the response can be written.

It changes the behavior of the 'test_client_shutdown_write_truncates' test, but I believe the change makes sense. If the client socket is still open for writing, unicorn should write the response.

Thoughts?

--drew

diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index 402f133..3620427 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -547,8 +547,6 @@ class Unicorn::HttpServer
# the socket is closed at the end of this function
def handle_error(client, e)
code = case e
- when EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::ENOTCONN
- # client disconnected on us and there's nothing we can do
when Unicorn::RequestURITooLongError
414
when Unicorn::RequestEntityTooLargeError
@@ -556,8 +554,12 @@ class Unicorn::HttpServer
when Unicorn::HttpParserError # try to tell the client they're bad
400
else
- Unicorn.log_error(@logger, "app error", e)
- 500
+ if client.closed?
+ # client disconnected on us and there's nothing we can do
+ else
+ Unicorn.log_error(@logger, "app error", e)
+ 500
+ end
end
if code
client.kgio_trywrite(err_response(code, @request.response_start_sent))
diff --git a/test/unit/test_server.rb b/test/unit/test_server.rb
index e5b335f..2fca1b4 100644
--- a/test/unit/test_server.rb
+++ b/test/unit/test_server.rb
@@ -145,8 +145,7 @@ class WebServerTest < Test::Unit::TestCase
# processing on us even during app dispatch
sock.shutdown(Socket::SHUT_WR)
IO.select([sock], nil, nil, 60) or raise "Timed out"
- buf = sock.read
- assert_equal "", buf
+ assert_match %r{\AHTTP/1.[01] 500\b}, sock.sysread(4096)
next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
assert_equal 'hello!\n', next_client
lines = File.readlines("test_stderr.#$$.log")
@@ -265,4 +264,55 @@ class WebServerTest < Test::Unit::TestCase
def test_listener_names
assert_equal [ "127.0.0.1:#@port" ], Unicorn.listener_names
end
+
+ # ensure that EOFError from client code is not ignored
+ def test_eof_app
+ teardown
+ app = lambda { |env| raise EOFError }
+ # [200, {}, []] }
+ redirect_test_io do
+ @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
+ @server.start
+ end
+ sock = TCPSocket.new('127.0.0.1', @port)
+ sock.syswrite("GET / HTTP/1.0\r\n\r\n")
+ assert_match %r{\AHTTP/1.[01] 500\b}, sock.sysread(4096)
+ assert_nil sock.close
+ lines = File.readlines("test_stderr.#$$.log")
+ assert lines.grep(/app error:/)
+ end
+
+ def test_file_streamed_request_close
+ teardown
+ # do a funky dance so that the socket is closed partway though the request
+ tmp = Tempfile.new('test_file_streamed_request_close')
+ app = lambda { |env|
+ while File.zero?(tmp.path)
+ sleep(0.2)
+ end
+ tmp.unlink
+ o = env['rack.input'].instance_variable_get(:@socket)
+ o.close
+ hdrs = { 'Content-Type' => 'text/plain' }
+ [ 200, hdrs, [ "#$$\n" ] ]
+ }
+ # [200, {}, []] }
+ redirect_test_io do
+ @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
+ @server.start
+ end
+ sock = TCPSocket.new('127.0.0.1', @port)
+ body = "a" * 10
+ long = "PUT /test HTTP/1.1\r\nContent-length: #{body.length + 10}\r\n\r\n" + body
+ sock.syswrite(long)
+ sock.close
+ tmp.puts "ok"
+ tmp.flush
+ while File.exists?(tmp.path)
+ sleep(0.2)
+ end
+ lines = File.readlines("test_stderr.#$$.log")
+ assert lines.grep(/app error:/).empty?
+ end
+
end






_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

             reply	other threads:[~2013-11-05 14:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-05 14:46 Andrew Hobson [this message]
2013-11-05 17:20 ` Handling closed clients Eric Wong
     [not found]   ` <m2iow6k7nk.fsf@macdaddy.atl.damballa>
2013-11-05 20:51     ` Eric Wong
     [not found]       ` <m21u2sjpc9.fsf@macdaddy.atl.damballa>
2013-11-07 16:48         ` Eric Wong
2013-11-07 20:22           ` [PATCH] stream_input: avoid IO#close on client disconnect Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=EEC9EE945FE240D2AB396470D47E8AC4@gmail.com \
    --to=ahobson@gmail.com \
    --cc=mongrel-unicorn@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).