Supervisors

Start a tree

A supervisor is a process that starts and watches children. start_link/0 starts it, and init/1 answers the restart strategy plus the child specifications. Save these modules as counter.erl and my_sup.erl; the single child is the counter from lesson 34, and restart_counter/0 exists to make the restart visible in a session.

-module(counter). -behaviour(gen_server). -export([start_link/0, increment/0, value/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]). start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, 0, []). increment() -> gen_server:cast(?MODULE, increment). value() -> gen_server:call(?MODULE, value). init(State) -> {ok, State}. handle_call(value, _From, State) -> {reply, State, State}. handle_cast(increment, State) -> {noreply, State + 1}. handle_info(_Msg, State) -> {noreply, State}. terminate(_Reason, _State) -> ok.

-module(my_sup). -behaviour(supervisor). -export([start_link/0, start/0, restart_counter/0, init/1]). start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). start() -> {ok, _Pid} = start_link(), ok. restart_counter() -> Old = whereis(counter), exit(Old, shutdown), wait_for_new(Old, 100). wait_for_new(_Old, 0) -> {error, no_restart}; wait_for_new(Old, Tries) -> case whereis(counter) of Pid when is_pid(Pid), Pid =/= Old -> ok; _ -> timer:sleep(10), wait_for_new(Old, Tries - 1) end. init([]) -> Children = [ #{id => counter, start => {counter, start_link, []}, restart => permanent, shutdown => 5000, type => worker, modules => [counter]} ], {ok, {{one_for_one, 5, 10}, Children}}.

The session turns the logger off first: a killed child makes the supervisor write a crash report to the console, and the report carries a pid and a timestamp that would not replay. Start the tree, move the counter to 1, kill the child, and see the fresh child answer 0.

erlc counter.erl my_sup.erl
erl

1> logger:set_primary_config(level, none).
ok
2> my_sup:start().
ok
3> counter:increment().
ok
4> counter:value().
1
5> my_sup:restart_counter().
ok
6> counter:value().
0

The last answer is 0 because the restarted child begins at its initial state. Nothing carried over, which is the clean slate a restart is supposed to give.

One for one

one_for_one restarts only the child that died, which is the right default for independent workers. one_for_all restarts every child, and rest_for_one restarts the dead child and the ones started after it. The deprecated simple_one_for_one let one template start many identical children; a dynamic supervisor covers that today.

Restart strategy

The restart field says what happens to a child: permanent always restarts, transient restarts only on an abnormal exit, and temporary never restarts. The numbers in {one_for_one, 5, 10} allow five restarts in ten seconds before the supervisor itself gives up. The counter in this tree is permanent, so even the clean shutdown in restart_counter/0 brings it back.

Shutdown strategy

shutdown gives the child time to stop: a timeout in milliseconds, infinity for a slow worker, or brutal_kill when the child has no cleanup. When a child crashes, the supervisor restarts it according to the strategy and the child spec, and the rest of the tree keeps running.

Next: OTP applications

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a supervisor?

    Answer

    A process that starts and watches children

    It starts its children and restarts them according to the child specification.

  2. What does init/1 of a supervisor answer?

    Answer

    A restart strategy and a list of child specs

    The answer is {ok, {Strategy, Children}}, as in {ok, {{one_for_one, 5, 10}, Children}}.

  3. What does one_for_one restart?

    Answer

    Only the child that died

    Independent workers restart alone, so the rest of the tree keeps running.

  4. What does one_for_all do when one child dies?

    Answer

    Restarts every child

    The children form one unit, so all of them start again together.

  5. What does rest_for_one restart?

    Answer

    The dead child and the ones started after it

    Later children may depend on the dead one, so they restart after it.

  6. What does restart => permanent mean?

    Answer

    Always restart

    A permanent child is always brought back, even after a clean shutdown.

  7. What does restart => transient mean?

    Answer

    Restart only on an abnormal exit

    A clean exit is accepted; a crash is restarted.

  8. What do the numbers in {one_for_one, 5, 10} mean?

    Answer

    Five restarts in ten seconds at most

    The supervisor gives up when it has to restart more than five times in ten seconds.

  9. What does shutdown => 5000 give the child?

    Answer

    Five seconds to stop

    The child gets 5000 milliseconds to shut down before it is killed.

  10. Why does counter:value() answer 0 after my_sup:restart_counter()?

    Answer

    The restarted child starts from its initial state

    A restart creates a new process with init(0), so nothing of the old state survives.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev