1
votes

From the example given here, Erlang and process_flag(trap_exit, true)

-module(play).
-compile(export_all).

start() ->
    process_flag(trap_exit, true),
    spawn_link(?MODULE, inverse, [***0***]),
    loop().

loop() ->
    receive
        Msg -> io:format("~p~n", [Msg])
    end,
    loop().

inverse(N) -> 1/N.

If I run it as,

A = spawn(play, start, []).

The spawned process <0.40.0> dies as it is suppose to but the main process (A <0.39.0>) which spawned it doesn't die.

{'EXIT',<0.40.0>,{badarith,[{play,inverse,1,[{file,"play.erl"},{line,15}]}]}}
<0.39.0>
i().
....
....
<0.39.0>              play:start/0                           233       19    0
                  play:loop/0                              1              

A does get an exit signal (not an exit message since A is not trapping exit) then why doesn't it exit?

1
What version of Erlang/OTP are you using? On 17.4, spawn_link(erlang, div, [1, 0]) kills the shell process that spawned it. It's possible that in later (or earlier) versions they added exit trapping to shell processes. - Soup d'Campbells
erl -v Erlang/OTP 17 [erts-6.2] [source-aaaefb3] [64-bit] [smp:4:4] [async- threads:10] [hipe] [kernel-poll:false] Eshell V6.2 (abort with ^G) - Vishal
Shell is killed by div by 0 for me as well 2> self(). <0.36.0> 3> spawn_link(fun() -> 1/0 end). =ERROR REPORT==== 5-Jun-2015::16:21:47 === Error in process <0.39.0> with exit value: {badarith,[{erlang,'/',[1,0],[]}]} ** exception exit: badarith in operator '/'/2 called as 1 / 0 4> self(). <0.40.0> 5> - Vishal

1 Answers

4
votes

The reason for this is that you set a trap_exit flag to true which means this process will get {'EXIT', FromPid, Reason} message instead of being killed. Just remove process_flag(trap_exit, true) or in case of receiving this type of message, kill it.

You can read about it here.