Question

Here is part of the source code of application master, which is the module managing an erlang application.

348 loop_it(Parent, Child, Mod, AppState) ->
349     receive
350         {Parent, get_child} ->
351             Parent ! {self(), Child, Mod},
352             loop_it(Parent, Child, Mod, AppState);
353         {Parent, terminate} ->
354             NewAppState = prep_stop(Mod, AppState),
355             exit(Child, shutdown),
356             receive
357                 {'EXIT', Child, _} -> ok
358             end,
359             catch Mod:stop(NewAppState),
360             exit(normal);
361         {'EXIT', Parent, Reason} ->
362             NewAppState = prep_stop(Mod, AppState),
363             exit(Child, Reason),
364             receive
365                 {'EXIT', Child, Reason2} ->
366                     exit(Reason2)
367             end,
368             catch Mod:stop(NewAppState);
369         {'EXIT', Child, Reason} -> % forward *all* exit reasons (inc. normal)
370             NewAppState = prep_stop(Mod, AppState),
371             catch Mod:stop(NewAppState),
372             exit(Reason);
373         _ ->
374             loop_it(Parent, Child, Mod, AppState)
375     end.

My question is:

  1. Why is the behaviour of application master different when it receives {Parent, terminate} and {'EXIT', Parent, Reason}?

  2. What does the code in line 368 mean? The application master must have exited before in line 366.

Was it helpful?

Solution

The application master receives 'terminate' when doing a controlled shutdown or restart. When it received any exit signal it does an uncontrolled shutdown.

gen_servers, supervisors, etc log to the error logger if an uncontrolled shutdown happens, if it is controlled then no error reports are issued.

The code as line 368 looks like a copy paste error. The line will never execute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top