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/10/17 20:51:32 UTC

[couchdb] 02/03: Fix prometheus message queue len race condition

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

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 4a9bef0fe02832111c0c52dd0e0215e32693bffd
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Mon Oct 17 14:46:43 2022 -0400

    Fix prometheus message queue len race condition
    
    By the time we get the message queue length the registered process might have
    died already. In that case `whereis/1` would return `undefined` and would crash
    the gen_server process.
---
 .../src/couch_prometheus_server.erl                | 23 +++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/couch_prometheus/src/couch_prometheus_server.erl b/src/couch_prometheus/src/couch_prometheus_server.erl
index 969f68ec9..fbca9eb66 100644
--- a/src/couch_prometheus/src/couch_prometheus_server.erl
+++ b/src/couch_prometheus/src/couch_prometheus_server.erl
@@ -149,23 +149,24 @@ get_io_stats() ->
     ].
 
 get_message_queue_stats() ->
-    Queues = lists:map(
-        fun(Name) ->
-            case process_info(whereis(Name), message_queue_len) of
-                {message_queue_len, N} ->
-                    N;
-                _ ->
-                    0
-            end
-        end,
-        registered()
-    ),
+    QLenFun = fun(Name) -> message_queue_len(whereis(Name)) end,
+    Queues = lists:map(QLenFun, registered()),
     [
         to_prom(erlang_message_queues, gauge, lists:sum(Queues)),
         to_prom(erlang_message_queue_min, gauge, lists:min(Queues)),
         to_prom(erlang_message_queue_max, gauge, lists:max(Queues))
     ].
 
+message_queue_len(undefined) ->
+    0;
+message_queue_len(Pid) when is_pid(Pid) ->
+    case process_info(Pid, message_queue_len) of
+        {message_queue_len, N} ->
+            N;
+        _ ->
+            0
+    end.
+
 get_run_queue_stats() ->
     %% Workaround for https://bugs.erlang.org/browse/ERL-1355
     {Normal, Dirty} =