You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2022/05/02 22:31:14 UTC

[couchdb] 01/01: See how mochiweb with shutdown exit resons would work

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch mochiweb-with-shutdown-exits
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 7a0b15db40930693aa6f05fd6ccd5f289e3e1c9a
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Mon May 2 18:29:59 2022 -0400

    See how mochiweb with shutdown exit resons would work
    
    See https://github.com/mochi/mochiweb/commit/e56a4dce6b360c5c5d037e8de33dd267790092e4
---
 rebar.config.script                |  2 +-
 src/chttpd/src/chttpd.erl          | 11 ++++++++---
 src/chttpd/src/chttpd_external.erl |  2 ++
 src/couch/src/couch_httpd.erl      |  2 ++
 4 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/rebar.config.script b/rebar.config.script
index da3fc58a1..00a78fe4d 100644
--- a/rebar.config.script
+++ b/rebar.config.script
@@ -162,7 +162,7 @@ DepDescs = [
 {hyper,            "hyper",            {tag, "CouchDB-2.2.0-7"}},
 {ibrowse,          "ibrowse",          {tag, "CouchDB-4.4.2-5"}},
 {jiffy,            "jiffy",            {tag, "CouchDB-1.0.9-1"}},
-{mochiweb,         "mochiweb",         {tag, "CouchDB-v2.21.0-1"}},
+{mochiweb,         {url, "https://github.com/nickva/mochiweb.git"}, {tag, "use-shutdown-for-process-exits"}},
 {meck,             "meck",             {tag, "0.9.2"}},
 {recon,            "recon",            {tag, "2.5.2"}}
 ].
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 3cfdb5584..cd8982aa1 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -342,7 +342,7 @@ handle_request_int(MochiReq) ->
             {ok, Resp};
         #httpd_resp{status = aborted, reason = Reason} ->
             couch_log:error("Response abnormally terminated: ~p", [Reason]),
-            exit(normal)
+            exit({shutdown, Reason})
     end.
 
 before_request(HttpReq) ->
@@ -423,7 +423,7 @@ catch_error(HttpReq, exit, {mochiweb_recv_error, E}, _Stack) ->
         MochiReq:get(raw_path),
         E
     ]),
-    exit(normal);
+    exit({shutdown, E});
 catch_error(HttpReq, exit, {uri_too_long, _}, _Stack) ->
     send_error(HttpReq, request_uri_too_long);
 catch_error(HttpReq, exit, {body_too_large, _}, _Stack) ->
@@ -437,7 +437,10 @@ catch_error(HttpReq, Tag, Error, Stack) ->
     case {Tag, Error, Stack} of
         {exit, normal, [{mochiweb_request, send, _, _} | _]} ->
             % Client disconnect (R15+)
-            exit(normal);
+            exit(shutdown);
+        {exit, {shutdown, _}, [{mochiweb_request, send, _, _} | _]} ->
+            % Client disconnect (R15+)
+            exit(shutdown);
         _Else ->
             send_error(HttpReq, {Error, nil, Stack})
     end.
@@ -1135,6 +1138,8 @@ maybe_handle_error(Error) ->
             {500, couch_util:to_binary(Err), couch_util:to_binary(Reason)};
         normal ->
             exit(normal);
+        {shutdown, Err} ->
+            exit({shutdown, Err});
         Error ->
             {500, <<"unknown_error">>, couch_util:to_binary(Error)}
     end.
diff --git a/src/chttpd/src/chttpd_external.erl b/src/chttpd/src/chttpd_external.erl
index 84cfc9620..352087d58 100644
--- a/src/chttpd/src/chttpd_external.erl
+++ b/src/chttpd/src/chttpd_external.erl
@@ -79,6 +79,8 @@ json_req_obj_field(<<"body">>, #httpd{req_body = undefined, mochi_req = Req}, _D
     try
         Req:recv_body(MaxSize)
     catch
+        exit:{shutdown, _} ->
+            exit({bad_request, <<"Invalid request body">>});
         exit:normal ->
             exit({bad_request, <<"Invalid request body">>})
     end;
diff --git a/src/couch/src/couch_httpd.erl b/src/couch/src/couch_httpd.erl
index 64b68ce3f..629cbbdcc 100644
--- a/src/couch/src/couch_httpd.erl
+++ b/src/couch/src/couch_httpd.erl
@@ -411,6 +411,8 @@ handle_request_int(MochiReq, DefaultFun,
         throw:bad_accept_encoding_value ->
             couch_log:error("received invalid Accept-Encoding header", []),
             send_error(HttpReq, bad_request);
+        exit:{shutdown, Error} ->
+            exit({shutdown, Error});
         exit:normal ->
             exit(normal);
         exit:snappy_nif_not_loaded ->