I am reading the Little Elixir and OTP Guidebook to learn elixir, there is a chapter in the book in which the author builds a Supervisor from scratch. There is a part where the author implements a function to terminate the Supervisor process which in turn also terminates all the child processes linked to it before terminating itself.
As far as I understand if you have processes linked to a particular process and if one of them is killed then all of them are killed right? So why do we need to kill all the child procs before killing the supervisor, is it just a best practice?
This is what the book says btw
Before you terminate the supervisor process, you need to terminate all the children it’s linked to, which is handled by the terminate_children/1 private function,
defmodule ThySupervisor do
use GenServer
######################
# Callback Functions #
######################
def terminate(_reason, state) do
terminate_children(state)
:ok
end
#####################
# Private Functions #
#####################
defp terminate_children([]) do
:ok
end
defp terminate_children(child_specs) do
child_specs |> Enum.each(fn {pid, _} -> terminate_child(pid) end)
end
defp terminate_child(pid) do
Process.exit(pid, :kill)
:ok
end
end