yahns Ruby server user/dev discussion
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH 7/7] proxy_pass: fix resumes after complete buffering is unblocked
Date: Mon, 16 May 2016 01:43:40 +0000
Message-ID: <20160516014340.8258-8-e@80x24.org> (raw)
In-Reply-To: <20160516014340.8258-1-e@80x24.org>

This fixes a bug where a cleared wbuf would kill the
process after the response got flushed out to the client
as `wbuf.busy' becomes `false'.

This fixes a regression introduced in
"proxy_pass: trim down proxy_response_finish, too"
which was never in a stable release
---
 lib/yahns/proxy_http_response.rb | 51 ++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 26 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index 65fd03b..284a3c6 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -160,7 +160,7 @@ def proxy_read_body(tip, kcar, req_res, alive, wbuf)
       req_res.proxy_trailers = [ rbuf.dup, tip ]
       return proxy_read_trailers(kcar, req_res, alive, wbuf)
     end
-    wbuf ? proxy_busy_mod_blocked(wbuf, wbuf.busy) : proxy_busy_mod_done(alive)
+    proxy_busy_mod(wbuf, alive)
   end
 
   def proxy_read_trailers(kcar, req_res, alive, wbuf)
@@ -178,7 +178,7 @@ def proxy_read_trailers(kcar, req_res, alive, wbuf)
       end # no loop here
     end
     wbuf = proxy_write(wbuf, trailer_out(tlr), alive)
-    wbuf ? proxy_busy_mod_blocked(wbuf, wbuf.busy) : proxy_busy_mod_done(alive)
+    proxy_busy_mod(wbuf, alive)
   end
 
   # start streaming the response once upstream is done sending headers to us.
@@ -196,7 +196,7 @@ def proxy_response_start(res, tip, kcar, req_res)
 
     # all done reading response from upstream, req_res will be discarded
     # when we return nil:
-    wbuf ? proxy_busy_mod_blocked(wbuf, wbuf.busy) : proxy_busy_mod_done(alive)
+    proxy_busy_mod(wbuf, alive)
   rescue => e
     proxy_err_response(502, req_res, e, wbuf)
   end
@@ -218,7 +218,7 @@ def proxy_wait_next(qflags)
     # <thread is scheduled away>                       | epoll_wait readiness
     #                                                  | ReqRes#yahns_step
     #                                                  | proxy dispatch ...
-    #                                                  | proxy_busy_mod_done
+    #                                                  | proxy_busy_mod
     # ************************** DANGER BELOW ********************************
     #                                                  | HttpClient#yahns_step
     #                                                  | # clears env
@@ -238,29 +238,28 @@ def proxy_wait_next(qflags)
     Thread.current[:yahns_queue].queue_mod(self, qflags)
   end
 
-  def proxy_busy_mod_done(alive)
-    case http_response_done(alive)
-    when :wait_readable then proxy_wait_next(Yahns::Queue::QEV_RD)
-    when :wait_writable then proxy_wait_next(Yahns::Queue::QEV_WR)
-    when :close then close
+  def proxy_busy_mod(wbuf, alive)
+    busy = wbuf.busy if wbuf
+    if busy
+      # we are completely done reading and buffering the upstream response,
+      # but have not completely written the response to the client,
+      # yield control to the client socket:
+      @state = wbuf
+      proxy_wait_next(case busy
+        when :wait_readable then Yahns::Queue::QEV_RD
+        when :wait_writable then Yahns::Queue::QEV_WR
+        else
+          raise "BUG: invalid wbuf.busy: #{busy.inspect}"
+        end)
+      # no touching self after proxy_wait_next, we may be running
+      # HttpClient#yahns_step in a different thread at this point
+    else
+      case http_response_done(alive)
+      when :wait_readable then proxy_wait_next(Yahns::Queue::QEV_RD)
+      when :wait_writable then proxy_wait_next(Yahns::Queue::QEV_WR)
+      when :close then close
+      end
     end
-
-    nil # signal close for ReqRes#yahns_step
-  end
-
-  def proxy_busy_mod_blocked(wbuf, busy)
-    # we are completely done reading and buffering the upstream response,
-    # but have not completely written the response to the client,
-    # yield control to the client socket:
-    @state = wbuf
-    proxy_wait_next(case busy
-      when :wait_readable then Yahns::Queue::QEV_RD
-      when :wait_writable then Yahns::Queue::QEV_WR
-      else
-        abort "BUG: invalid wbuf.busy: #{busy.inspect}"
-      end)
-    # no touching self after proxy_wait_next, we may be running
-    # HttpClient#yahns_step in a different thread at this point
     nil # signal close for ReqRes#yahns_step
   end
 

  parent reply index

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-16  1:43 [PATCH 0/7] proxy_pass cleanups Eric Wong
2016-05-16  1:43 ` [PATCH 1/7] proxy_pass: simplify writing request bodies upstream Eric Wong
2016-05-16  1:43 ` [PATCH 2/7] proxy_pass: hoist out proxy_res_headers method Eric Wong
2016-05-16  1:43 ` [PATCH 3/7] proxy_pass: simplify proxy_http_response Eric Wong
2016-05-16  1:43 ` [PATCH 4/7] proxy_pass: split out body and trailer reading in response Eric Wong
2016-05-16  1:43 ` [PATCH 5/7] proxy_pass: trim down proxy_response_finish, too Eric Wong
2016-05-16  1:43 ` [PATCH 6/7] proxy_pass: split out req_res into a separate file Eric Wong
2016-05-16  1:43 ` Eric Wong [this message]
2016-05-16  2:05 ` false-positive spam [Re: [PATCH 0/7] proxy_pass cleanups] 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://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=20160516014340.8258-8-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

yahns Ruby server user/dev discussion

Archives are clonable:
	git clone --mirror https://yhbt.net/yahns-public
	git clone --mirror http://ou63pmih66umazou.onion/yahns-public

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

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

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