From: Tim Snowhite <tsnowhite@taximagic.com>
To: mongrel-unicorn@rubyforge.org
Cc: Tim Snowhite <tsnowhite@taximagic.com>
Subject: [PATCH] http_response: reattempt writing body chunks to the socket under situations of high EINVAL/EAGAIN load.
Date: Thu, 15 Aug 2013 18:32:16 -0400 [thread overview]
Message-ID: <1376605936-1758-1-git-send-email-tsnowhite@taximagic.com> (raw)
We run Unicorn with ruby 1.8.7 on FreeBSD and began experiencing many early terminations of the body stream when attempting to transfer files over a couple hundred Kb. The body stream would terminate with …data…HTTP 1.1 500 Internal Service Error, due to raising an Errno::EINVAL and catching this handler:
# if we get any error, try to write something back to the client
# assuming we haven't closed the socket, but don't get hung up
# if the socket is already closed or broken. We'll always ensure
# 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::EINVAL,Errno::EBADF,
Errno::ENOTCONN
500
when Unicorn::RequestURITooLongError
414
when Unicorn::RequestEntityTooLargeError
413
when Unicorn::HttpParserError # try to tell the client they're bad
400
else
Unicorn.log_error(@logger, "app error", e)
500
end
client.kgio_trywrite(err_response(code, @request.response_start_sent))
client.close
rescue
end
By reattempting the write to the socket over and over we were able to get past the error and send the appropriate data along. (It didn't usually take more than 10 retries per _connection_, I bumped the number up to 50 tries per _write_ to be far beyond anything we'd ever need.)
It appears that Kgio is used throughout unicorn to serve this exact purpose, a possible better solution might be to use something along the lines of:
body.each {|chunk|
50.times {
failure = socket.kgio_trywrite(chunk)
case failure
when :wait_writable then
next;
when String then
chunk = failure
next;
when nil then
break;
end
}
Unicorn.log_error(@logger, "response write error", Exception.new(maybe)) if @logger.respond_to?(:error)
}
Sadly I was not able to create an example scenario under which the socket.write would throw Errno::EINVAL. Any suggestions would be welcome for how to generate such a scenario.
---
lib/unicorn/http_response.rb | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb
index 083951c..edaceee 100644
--- a/lib/unicorn/http_response.rb
+++ b/lib/unicorn/http_response.rb
@@ -57,7 +57,20 @@ module Unicorn::HttpResponse
body = nil # ensure we do not close body
hijack.call(socket)
else
- body.each { |chunk| socket.write(chunk) }
+ body.each { |chunk|
+ tries = 0
+ begin
+ socket.write(chunk)
+ rescue Errno::EINVAL => e
+ @logger.error "response write retryable error einval attempt #{tries}: #{e.message} (#{e.class}) #{e.backtrace.first}" if @logger.respond_to?(:error)
+ tries += 1
+ retry if tries < 50
+ raise
+ rescue => e
+ Unicorn.log_error(@logger, "response write error", e) if @logger.respond_to?(:error)
+ raise
+ end
+ }
end
ensure
body.respond_to?(:close) and body.close
--
1.8.1.5
_______________________________________________
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
next reply other threads:[~2013-08-15 23:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-15 22:32 Tim Snowhite [this message]
2013-08-15 23:46 ` [PATCH] http_response: reattempt writing body chunks to the socket under situations of high EINVAL/EAGAIN load 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=1376605936-1758-1-git-send-email-tsnowhite@taximagic.com \
--to=tsnowhite@taximagic.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).