diff --git a/lib/trinity/scheduler.ex b/lib/trinity/scheduler.ex index f6cfc05..a7a2ca4 100644 --- a/lib/trinity/scheduler.ex +++ b/lib/trinity/scheduler.ex @@ -225,8 +225,9 @@ defmodule Trinity.Scheduler do :ok end - def handle_down(%Simulation{} = sim, _pid, :normal) do + def handle_down(%Simulation{} = sim, pid, :normal) do %{queue: queue, proc_queue_keys: proc_queue_keys} = sim + destroy_process(sim, pid) perform_next(queue, proc_queue_keys, sim.now) [] end @@ -235,9 +236,13 @@ defmodule Trinity.Scheduler do %{queue: queue, proc_queue_keys: proc_queue_keys, proc_links: proc_links} = sim linked = gather_linked(pid, proc_links) - Enum.each(linked, fn pid -> - destroy_process(sim, pid) - Process.exit(pid, reason) + destroy_process(sim, pid) + Enum.each(linked, fn lp -> + destroy_process(sim, lp) + Process.exit(lp, reason) + receive do + {:EXIT, ^lp, _reason} -> :noop + end end) perform_next(queue, proc_queue_keys, sim.now) diff --git a/lib/trinity/scheduler/simulation_supervisor.ex b/lib/trinity/scheduler/simulation_supervisor.ex index ebb0f1c..4138137 100644 --- a/lib/trinity/scheduler/simulation_supervisor.ex +++ b/lib/trinity/scheduler/simulation_supervisor.ex @@ -97,25 +97,18 @@ defmodule Trinity.Scheduler.SimulationSupervisor do exit(reason) ^root_pid -> - # The root has died, end the simulation + # The root died, end the simulation end_simulation(state, reason) {:noreply, state} _ -> killed = Trinity.Scheduler.handle_down(sim, from, reason) - Enum.each(killed, fn pid -> - # Flush exit messages for (simulated) linked processes - receive do - {:EXIT, ^pid, _reason} -> :noop - end - - # If the root was killed, end the simulation - case pid == root_pid do - true -> end_simulation(state, reason) - _ -> :noop - end - end) + case root_pid in killed do + # The root was linked and therefore killed, end the simulation + true -> end_simulation(state, reason) + _ -> :noop + end {:noreply, state} end