summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Vatamaniuc <vatamane@apache.org>2020-02-05 15:22:07 -0500
committerNick Vatamaniuc <vatamane@apache.org>2020-02-05 15:26:23 -0500
commit9792e8d3cb6f46d371120c95e0c2d21cdd165269 (patch)
tree98a7429b8320aff8500da5d675df3539ebefa707
parent1c02da2edb25789d07ee9f947d86449ee3239a4b (diff)
downloadcouchdb-demonitor-in-couch-auth-cache-tests.tar.gz
Demonitor users db when reinitializing the couch_auth_cachedemonitor-in-couch-auth-cache-tests
Previously when the db was re-initialized it ended up stacking monitors. We need to demonitor the previous instance before getting a new one. Issue: #2493
-rw-r--r--src/couch/src/couch_auth_cache.erl15
-rw-r--r--src/couch/test/eunit/couch_auth_cache_tests.erl15
2 files changed, 25 insertions, 5 deletions
diff --git a/src/couch/src/couch_auth_cache.erl b/src/couch/src/couch_auth_cache.erl
index 157b0902e..d93d7fc0e 100644
--- a/src/couch/src/couch_auth_cache.erl
+++ b/src/couch/src/couch_auth_cache.erl
@@ -39,7 +39,8 @@
max_cache_size = 0,
cache_size = 0,
db_notifier = nil,
- event_listener = nil
+ event_listener = nil,
+ auth_db_monitor = nil
}).
@@ -243,7 +244,10 @@ handle_info(restart_config_listener, State) ->
-terminate(_Reason, #state{event_listener = Listener}) ->
+terminate(_Reason, #state{event_listener = Listener, auth_db_monitor = Mon}) ->
+ if Mon =:= nil -> ok; true ->
+ erlang:demonitor(Mon, [flush])
+ end,
couch_event:stop_listener(Listener),
exec_if_auth_db(fun(AuthDb) -> catch couch_db:close(AuthDb) end),
true = ets:delete(?BY_USER),
@@ -276,13 +280,16 @@ clear_cache(State) ->
reinit_cache(#state{} = State) ->
+ Mon = State#state.auth_db_monitor,
+ if Mon =:= nil -> ok; true ->
+ erlang:demonitor(Mon, [flush])
+ end,
NewState = clear_cache(State),
AuthDbName = ?l2b(config:get("couch_httpd_auth", "authentication_db")),
true = ets:insert(?STATE, {auth_db_name, AuthDbName}),
AuthDb = open_auth_db(),
true = ets:insert(?STATE, {auth_db, AuthDb}),
- couch_db:monitor(AuthDb),
- NewState.
+ NewState#state{auth_db_monitor = couch_db:monitor(AuthDb)}.
add_cache_entry(_, _, _, #state{max_cache_size = 0} = State) ->
diff --git a/src/couch/test/eunit/couch_auth_cache_tests.erl b/src/couch/test/eunit/couch_auth_cache_tests.erl
index 5439dd7b9..18aa943bc 100644
--- a/src/couch/test/eunit/couch_auth_cache_tests.erl
+++ b/src/couch/test/eunit/couch_auth_cache_tests.erl
@@ -53,7 +53,8 @@ couch_auth_cache_test_() ->
fun should_restore_cache_on_auth_db_change/1,
fun should_recover_cache_after_shutdown/1,
fun should_close_old_db_on_auth_db_change/1,
- fun should_get_admin_from_config/1
+ fun should_get_admin_from_config/1,
+ fun should_not_pile_on_monitors/1
]
}
}
@@ -247,6 +248,18 @@ should_get_admin_from_config(_DbName) ->
?assertEqual([<<"_admin">>], Roles)
end).
+should_not_pile_on_monitors(_DbName) ->
+ ?_test(begin
+ Pid = whereis(couch_auth_cache),
+ {monitors, Mons0} = erlang:process_info(Pid, monitors),
+ lists:foreach(fun(_) ->
+ gen_server:call(Pid, reinit_cache)
+ end, lists:seq(1,100)),
+ {monitors, Mons1} = erlang:process_info(Pid, monitors),
+ ?assertEqual(Mons0, Mons1)
+ end).
+
+
update_user_doc(DbName, UserName, Password) ->
update_user_doc(DbName, UserName, Password, nil).