Mithun B
Mithun B

Reputation: 284

Erlang process: normal exit ignored by linked process, exits after "compiled twice"

In Erlang I am creating 2 processes linked to each other. If I exit form one process then the other ignores it, if exited normally. This behaviour can be seen documented in a link.

My Question is, on compiling the same code twice I can see the second process also got exit. Here are my sample code:

-module(exitnormal).
-export([f1/0]).

f1() ->
    X = whereis(f2) =/= undefined,
    if
        X -> exit( whereis(f2), shutdown ), timer:sleep(1);
        true -> ""
    end,
    register( f2, spawn_link( fun() -> f2() end )),
    receive
        kill -> { ok, f1 }
    end.

f2() ->
    receive
        kill -> { ok, f2 }
    end.

and I ran it with the following results:

1> c(exitnormal).
{ok,exitnormal}
2> erlang:register( f1, spawn( exitnormal, f1, [] )).
true
3> whereis(f2) ! kill, ok.
ok
4> whereis(f2).
undefined
5> whereis(f1).
<0.40.0>
6> c(exitnormal).
{ok,exitnormal}
7> whereis(f1).
<0.40.0>
8> c(exitnormal).
{ok,exitnormal}
9> whereis(f1).
undefined
10> erlang:register( f1, spawn( exitnormal, f1, [] )).
true
11> whereis(f1) ! kill, ok.
ok
12> whereis(f1).                                      
undefined
13> whereis(f2).           
<0.59.0>
14> c(exitnormal).
{ok,exitnormal}
15> c(exitnormal).
{ok,exitnormal}
16> whereis(f2).
undefined
17> 

Upvotes: 0

Views: 174

Answers (1)

legoscia
legoscia

Reputation: 41648

As described in the Compilation and Code Loading section of the Erlang Reference Manual, a module can have two variants in a running system: "current" and "old". When you reload a module, the new version becomes the "current" one, the one that was previously "current" becomes "old", and the one that was previously "old" is purged. When the old version is purged, any process that still uses it, or has references to funs from the old version, is killed. That's why your processes are killed after reloading the code twice.

This happens even if the module hasn't changed between the two loads; the act of loading will unconditionally trigger the transition.

Upvotes: 2

Related Questions