I have been trying to monitor a gen_server using erlang:monitor/2. Unfortunately every time I try this the Erlang shell goes into an infinite loop.
Here is the test program i've written to test this out.
-module(testmon).
-compile(export_all).
start() ->
{ok,Proc} = gen_server:start(calc,[],[]),
erlang:monitor(process,Proc),
receive
{'DOWN', Ref, process, Pid, normal} ->
io:format("~p said that ~p died by natural causes~n",[Ref,Pid]);
{'DOWN', Ref, process, Pid, Reason} ->
io:format("~p said that ~p died by unnatural causes~n~p",[Ref,Pid,Reason])
end.
When I use the above code to monitor something like this spawn(fun() -> ok end) (by changing lines 6 and 7) to erlang:monitor(spawn(fun() -> ok end)) the above code works as intended.
Can someone please show me what i am doing wrong? Is it only possible to monitor a gen_server process via a supervisor?
Thank you
gen_server
doesn't terminate then thereceive
will wait for ever, unless you add a timeout as @Lukas suggested. What do you mean by "infinite loop"? Do you just mean that the shell never returns? – rvirding