[erlang-questions] How to clean up before termination...

Dawid Figiel dawid.figiel@REDACTED
Tue Mar 20 19:18:13 CET 2012


Hi Ulf! :)

OK,... but shame on me I'd forgotten that.

http://www.erlang.org/doc/man/gen_server.html#Module:terminate-2

Thanks!

Dawid




On 3/20/12, Ulf Wiger <ulf@REDACTED> wrote:
>
> Hi Dawid,
>
> The gen_server loop will always initiate a shutdown if it receives an
> {'EXIT', Parent, shutdown}. As part of this process, it calls
> Mod:terminate(shutdown, ModState). If it doesn't trap exits, the Erlang VM
> will simply terminate the process, and the EXIT signal will never reach the
> erlang code level. Thus, without the trap_exit flag set, there is no way the
> terminate/2 function can be called.
>
> To further clarify, when the supervisor wants to terminate a child, it
> doesn't ask nicely; it simply calls exit(Child, shutdown).
>
> BR,
> Ulf W
>
> On 20 Mar 2012, at 17:44, Dawid Figiel wrote:
>
>> Heh..
>>
>> I read it view times, then I call someone to read this for me... then
>> that someone becomes confused too ;)
>>
>> (1) We jump to terminate/1 when supervisor wants to shut down the
>> gen_server.
>> (2) Trap_exit lets you handle 'EXIT' signals from linked processes.
>>
>> What is the relation between (1) and (2).
>> In my opinion function terminate has nothing to do with trap_exit.
>>
>> Regards,
>>
>> Dawid
>>
>>
>> On 3/20/12, tom kelly <ttom.kelly@REDACTED> wrote:
>>> Hi Dawid,
>>>
>>> You may have mis-read the documentation slightly, or else I misunderstand
>>> your question.
>>>
>>> If trap_exit is set to true then the terminate function is called when
>>> the
>>> gen_server is shutting down, if it is false then the terminate function
>>> is
>>> never called. All cleanup should be done in terminate, nowhere else.
>>>
>>> Is this what you're asking?
>>>
>>> //TTom.
>>>
>>>
>>>
>>> On Tue, Mar 20, 2012 at 4:17 PM, Dawid Figiel
>>> <dawid.figiel@REDACTED>wrote:
>>>
>>>> Hi,
>>>>
>>>> There is a recommendation in Erlang doc, which is telling to set up
>>>> the trap_exit to true in the gen_server:init, when want to do clean up
>>>> before supervisor will terminate the gen_server.
>>>>
>>>>
>>>> http://www.erlang.org/doc/design_principles/gen_server_concepts.html#id66567
>>>>
>>>> My question is:
>>>> What for trap_exit has to be set to true when the clean up is needed?
>>>>
>>>> The place for doing some clean ups is under terminate function.
>>>> We want to have trape_exit set to true when we need to handle 'EXIT'
>>>> signals coming from linked processes to (for example) not crash
>>>> gen_server.
>>>>
>>>> The only explanation for me is that the supervisor may crash too and
>>>> send 'Exit' to gen_server as well... but this doesn't convince even
>>>> myself.
>>>>
>>>> Has anyone some explanation for this advice ?
>>>>
>>>>
>>>> Best,
>>>>
>>>> Dawid
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> erlang-questions@REDACTED
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>>
>>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>



More information about the erlang-questions mailing list