sezaru

sezaru

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:
03
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!

First 5 of 5 Posts! Switch mode

kokolegorille

kokolegorille

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:

sezaru

sezaru

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.

NobbZ

NobbZ

Have you consider the one_for_all strategy?

kokolegorille

kokolegorille

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

sezaru

sezaru

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

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement