yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH] proxy_pass: fix race condition due to ensure
Date: Sat,  9 May 2015 01:05:28 +0000	[thread overview]
Message-ID: <1431133528-30702-1-git-send-email-e@80x24.org> (raw)

When calling proxy_busy_mod_blocked to re-enable a descriptor via
epoll, the ensure block is dangerous because the "ensure" clause
modifies the object after the ReqRes is injected into epoll.

This is extremely dangerous as we give up exclusive access to
the object once we call epoll_ctl.

This simplifies the code a bit while we're at it.
---
 lib/yahns/proxy_http_response.rb | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index 5bb0608..90a9395 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -47,6 +47,13 @@ module Yahns::HttpResponse # :nodoc:
     wbuf.wbuf_abort if wbuf
   end
 
+  def wait_on_upstream(req_res, alive, wbuf)
+    req_res.resbuf = wbuf || Yahns::Wbuf.new(nil, alive,
+                                             self.class.output_buffer_tmpdir,
+                                             false)
+    :wait_readable # self remains in :ignore, wait on upstream
+  end
+
   # returns :wait_readable if we need to read more from req_res
   # returns :ignore if we yield control to the client(self)
   # returns nil if completely done
@@ -58,8 +65,7 @@ module Yahns::HttpResponse # :nodoc:
     have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(si) &&
                 env[REQUEST_METHOD] != HEAD
     flags = MSG_DONTWAIT
-    k = self.class
-    alive = @hs.next? && k.persistent_connections
+    alive = @hs.next? && self.class.persistent_connections
 
     res = "HTTP/1.1 #{status}\r\n"
     headers.each do |key,value| # n.b.: headers is an Array of 2-element Arrays
@@ -101,9 +107,7 @@ module Yahns::HttpResponse # :nodoc:
         when nil # premature EOF
           return proxy_err_response(nil, req_res, nil, wbuf)
         when :wait_readable
-          # for ensure:
-          wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir, false)
-          return :wait_readable # self remains in :ignore, wait on upstream
+          return wait_on_upstream(req_res, alive, wbuf)
         end until len == 0
 
       elsif kcar.chunked? # nasty chunked body
@@ -116,9 +120,7 @@ module Yahns::HttpResponse # :nodoc:
         when nil # premature EOF
           return proxy_err_response(nil, req_res, nil, wbuf)
         when :wait_readable
-          # for ensure:
-          wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir, false)
-          return :wait_readable # self remains in :ignore, wait on upstream
+          return wait_on_upstream(req_res, alive, wbuf)
         end until kcar.body_eof?
 
         buf = tmp
@@ -129,9 +131,7 @@ module Yahns::HttpResponse # :nodoc:
           when String
             buf << rv
           when :wait_readable
-            # for ensure:
-            wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir, false)
-            return :wait_readable
+            return wait_on_upstream(req_res, alive, wbuf)
           when nil # premature EOF
             return proxy_err_response(nil, req_res, nil, wbuf)
           end # no loop here
@@ -147,21 +147,17 @@ module Yahns::HttpResponse # :nodoc:
           req_res.shutdown
           break
         when :wait_readable
-          # for ensure:
-          wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir, false)
-          return :wait_readable # self remains in :ignore, wait on upstream
+          return wait_on_upstream(req_res, alive, wbuf)
         end while true
 
       end
     end
 
-    wbuf and return proxy_busy_mod_blocked(wbuf, wbuf.busy)
-    proxy_busy_mod_done(alive)
+    return proxy_busy_mod_done(alive) unless wbuf
+    req_res.resbuf = wbuf
+    proxy_busy_mod_blocked(wbuf, wbuf.busy)
   rescue => e
     proxy_err_response(502, req_res, e, wbuf)
-  ensure
-    # this happens if this method returns :wait_readable
-    req_res.resbuf = wbuf if wbuf
   end
 
   def proxy_response_finish(kcar, wbuf, req_res)
-- 
EW


             reply	other threads:[~2015-05-09  1:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-09  1:05 Eric Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-05-08 22:24 [PATCH] proxy_pass: fix race condition due to ensure 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/yahns/README

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

  git send-email \
    --in-reply-to=1431133528-30702-1-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /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/yahns.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).