Handle couch_auth_terminate gracefully In the case when `authentication_db` is changed we will get DOWN message with the Ref we already forgot. So we keep a list of monitors which we close but didn't receive DOWN message. COUCHDB-2547 Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/70049c44 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/70049c44 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/70049c44 Branch: refs/heads/master Commit: 70049c443098d2f747c6fd5b218092f4eda9a65e Parents: 7d49860 Author: ILYA Khlopotov Authored: Mon Feb 2 11:48:39 2015 -0800 Committer: ILYA Khlopotov Committed: Tue Feb 10 11:03:45 2015 -0800 ---------------------------------------------------------------------- src/couch_auth_cache.erl | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/70049c44/src/couch_auth_cache.erl ---------------------------------------------------------------------- diff --git a/src/couch_auth_cache.erl b/src/couch_auth_cache.erl index 0b15489..a8fdab8 100644 --- a/src/couch_auth_cache.erl +++ b/src/couch_auth_cache.erl @@ -38,6 +38,7 @@ cache_size = 0, db_notifier = nil, db_mon_ref = nil, + closed = [], event_listener = nil }). @@ -231,8 +232,18 @@ handle_info(restart_event_listener, State) -> ?MODULE, handle_db_event, nil, [{dbname, AuthDbName}] ), {noreply, State#state{event_listener=NewListener}}; +handle_info({'DOWN', Ref, _, _, shutdown}, State) -> + {stop, shutdown, State}; handle_info({'DOWN', Ref, _, _, _Reason}, #state{db_mon_ref = Ref} = State) -> - {noreply, reinit_cache(State)}. + {noreply, reinit_cache(State)}; +handle_info({'DOWN', Ref, _, _, Reason}, #state{closed = Closed} = State) -> + case lists:delete(Ref, Closed) of + Closed -> + {stop, Reason, State}; + NewClosed -> + {noreply, reinit_cache(State#state{closed = NewClosed})} + end. + terminate(_Reason, #state{event_listener = Listener}) -> couch_event:stop_listener(Listener), @@ -268,13 +279,14 @@ clear_cache(State) -> State#state{cache_size = 0}. -reinit_cache(State) -> +reinit_cache(#state{db_mon_ref = Ref, closed = Closed} = State) -> 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}), - NewState#state{db_mon_ref = erlang:monitor(process, AuthDb#db.main_pid)}. + NewState#state{closed = [Ref|Closed], + db_mon_ref = erlang:monitor(process, AuthDb#db.main_pid)}. add_cache_entry(_, _, _, #state{max_cache_size = 0} = State) ->