[erlang-questions] gen_server and exit(Pid, shutdown)
Raimo Niskanen
raimo+erlang-questions@REDACTED
Mon May 7 10:54:06 CEST 2018
On Fri, May 04, 2018 at 07:23:23PM +0300, Alexander Petrovsky wrote:
> Hello!
>
> After a long timer working with Erlang and using gen_server abstraction,
> I've discovered for myself that when I send exit(Pid, shutdown) for some
> gen_server (or when application stopping), according to the documentation,
> terminate callback will be called only when trap_exit flag is true. In my
> mind it's absolutely not intuitively, the documentation very clear
> for trap_exit and it's says:
In http://erlang.org/doc/man/gen_server.html, Description, 5:th paragraph:
Notice that a gen_server process does not trap exit signals
automatically, this must be explicitly initiated in the callback module.
In http://erlang.org/doc/man/gen_server.html#Module:terminate-2, 4:th
paragraph, first point in bullet list:
* The gen_server process has been set to trap exit signals.
This is a process property, it applies to all processes.
In http://erlang.org/doc/reference_manual/processes.html#id88677, section
12.7 Error Handling, Receiving Exit Signals:
The default behaviour when a process receives an exit signal with an exit
reason other than normal, is to terminate and in turn emit exit signals
with the same exit reason to its linked processes. An exit signal with
reason normal is ignored.
A process can be set to trap exit signals by calling:
process_flag(trap_exit, true)
When a process is trapping exits, it does not terminate when an exit
signal is received. Instead, the signal is transformed into a message
{'EXIT',FromPid,Reason}, which is put into the mailbox of the process,
just like a regular message.
An exception to the above is if the exit reason is kill, that is if
exit(Pid,kill) has been called. This unconditionally terminates the
process, regardless of if it is trapping exit signals.
>
> When a process is trapping exits, it does not terminate when an exit signal
> > is received. Instead, the signal is transformed into a message
> > {'EXIT',FromPid,Reason}, which is put into the mailbox of the process,
> > just like a regular message.
>
>
> So, I suppose to see the message {'EXIT', FromPid, shutdown} in
> handle_info when
> I call exit(GenServerPid, shutdown), and I always suppose, that terminate
> callback will be called (of course I remeber about reason kill)
I do not see a question here, is there one?
In http://erlang.org/doc/man/gen_server.html#Module:terminate-2
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.
So, the gen_server will interpret an {'EXIT',Parent,Reason} as an order
to terminate. Note, that it has to be from the Parent, that is; the process
that started the gen_server.
Any other exit message is handled in handle_info/2 just like any other
process message.
And if the process does not trap exits it is like any other Erlang process
killed by exit(Pid, Reason).
>
> --
> Петровский Александр / Alexander Petrovsky,
>
> Skype: askjuise
> Phone: +7 931 9877991
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list