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 2018/10/04 16:31:06 UTC

[couchdb] 03/04: Fix use of process_info(Pid, monitored_by)

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

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

commit 436e5f0d29ab399d6e14a4c531bfa35336591d41
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Jun 21 16:23:32 2018 -0500

    Fix use of process_info(Pid, monitored_by)
    
    This can now return references that are from NIFs monitoring the
    process. This is important for the new file IO NIFs that monitor the
    controlling process. For now we'll just take the easy way out by
    filtering the references from our returned monitor lists.
    
    Fixes #1396
---
 src/couch/src/couch_bt_engine.erl      |  2 +-
 src/couch/src/couch_file.erl           | 15 +++++++++------
 src/couch/test/couchdb_views_tests.erl |  3 ++-
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/couch/src/couch_bt_engine.erl b/src/couch/src/couch_bt_engine.erl
index 6d858ed..f856bde 100644
--- a/src/couch/src/couch_bt_engine.erl
+++ b/src/couch/src/couch_bt_engine.erl
@@ -196,7 +196,7 @@ decref(St) ->
 monitored_by(St) ->
     case erlang:process_info(St#st.fd, monitored_by) of
         {monitored_by, Pids} ->
-            Pids;
+            lists:filter(fun is_pid/1, Pids);
         _ ->
             []
     end.
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index 6aa8d0b..0c6070d 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -695,19 +695,22 @@ split_iolist([Sublist| Rest], SplitAt, BeginAcc) when is_list(Sublist) ->
 split_iolist([Byte | Rest], SplitAt, BeginAcc) when is_integer(Byte) ->
     split_iolist(Rest, SplitAt - 1, [Byte | BeginAcc]).
 
+monitored_by_pids() ->
+    {monitored_by, PidsAndRefs} = process_info(self(), monitored_by),
+    lists:filter(fun is_pid/1, PidsAndRefs).
 
 % System dbs aren't monitored by couch_stats_process_tracker
 is_idle(#file{is_sys=true}) ->
-    case process_info(self(), monitored_by) of
-        {monitored_by, []} -> true;
+    case monitored_by_pids() of
+        [] -> true;
         _ -> false
     end;
 is_idle(#file{is_sys=false}) ->
     Tracker = whereis(couch_stats_process_tracker),
-    case process_info(self(), monitored_by) of
-        {monitored_by, []} -> true;
-        {monitored_by, [Tracker]} -> true;
-        {monitored_by, [_]} -> exit(tracker_monitoring_failed);
+    case monitored_by_pids() of
+        [] -> true;
+        [Tracker] -> true;
+        [_] -> exit(tracker_monitoring_failed);
         _ -> false
     end.
 
diff --git a/src/couch/test/couchdb_views_tests.erl b/src/couch/test/couchdb_views_tests.erl
index 1b1a8e5..60bb5c9 100644
--- a/src/couch/test/couchdb_views_tests.erl
+++ b/src/couch/test/couchdb_views_tests.erl
@@ -523,7 +523,8 @@ view_cleanup(DbName) ->
 count_users(DbName) ->
     {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX]),
     DbPid = couch_db:get_pid(Db),
-    {monitored_by, Monitors} = process_info(DbPid, monitored_by),
+    {monitored_by, Monitors0} = process_info(DbPid, monitored_by),
+    Monitors = lists:filter(fun is_pid/1, Monitors0),
     CouchFiles = [P || P <- Monitors, couch_file:process_info(P) =/= undefined],
     ok = couch_db:close(Db),
     length(lists:usort(Monitors) -- [self() | CouchFiles]).