gen_server
documentation on Module:terminate
callback says:
Even if the gen_server process is not part of a supervision tree, this function is called if it receives an 'EXIT' message from its parent. Reason is the same as in the 'EXIT' message.
Here is my handle_info
and terminate
function:
handle_info(UnknownMessage, State) ->
io:format("Got unknown message: ~p~n", [UnknownMessage]),
{noreply, State}.
terminate(Reason, State) ->
io:format("Terminating with reason: ~p~n", [Reason]).
I start this server using gen_server:start
. I assume when I call erlang:exit(Pid, fuckoff)
, it should call terminate
callback function. But it shows:
Got unknown message: {'EXIT',<0.33.0>,fuckoff}
Which means it is calling handle_info
. But when I call gen_server:stop
, everything works as mentioned in documentation. I'm calling my gen_server
from shell. Would you please clarify this?
[UPDATE]
Here is source code of decode_msg
function inside gen_server
. If it receives any 'EXIT' message it should call terminate
function:
decode_msg(Msg, Parent, Name, State, Mod, Time, Debug, Hib) ->
case Msg of
{system, From, Req} ->
sys:handle_system_msg(Req, From, Parent, ?MODULE, Debug,
[Name, State, Mod, Time], Hib);
{'EXIT', Parent, Reason} ->
terminate(Reason, Name, Msg, Mod, State, Debug);
_Msg when Debug =:= [] ->
handle_msg(Msg, Parent, Name, State, Mod);
_Msg ->
Debug1 = sys:handle_debug(Debug, fun print_event/3,
Name, {in, Msg}),
handle_msg(Msg, Parent, Name, State, Mod, Debug1)
end.
In my case it doesn't call terminate
function.
[UPDATE]
When I start gen_server
using gen_server:start_link()
, sending an exit signal using erlang:exit(Pid, Reason)
will result in calling terminate
call back function which is an expected behaviour. It seems there is a difference in interpreting an exit signal whether a process is linked to its parent or not.
trap_exit
flag of process totrue
somewhere in the code? – Jasmininit
function. – Seroka