Supervisor.which_children can cause calling process to exit under some circumstances
Environment
$ elixir --version
Erlang/OTP 19 [erts-8.3] [source-d5c06c6] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]
Elixir 1.4.2
$ uname -a
Darwin jfitzpat-mbpr15.xxxx.com 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64
Current behavior
I have noticed that calling Supervisor.which_children/1 can cause the calling process to mysteriously exit.
Consider the following code:
defmodule Supervisor1 do
use Supervisor
def start_link do
IO.puts "Supervisor 1 starting..."
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_args) do
children = [
worker(Worker1, []),
worker(Worker2, []),
supervisor(Supervisor2, []),
supervisor(Supervisor3, [])
]
supervise(children, strategy: :one_for_one)
end
end
defmodule Supervisor2 do
use Supervisor
def start_link do
IO.puts "Supervisor 2 starting..."
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_args) do
children = [
worker(Worker3, [])
]
supervise(children, strategy: :one_for_one)
end
end
defmodule Supervisor3 do
use Supervisor
def start_link do
IO.puts "Supervisor 3 starting..."
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_args) do
children = [
worker(Worker4, [])
]
supervise(children, strategy: :simple_one_for_one)
end
end
Where each worker is defined as follows:
defmodule Worker1 do
use GenServer
def start_link do
_ = IO.puts "Worker 1 starting..."
GenServer.start_link(__MODULE__, name: __MODULE__)
end
end
The following test case will cause the test process to exit:
defmodule WhichChildrenCrashTest do
use ExUnit.Case, async: true
def wait_until(timeout, fun) do
cond do
fun.() -> :ok
timeout <= 0 -> :condition_not_met
true ->
_ = Process.sleep(10)
wait_until(timeout - 10, fun)
end
end
test "supervisor restarts child processes on crash" do
IO.puts "Test process pid is " <> inspect(self())
{:ok, supervisor_pid} = Supervisor1.start_link
IO.puts "Top level supervisor pid is " <> inspect(self())
init_children = Supervisor.which_children(supervisor_pid)
IO.puts "== Child Pids =="
# Create map of child processes
pids = Enum.reduce(init_children, %{}, fn({id, pid, _type, _modules}, map) ->
IO.puts inspect(pid)
Map.put(map, id, pid)
end)
# Monitor each child process
refs = Enum.reduce(pids, %{}, fn({id, pid}, map) ->
Map.put(map, id, Process.monitor(pid))
end)
# Tell each child process to exit
Enum.each(pids, fn({_id, pid}) ->
Process.exit(pid, :kill)
end)
# Ensure each child process exited
Enum.each(pids, fn({id, pid}) ->
ref = Map.get(refs, id)
assert_receive {:DOWN, ^ref, :process, ^pid, :killed}
end)
# Check that all children were restarted within 500 ms by checking that each child has a new pid
assert :ok == wait_until(500, fn ->
children = Supervisor.which_children(supervisor_pid)
cond do
length(children) == length(init_children) ->
Enum.all?(children, fn({id, pid, _type, _modules}) -> Map.get(pids, id) != pid end)
true -> false
end
end)
end
end
An example run showing this behaviour is as follows:
$ mix test --no-start
Test process pid is #PID<0.117.0>
Supervisor 1 starting...
Worker 1 starting...
Worker 2 starting...
Supervisor 2 starting...
Worker 3 starting...
Supervisor 3 starting...
Top level supervisor pid is #PID<0.117.0>
== Child Pids ==
#PID<0.123.0>
#PID<0.121.0>
#PID<0.120.0>
#PID<0.119.0>
Supervisor 2 starting...
Worker 3 starting...
Supervisor 3 starting...
Worker 1 starting...
1) test supervisor restarts child processes on crash (WhichChildrenCrashTest)
test/which_children_crash_test.exs:14
** (EXIT from #PID<0.117.0>) shutdown
Finished in 0.04 seconds
1 test, 1 failure
Randomized with seed 158172
Expected behavior
I expect that calling Supervisor.which_children/1 should never cause the current process to exit.
The problem seems to be related so some combination of workers and supervisors where one supervisor uses the :simple_one_for_one supervision strategy. Specifically, if I change Supervisor1 so that Supervisor3, the supervisor that uses the :simple_one_for_one supervision strategy is not started the test passes as expected:
defmodule Supervisor1 do
use Supervisor
def start_link do
IO.puts "Supervisor 1 starting..."
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_args) do
children = [
worker(Worker1, []),
worker(Worker2, []),
supervisor(Supervisor2, []),
# supervisor(Supervisor3, [])
]
supervise(children, strategy: :one_for_one)
end
end
$ mix test --no-start
Compiling 1 file (.ex)
Test process pid is #PID<0.125.0>
Supervisor 1 starting...
Worker 1 starting...
Worker 2 starting...
Supervisor 2 starting...
Worker 3 starting...
Top level supervisor pid is #PID<0.125.0>
== Child Pids ==
#PID<0.129.0>
#PID<0.128.0>
#PID<0.127.0>
Supervisor 2 starting...
Worker 3 starting...
Worker 1 starting...
Worker 2 starting...
.
Finished in 0.04 seconds
1 test, 0 failures
Randomized with seed 226602
Strangely not starting Supervisor2 or one of the workers will also allow the test to pass. I narrowed down the issue to the call to Supervisor.which_children by inserting calls to that function at various points in the test method. If I comment out the call to the wait_until function and just call Supervisor.which_children/1 the test will fail.
I have attached a zip archive containing my complete project that reproduces the problem.
I didn't dig very deeply into your code, but I suspect the reason could be that when you kill all children in
Enum.each(pids, fn({_id, pid}) ->
Process.exit(pid, :kill)
end)
the top-level supervisor crashes because it has 4 children and the default :max_restarts option is 3. That would explain why taking out Supervisor3 doesn't break (you never reach 4 restarts). Can you try the same code you have now for Supervisor1, but with four worker(WorkerN, []) children and see if it exits anyways?
Thank you! You nailed it. I increased the max_restarts option to 10 and now the test passes as expected. Thanks again.