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 via email to