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

rnewson pushed a commit to branch changes-count
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit ab33165a06659aee6fbe8a5209a69e3b5df52b63
Author: Robert Newson <[email protected]>
AuthorDate: Wed Dec 3 12:08:37 2025 +0000

    ensure we decrement clients_requesting_changes
    
    ensure we decrement clients_requesting_changes even if the mochiweb process
    mediating the change request is killed by the client disconnect detector.
---
 src/chttpd/src/chttpd_db.erl     |  7 ++-----
 src/couch/src/couch_httpd_db.erl | 22 ++++++++++++++--------
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 5ef69f33f..44dd1cd08 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -151,11 +151,8 @@ handle_changes_req1(#httpd{} = Req, Db) ->
                 mochi = Req,
                 threshold = Max
             },
-            try
-                fabric:changes(Db, fun changes_callback/2, Acc0, ChangesArgs)
-            after
-                couch_stats:decrement_counter([couchdb, httpd, 
clients_requesting_changes])
-            end;
+            couch_httpd_db:decrement_clients_requesting_changes_on_exit(),
+            fabric:changes(Db, fun changes_callback/2, Acc0, ChangesArgs);
         _ ->
             Msg = <<"Supported `feed` types: normal, continuous, live, 
longpoll, eventsource">>,
             throw({bad_request, Msg})
diff --git a/src/couch/src/couch_httpd_db.erl b/src/couch/src/couch_httpd_db.erl
index 733991d84..96523611b 100644
--- a/src/couch/src/couch_httpd_db.erl
+++ b/src/couch/src/couch_httpd_db.erl
@@ -27,7 +27,8 @@
     handle_design_info_req/3,
     parse_copy_destination_header/1,
     parse_changes_query/2,
-    handle_changes_req/4
+    handle_changes_req/4,
+    decrement_clients_requesting_changes_on_exit/0
 ]).
 
 -import(
@@ -189,13 +190,8 @@ handle_changes_req1(Req, Db, ChangesArgs, ChangesFun) ->
     couch_stats:increment_counter(
         [couchdb, httpd, clients_requesting_changes]
     ),
-    try
-        WrapperFun(ChangesFun)
-    after
-        couch_stats:decrement_counter(
-            [couchdb, httpd, clients_requesting_changes]
-        )
-    end.
+    decrement_clients_requesting_changes_on_exit(),
+    WrapperFun(ChangesFun).
 
 handle_compact_req(#httpd{method = 'POST'} = Req, Db) ->
     case Req#httpd.path_parts of
@@ -1419,3 +1415,13 @@ validate_attachment_name(Name) ->
         true -> Name;
         false -> throw({bad_request, <<"Attachment name is not UTF-8 
encoded">>})
     end.
+
+decrement_clients_requesting_changes_on_exit() ->
+    Self = self(),
+    spawn(fun() ->
+        MonitorRef = monitor(process, Self),
+        receive
+            {'DOWN', MonitorRef, process, Self, _Info} ->
+                couch_stats:decrement_counter([couchdb, httpd, 
clients_requesting_changes])
+        end
+    end).

Reply via email to