[erlang-questions] gen_server:terminate/2 question

Angel Alvarez clist@REDACTED
Wed Nov 11 19:32:16 CET 2009


i mistakely asumed that supervisor send a obscure OTP message to children and not a plain kill signal when not in "brutal_kill" mode.

ill reread recent messages about kill behavior, to complete understanding...

demote signals to plain messages is a interesting concept...

Thanks!

El Miércoles, 11 de Noviembre de 2009 Tamas Nagy escribió:
> Hi,
> 
> There were a recent thread on different type of exit signals. So I will not go into to much detail.
> When the supervisor shuts down it will send exit signals to the children. The difference is that in case of brutal_kill it sends a exit(Pid, kill) and this cannot be trapped so the process dies without being doing anything before it. If there is a timeout it sends an exit(Pid, shutdown) (if I remember right) which can be trapped and after the specified time it the process still hasn't stopped an exit(Pid, kill) which will definitely kill it.
> 
> So what does trapping mean. It means that exit signals are turned into regular messages which are put into the mailbox of the process which can process it as any other message. These messages have a special format.
> 13> Fun= fun() -> process_flag(trap_exit, true), receive Message -> io:format("Message received:~p~n", [Message]) end end.
> #Fun<erl_eval.20.67289768>
> 14> spawn(Fun).                                                                 
> <0.52.0>
> 15> exit(pid(0,52,0), test).
> Message received:{'EXIT',<0.48.0>,test}
> true
> 16> spawn(Fun).
> <0.55.0>
> 17> exit(pid(0,55,0), kill).
> true
> 18>
> 
> So if in your gen_server callback module you turn on trapping exits the gen_server module (the generic code) will handle the exit messages (exit signals turned into messages) coming from the supervisor for you and call the terminate function with the appropriate parameters (in case of shutdown: terminate(shutdown, State) ).
> 
> Hope this helps,
>     Tamas
> 
> ----- "Angel Alvarez" <clist@REDACTED> wrote:
> 
> > I dont undertand this part
> > 
> > 
> > i checked out again the docs but no clue
> > 
> > "...If the gen_server is part of a supervision tree and is ordered by
> > its supervisor to terminate, this function will be called with
> > Reason=shutdown if the following conditions apply: 
> > - the gen_server has been set to trap exit signals, and 
> > - the shutdown strategy as defined in the supervisor's child
> > specification is an integer timeout value, not brutal_kill. 
> > Otherwise, the gen_server will be immediately terminated."
> > 
> > Why, the need to trap signals? has the supervisor started this
> > gen_server with spawn_link. 
> > Does the gen_server behavior call terminate/2 only upon receiveing  an
> > exit mesage from the supervisor( so ti needs to trap this signal)?
> > 
> > Please explain this, thanks!?
> > 
> > 
> > 
> > El Miércoles, 11 de Noviembre de 2009 Tamas Nagy escribió:
> > > Hi,
> > > 
> > > One thing you should not forget is if you want the gen_server to do
> > a terminate/2 you need to trap exits otherwise it will die immediately
> > no matter what shutdown method is configured in the supervisor.
> > > 
> > > Regards,
> > >     Tamas
> > > 
> > > ----- "Bernard Duggan" <bernie@REDACTED> wrote:
> > > 
> > > > Yogish Baliga wrote:
> > > > > I have a gen_server process listening for events on message
> > bus.
> > > > These messages are placed in its message box.  The processing of
> > these
> > > > messages is  slower than the rate of incoming message. 
> > > > >
> > > > > When I stop the application (supervisor of which is a parent of
> > the
> > > > above mentioned gen-server), Will this gen_server get the
> > terminate/2
> > > > call immediately or only after all the messages are processed OR
> > > > should I have a wait logic in the terminate function to wait for
> > the
> > > > message box to be cleared?
> > > > >   
> > > > It depends on the shutdown method you've specified in the
> > supervisor.
> > > > 
> > > > If you've nominated 'brutal_kill', I'm pretty sure even
> > terminate/2
> > > > doesn't get called.  If you've specified a timeout or 'infinity',
> > I
> > > > think the request to shutdown will be placed in the queue after
> > all
> > > > the
> > > > other messages.  If, of course, you reach the timeout before the
> > > > message
> > > > queue is cleared and the shutdown is processed, the supervisor
> > will
> > > > fall
> > > > back to a kill which will bypass the queue (and, as above, will
> > > > probably
> > > > also mean terminate/2 doesn't get called).
> > > > 
> > > > (Anyone please feel free to correct me on the above - I've only
> > been
> > > > using the gen_server and supervisor stuff for a few months and so
> > > > could
> > > > easily be under some misapprehensions).
> > > > 
> > > > Remember also that if you want to ensure all the messages ever
> > sent
> > > > to
> > > > the server are handled, you need to make sure the things sending
> > them
> > > > stop before you make the shutdown request.
> > > > 
> > > > Cheers,
> > > > 
> > > > Bernard
> > > > 
> > > > ________________________________________________________________
> > > > erlang-questions mailing list. See http://www.erlang.org/faq.html
> > > > erlang-questions (at) erlang.org
> > > 
> > 
> > 
> > 
> > -- 
> > Agua para todo? No, Agua para Todos.
> > ->>-----------------------------------------------
> >     Clist UAH a.k.a Angel
> > ---------------------------------[www.uah.es]-<<--
> > 
> > Tú lo compras, yo lo copio. Todo legal.
> > 
> > ________________________________________________________________
> > erlang-questions mailing list. See http://www.erlang.org/faq.html
> > erlang-questions (at) erlang.org
> 



-- 
Agua para todo? No, Agua para Todos.
->>-----------------------------------------------
    Clist UAH a.k.a Angel
---------------------------------[www.uah.es]-<<--

Tú lo compras, yo lo copio. Todo legal.


More information about the erlang-questions mailing list