unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / Atom feed
From: Jeremy Evans <code@jeremyevans.net>
To: unicorn-public@bogomips.org
Subject: [PATCH] Check for SocketError on first ccc attempt
Date: Fri, 24 Mar 2017 13:03:46 -0700
Message-ID: <20170324200346.GC50561@jeremyevans.local> (raw)

On OpenBSD, getsockopt(2) does not support TCP_INFO.  With the current code,
this results in a 500 for all clients if check_client_connection is enabled
on OpenBSD.

This patch rescues SocketError on the first getsockopt call, and
if SocketError is raised, it doesn't check in the future.  This
should be the same behavior as if TCP_INFO was supported but
inspect did not return a string in the expected format.
---
 lib/unicorn/http_request.rb | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 7253497..6dc0aa7 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -29,7 +29,7 @@ class Unicorn::HttpParser
   EMPTY_ARRAY = [].freeze
   @@input_class = Unicorn::TeeInput
   @@check_client_connection = false
-  @@tcpi_inspect_ok = true
+  @@tcpi_inspect_ok = nil
 
   def self.input_class
     @@input_class
@@ -154,10 +154,20 @@ def closed_state?(state) # :nodoc:
     # Not that efficient, but probably still better than doing unnecessary
     # work after a client gives up.
     def check_client_connection(socket) # :nodoc:
-      if Unicorn::TCPClient === socket && @@tcpi_inspect_ok
-        opt = socket.getsockopt(:IPPROTO_TCP, :TCP_INFO).inspect
-        if opt =~ /\bstate=(\S+)/
+      if Unicorn::TCPClient === socket && @@tcpi_inspect_ok != false
+        if @@tcpi_inspect_ok
+          opt = socket.getsockopt(:IPPROTO_TCP, :TCP_INFO).inspect
+        else
           @@tcpi_inspect_ok = true
+          opt = begin
+            socket.getsockopt(:IPPROTO_TCP, :TCP_INFO)
+          rescue SocketError
+            @@tcpi_inspect_ok = false
+            return write_http_header(socket)
+          end.inspect
+        end
+
+        if opt =~ /\bstate=(\S+)/
           raise Errno::EPIPE, "client closed connection".freeze,
                 EMPTY_ARRAY if closed_state_str?($1)
         else
-- 
2.11.0


             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-24 20:03 Jeremy Evans [this message]
2017-03-24 21:07 ` Eric Wong

Reply instructions:

You may reply publically 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://bogomips.org/unicorn/

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

  git send-email \
    --in-reply-to=20170324200346.GC50561@jeremyevans.local \
    --to=code@jeremyevans.net \
    --cc=unicorn-public@bogomips.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

unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help

Archives are clonable:
	git clone --mirror https://bogomips.org/unicorn-public
	git clone --mirror http://ou63pmih66umazou.onion/unicorn-public

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.unicorn
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.unicorn

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox