stevensonmt

stevensonmt

In the Elixir for Programmers course by @pragdave he asks the student to come up with a means of monitoring when children of a dynamic supervisor “goes away”. By “goes away” I assume that means exits gracefully or the node crashes out. I can easily monitor when nodes connect by implementing handle_info and when the the call to DynamicSupervisor.start_child occurs I follow it with a call to send(pid, DynamicSupervisor.which_children(my_supervisor_name)). I don’t yet have a graceful exit implemented for the game so right now I only have to handle when the node crashes out. Since this occurs without a message being sent to the supervisor I don’t know how to trigger a reaction to the event. Any suggestions?

===========
Actually it occurs to me that I might not have been killing or exiting the node in a reasonable way. I’m starting the supervisor in an IEX session, then in another terminal starting the child process. Then I ctrl-c to kill the IEX session of the child process. When running the observer on the supervisor I do not see any message coming into the supervisor, but the child process PID persists. So maybe as far as the VM is concerned nothing has happened?
The information in the observer for the child processes that are no longer active gives “status waiting” in Process Information tab but in the State tab “status” is “running” and Logged Events is an empty list.

Showing Posts 1 to 10

joaoevangelista

joaoevangelista

Disclamer: I have no idea if it is a correct usage.

What you could do is monitor the child when you start it

{:ok, child} = DynamicSupervisor.start_child(MyDynSupervisor, child_spec)
Process.monitor(child)

Now when you kill the child the process that is monitoring, it will receive a {:DOWN, ref, :process, object, reason} message that you can act upon.
So to wrap this behaviour you could introduce a GenServer that delegates start child to the underlying supervisor, and monitors the children.

Example
defmodule MyListener do

use GenServer

  @doc false
  def start_link do
    GenServer.start_link(__MODULE__, nil, name: MyListener)
  end

  @impl true
  def init(_) do
    DynamicSupervisor.start_link(name: MySup, strategy: :one_for_one)
  end


  def start_child(server, child_spec) do
    GenServer.cast(server, {:start_child, child_spec})
  end


  def children(server) do
    GenServer.call(server, :children)
  end

  def terminate_child(server, child) do
    GenServer.cast(server, {:terminate, child})
  end

  @impl true
  def handle_cast({:start_child, child_spec}, dyn) do
    {:ok, child} = DynamicSupervisor.start_child(dyn, child_spec)
    Process.monitor(child)
    {:noreply, dyn}
  end

  def handle_cast({:terminate, child}, dyn) do
    DynamicSupervisor.terminate_child(dyn, child)
    {:noreply, dyn}
  end

  @impl true
  def handle_call(:children, _from, dyn) do
    children = DynamicSupervisor.which_children(dyn)
    {:reply, children, dyn}
  end

  @impl true
  def handle_info(msg, dyn) do
    IO.inspect(msg)
    {:noreply, dyn}
  end
end

{:ok, server} = MyListener.start_link()
MyListener.start_child(server, {Agent, fn -> %{} end})
pids = for {_, pid, _, _} <- MyListener.children(server), do: pid
MyListener.terminate_child(server, Enum.random(pids))

You can paste the example on a iex session

stevensonmt

stevensonmt OP

Thanks but that doesn’t seem to work. The child process persists with no message sent to the supervisor.

When the observer is running and I kill the child-spawning IEX session, I see a “Node node_name down” message box, but the processes it was running do not change. I’d like my supervisor to be able to receive that “node down” message and respond to it.

joaoevangelista

joaoevangelista

The child will persist, since the supervisor is monitoring and restarting it when it dies.
The supervisor will not get a message that you can act upon, thats why we setup a monitor on another Process, to be notified when it dies.
To terminate a child, without the supervisor restarting it you need to call DynamicSupervisor.terminate_child/2 (see updated example above)
This will make that your GenServer gets a message on the handle_info callback that you can act upon

joaoevangelista

joaoevangelista

Nodes are VMs, if you start two iex sessions that is two nodes (vms)

stevensonmt

stevensonmt OP

Sorry for being so slow to get it. Thank you for your help. I understand the two nodes are separate VMs, I was simply pointing out that the VM running the parent process was getting some sort of message when the VM running the child process went down. If the observer can see that message I assume there is a way to read that message for my application as well.
I also appreciate you pointing out terminate_child/2 but my problem is figuring out how to trigger calling that.

stevensonmt

stevensonmt OP

Actually I think I just realized what is wrong with my thinking. Both processes are running on the same VM. The client node can disconnect but the game process persists.

I think I need to send the pid of the client node process that calls for the game to start.
Right now the client does this to start a new game:

def connect() do 
  :rpc.call(@my_game_server, MyGame, :new_game, [])
end

and then MyGame.new_game/0 calls the server’s start_game/0 function that is under the supervisor.
I think I need MyGame.new_game/1 that takes a pid from the client via :rpc.call(@my_game_server, MyGame, :new_game, [self()]) and then I can have Server.start_game/1 that takes that pid and calls Process.monitor(pid).

Maybe.

joaoevangelista

joaoevangelista

Oh so you are also dealing with distribution, you could start the game on one node and call functions of it using rpc, yes. If you need to see if the client node disconnects from the game node, you’ll need to use Node.monitor/2. I thought you wanted to log when a child of the dynamic supervisor exited.

stevensonmt

stevensonmt OP

Yes, I was confused and asked the wrong question.

stevensonmt

stevensonmt OP

Is there a way to tell the Supervisor to monitor the Node? When calling Node.monitor/2 in my start_game/0 function I don’t think the Supervisor is monitoring but rather the parent of the Supervisor. How do I find which process is doing the monitoring?

joaoevangelista

joaoevangelista

You can use a genserver to monitor the Node, the process where you call Node.monitor/2 will be the one responsible to monitoring it. If you call on a GenServer callback it will be the GenServer, then you can listen for messages on handle_info callback.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews