Skip to content

Commit

Permalink
FIXUP: Process processes in the process of dying
Browse files Browse the repository at this point in the history
Turns out we could go to evict an entry just after it had decided to
leave the cache (i.e., its ddoc was just deleted). When that happens
ddoc_cache_lru would die trying to shut the process down becuase the
process died normal earlier than expected.
  • Loading branch information
davisp committed Jul 20, 2017
1 parent d538f02 commit 8e66b07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/ddoc_cache/src/ddoc_cache_entry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
-include("ddoc_cache.hrl").


-ifndef(TEST).
-define(ENTRY_SHUTDOWN_TIMEOUT, 5000).
-else.
-define(ENTRY_SHUTDOWN_TIMEOUT, 500).
-endif.


-record(st, {
key,
val,
Expand Down Expand Up @@ -77,7 +84,17 @@ start_link(Key, Default) ->


shutdown(Pid) ->
ok = gen_server:call(Pid, shutdown).
Ref = erlang:monitor(process, Pid),
ok = gen_server:cast(Pid, shutdown),
receive
{'DOWN', Ref, process, Pid, normal} ->
ok;
{'DOWN', Ref, process, Pid, Reason} ->
erlang:exit(Reason)
after ?ENTRY_SHUTDOWN_TIMEOUT ->
erlang:demonitor(Ref, [flush]),
erlang:exit({timeout, {entry_shutdown, Pid}})
end.


open(Pid, Key) ->
Expand Down Expand Up @@ -170,10 +187,6 @@ handle_call(open, From, #st{opener = Pid} = St) when is_pid(Pid) ->
handle_call(open, _From, St) ->
{reply, St#st.val, St};

handle_call(shutdown, _From, St) ->
remove_from_cache(St),
{stop, normal, ok, St};

handle_call(Msg, _From, St) ->
{stop, {bad_call, Msg}, {bad_call, Msg}, St}.

Expand Down Expand Up @@ -227,6 +240,10 @@ handle_cast(refresh, #st{opener = Pid} = St) when is_pid(Pid) ->
},
{noreply, NewSt};

handle_cast(shutdown, St) ->
remove_from_cache(St),
{stop, normal, St};

handle_cast(Msg, St) ->
{stop, {bad_cast, Msg}, St}.

Expand Down
18 changes: 18 additions & 0 deletions src/ddoc_cache/test/ddoc_cache_entry_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,21 @@ handles_bad_messages(_) ->
handles_code_change(_) ->
CCExpect = {ok, bar},
?assertEqual(CCExpect, ddoc_cache_entry:code_change(foo, bar, baz)).


handles_bad_shutdown_test_() ->
{timeout, 10, ?_test(begin
ErrorPid = spawn(fun() ->
receive
_ -> exit(bad_shutdown)
end
end),
?assertExit(bad_shutdown, ddoc_cache_entry:shutdown(ErrorPid)),
NotDeadYetPid = spawn(fun() ->
timer:sleep(infinity)
end),
?assertExit(
{timeout, {entry_shutdown, NotDeadYetPid}},
ddoc_cache_entry:shutdown(NotDeadYetPid)
)
end)}.

0 comments on commit 8e66b07

Please sign in to comment.