The supervisor is responsible for starting, stopping, and monitoring its child processes. The basic idea
of a supervisor is that it must keep its child processes alive by restarting them when necessary.
The children of a supervisor are defined as a list of childspecifications. When the supervisor is
started, the child processes are started in order from left to right according to this list. When the
supervisor terminates, it first terminates its child processes in reversed start order, from right to
left.
Supervisorflags
The supervisor properties are defined by the supervisor flags. The type definition for the supervisor
flags is as follows:
sup_flags() = #{strategy => strategy(), % optional
intensity => non_neg_integer(), % optional
period => pos_integer(), % optional
auto_shutdown => auto_shutdown()} % optional
RestartStrategies
A supervisor can have one of the following restartstrategies specified with the strategy key in the
above map:
* one_for_one - If one child process terminates and is to be restarted, only that child process is
affected. This is the default restart strategy.
* one_for_all - If one child process terminates and is to be restarted, all other child processes are
terminated and then all child processes are restarted.
* rest_for_one - If one child process terminates and is to be restarted, the 'rest' of the child
processes (that is, the child processes after the terminated child process in the start order) are
terminated. Then the terminated child process and all child processes after it are restarted.
* simple_one_for_one - A simplified one_for_one supervisor, where all child processes are dynamically
added instances of the same process type, that is, running the same code.
Functions delete_child/2 and restart_child/2 are invalid for simple_one_for_one supervisors and
return {error,simple_one_for_one} if the specified supervisor uses this restart strategy.
Function terminate_child/2 can be used for children under simple_one_for_one supervisors by
specifying the child's pid() as the second argument. If instead the child specification identifier is
used, terminate_child/2 return {error,simple_one_for_one}.
As a simple_one_for_one supervisor can have many children, it shuts them all down asynchronously.
This means that the children do their cleanup in parallel, and therefore the order in which they are
stopped is not defined.
Restartintensityandperiod
To prevent a supervisor from getting into an infinite loop of child process terminations and restarts, a
maximumrestartintensity is defined using two integer values specified with keys intensity and period in
the above map. Assuming the values MaxR for intensity and MaxT for period, then, if more than MaxR
restarts occur within MaxT seconds, the supervisor terminates all child processes and then itself. The
termination reason for the supervisor itself in that case will be shutdown. intensity defaults to 1 and
period defaults to 5.
AutomaticShutdown
A supervisor can be configured to automatically shut itself down with exit reason shutdown when
significant children terminate with the auto_shutdown key in the above map:
* never - Automic shutdown is disabled. This is the default setting.
With auto_shutdown set to never, child specs with the significant flag set to true are considered
invalid and will be rejected.
* any_significant - The supervisor will shut itself down when any significant child terminates, that
is, when a transient significant child terminates normally or when a temporary significant child
terminates normally or abnormally.
* all_significant - The supervisor will shut itself down when all significant children have terminated,
that is, when the lastactive significant child terminates. The same rules as for any_significant
apply.
For more information, see the section Automatic Shutdown in Supervisor Behavior in OTP Design Principles.
Warning:
The automatic shutdown feature appeared in OTP 24.0, but applications using this feature will also
compile and run with older OTP versions.
However, such applications, when compiled with an OTP version that predates the appearance of the
automatic shutdown feature, will leak processes because the automatic shutdowns they rely on will not
happen.
It is up to implementors to take proper precautions if they expect that their applications may be
compiled with older OTP versions.
Childspecification
The type definition of a child specification is as follows:
child_spec() = #{id => child_id(), % mandatory
start => mfargs(), % mandatory
restart => restart(), % optional
significant => significant(), % optional
shutdown => shutdown(), % optional
type => worker(), % optional
modules => modules()} % optional
The old tuple format is kept for backwards compatibility, see child_spec(), but the map is preferred.
* id is used to identify the child specification internally by the supervisor.
The id key is mandatory.
Notice that this identifier on occations has been called "name". As far as possible, the terms
"identifier" or "id" are now used but to keep backward compatibility, some occurences of "name" can
still be found, for example in error messages.
* start defines the function call used to start the child process. It must be a module-function-
arguments tuple {M,F,A} used as apply(M,F,A).
The start function mustcreateandlinkto the child process, and must return {ok,Child} or
{ok,Child,Info}, where Child is the pid of the child process and Info any term that is ignored by the
supervisor.
The start function can also return ignore if the child process for some reason cannot be started, in
which case the child specification is kept by the supervisor (unless it is a temporary child) but the
non-existing child process is ignored.
If something goes wrong, the function can also return an error tuple {error,Error}.
Notice that the start_link functions of the different behavior modules fulfill the above
requirements.
The start key is mandatory.
*
restart defines when a terminated child process must be restarted. A permanent child process is
always restarted. A temporary child process is never restarted (even when the supervisor's restart
strategy is rest_for_one or one_for_all and a sibling's death causes the temporary process to be
terminated). A transient child process is restarted only if it terminates abnormally, that is, with
another exit reason than normal, shutdown, or {shutdown,Term}.
The restart key is optional. If it is not specified, it defaults to permanent.
*
significant defines if a child is considered significant for automatic self-shutdown of the
supervisor.
Setting this option to true when the restart type is permanent is invalid. Also, it is considered
invalid to start children with this option set to true in a supervisor when the auto_shutdown
supervisor flag is set to never.
The significant key is optional. If it is not specified, it defaults to false.
* shutdown defines how a child process must be terminated. brutal_kill means that the child process is
unconditionally terminated using exit(Child,kill). An integer time-out value means that the
supervisor tells the child process to terminate by calling exit(Child,shutdown) and then wait for an
exit signal with reason shutdown back from the child process. If no exit signal is received within
the specified number of milliseconds, the child process is unconditionally terminated using
exit(Child,kill).
If the child process is another supervisor, the shutdown time must be set to infinity to give the
subtree ample time to shut down.
Warning:
Setting the shutdown time to anything other than infinity for a child of type supervisor can cause a
race condition where the child in question unlinks its own children, but fails to terminate them before
it is killed.
It is also allowed to set it to infinity, if the child process is a worker.
Warning:
Be careful when setting the shutdown time to infinity when the child process is a worker. Because, in
this situation, the termination of the supervision tree depends on the child process, it must be
implemented in a safe way and its cleanup procedure must always return.
Notice that all child processes implemented using the standard OTP behavior modules automatically
adhere to the shutdown protocol.
The shutdown key is optional. If it is not specified, it defaults to 5000 if the child is of type
worker and it defaults to infinity if the child is of type supervisor.
* type specifies if the child process is a supervisor or a worker.
The type key is optional. If it is not specified, it defaults to worker.
* modules is used by the release handler during code replacement to determine which processes are using
a certain module. As a rule of thumb, if the child process is a supervisor, gen_server or,
gen_statem, this is to be a list with one element [Module], where Module is the callback module. If
the child process is an event manager (gen_event) with a dynamic set of callback modules, value
dynamic must be used. For more information about release handling, see Release Handling in OTP
Design Principles.
The modules key is optional. If it is not specified, it defaults to [M], where M comes from the
child's start {M,F,A}.
* Internally, the supervisor also keeps track of the pid Child of the child process, or undefined if no
pid exists.