In our application we have the main app file event_store_app, which starts a top-level supervisor called event_store_sup:
start(_StartType, _StartArgs) -> event_store_sup:start_link().
The supervisor, intern, spawns 3 child processes, including one called persistence_store:
ChildSpec_PS =
{
persistence_store,
{persistence_store, start_link, []},
permanent,
500,
worker,
[persistence_store]
},
Children = [ChildSpec_ES, ChildSpec_SS, ChildSpec_PS],
{ok, {RestartStrategy, Children}}.
In the persistence_store module, we have the terminate/2 function doing some test output:
terminate(_Reason, _State) ->
FileName = "test_terminate.txt",
Text = "Called",
{ok, File} = file:open(FileName, [write]),
file:write(File, Text),
file:close(File),
io:format("persistence_store:terminate being called!~n"),
ok.
The function is exported, and when doing our unit tests, we make sure that the persistence_store module is part of the supervision tree by making it crash on purpose, then calling some gen_server functions in it, confirming that it gets restarted by the supervisor.
However, when stopping our app with application:stop(event_store), the terminate/2 function of the child process is not called, even though the documentation suggests that the application stop call will be propagated through the supervision tree and call terminate/2 in all supervised processes.
What can be the reason here for the function not getting called?