How to kill supervisor when linked process is killed?

Hello!

In my application, I have two apps called market_manager and candle_manager.

In app candle_manager, I have this tree structure:


In app market_manager, I have this one:
02
MarketManager.ManagersSupervisor is a dynamic supervisor that can start managers. When I start one Manager, I get this:

Also, the manager that I started will start a new child in CandleManager.Bitfinex.RealtimeSupervisor called CandleManager.Bitfinex.Realtime.Supervisor:

CandleManager.Bitfinex.Realtime.Supervisor is started with :temporary restart, and its PID is linked to the Manager one via Process.link.

So, for example, if Manager has PID <0.612.0> and CandleManager.Bitfinex.Realtime.Supervisor PID <0.613.0> there will be a link between these two (I can confirm via :observer).

Now, if I go to :observer and kill CandleManager.Bitfinex.Realtime.Supervisor, then Manager will receive the exit message and kill itself too (as expected).

But, if I kill the Manager instead, CandleManager.Bitfinex.Realtime.Supervisor is not killed and Manager will fail to restart since it will try to start a new CandleManager.Bitfinex.Realtime.Supervisor and fail with :already_started error.

So, my question is, does Process.link work correctly with Supervisors? How can I make CandleManager.Bitfinex.Realtime.Supervisor kill itself when Manager is killed?

Just for completeness, here is CandleManager.Bitfinex.Realtime.Supervisor code, it doesn’t have anything fancy that I can see breaking the Process.link logic.

defmodule CandleManager.Bitfinex.Realtime.Supervisor do
  use Supervisor

  alias CandleManager.Bitfinex.Realtime

  require Logger
  def start_link(args) do
    Supervisor.start_link(__MODULE__, args, name: __MODULE__)
  end

  @impl Supervisor
  def init([market_manager_pid: _pid] = args) do
    childrens = [
      {Realtime.Websocket.Server, []},
      {Realtime.Manager.Server, []},
      {Realtime.TradeToCandle.Server, []},
      {Realtime.TradeDiscarder.Server, args}
    ]

    Supervisor.init(childrens, strategy: :one_for_all, max_restarts: 0)
  end
end

Thanks!

I see some problems with this design…

Supervisor are trapping exit signal, so they don’t have to shutdown when they receive an Exit signal from children. And they are linked to their children if started with start_link.

I see You give the responsability to start Supervisor to Manager, but You do it in a way it can fail if it is already started, this should not happen, You need to wrap the start of supervisor in a case statement.

Killing a supervisor is not something I would want to do programatically, and there is no reason to kill it if Manager dies.

You should solve this

fail with `:already_started` error.

instead of trying to kill bill :slight_smile:

Thanks for the answer @kokolegorille!

You are right, I don’t need to kill the supervisor, but I do need to kill all it’s child’s because I need to restart their state, that is why I created that supervisor, so I could kill only it and all the children’s would be killed to automatically.

Do you know a better solution for that? I can, for example, send the Manager pid to one of the supervisor children and link with then, that way when they are killed, the supervisor will die too (since I set max_restarts to 0), buy that seemed to me like a workaround.

Have you consider the one_for_all strategy?

https://hexdocs.pm/elixir/Supervisor.html#module-strategies

1 Like

That is a good advice, set Manager and Supervisor under a one_for_all strategy… or rest_for_one

The reason I tried doing that way is to separate the processes into smaller apps instead of having a large nested single app tree.

I have read once somewhere that I should not create too large supervision trees.

Do you guys think it is ok to design it into a single app instead of separating then into smaller ones?

Thanks for your answers so far