[erlang-questions] How does code:purge/1 know to kill processes?

Sverker Eriksson <>
Thu Jun 9 12:38:35 CEST 2016


And make sure that external call is a tail call
to avoid old return addresses on the stack.

All not yet returned recursive calls from the old module
will get your process killed by purge.

Old live funs (that survived a GC) will also get you killed. This check
is however not totally safe and can be fooled by sending funs
between processes during the purge or storing them outside
a process heap, like in ETS for example. Such orphan funs will
lead to an 'undef' exception if called.


/Sverker, Erlang/OTP

On 06/09/2016 09:49 AM, Samuel wrote:
> The processes are killed immediately. That happens, for example, if a
> process is in a loop that doesn't involve any external call (since
> external calls would load the latest version of the code)
>
> -module(test).
> -compile(export_all).
>
> start_process() ->
>      spawn_link(fun foo/0).
>
> foo() ->
>      timer:sleep(100000),
>      foo().
>
>
> 14> c(test).
> {ok,test}
> 15> test:start_process().
> <0.81.0>
> 16> l(test).
> {ok,test}
> 17> l(test).
> ** exception exit: killed
>
> This doesn't happen if the code has time to reload code by doing an
> external call. So if we instead do
>
> -module(test).
> -compile(export_all).
>
> start_process() ->
>      spawn_link(fun foo/0).
>
> foo() ->
>      timer:sleep(100),
>      ?MODULE:foo().
>
> 26> c(test).
> {ok,test}
> 27> test:start_process().
> <0.126.0>
> 28> l(test).
> {module,test}
> 29> l(test).
> {module,test}
> 30> l(test).
> {module,test}
> 31> l(test).
> {module,test}
>
> Unless we are fast enough to load code twice while the process is
> lingering in the sleep.
>
>
> On 9 June 2016 at 09:25, Roger Lipscombe <> wrote:
>> The documentation for code:purge/1
>> (http://erlang.org/doc/man/code.html#purge-1) says "If some processes
>> still linger in the old code, these processes are killed before the
>> code is removed."
>>
>> How is "lingering" defined?
>>
>> Does the VM kill the process when it attempts to invoke the old module?
>> Does the VM walk the stack for all processes and kill those that have
>> the old code somewhere in their call stack?
>> Something else?
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>



More information about the erlang-questions mailing list