with

with

Hi, I have a DynamicSupervisor spawning 20 child processes. They work fine for a few minutes, and then all of them terminate unexpectedly. The supervisor doesn’t restart the processes even though I have the restart option set to :permanent. I’ve trapped exits of the child process, and indeed the terminate callback is receiving a :shutdown message. None of my code could potentially be shutting down the child processes, let alone all 20 of them simultaneously. All other GenServers are untouched, the parent process keeps running.

What could be causing such weird behaviour, and how can I get the DynamicSupervisor to restart its children?

The application is database-heavy, the workers constantly write to the database, hundreds inserts per second each. Perhaps this is somehow related? The child processes interact directly with Ecto with no intermediary processes.

defmodule App.Worker do
  use GenServer, restart: :permanent
  def start_link(args), do: GenServer.start_link(__MODULE__, args)

  def start_new_worker() do
    {:ok, pid} =
      DynamicSupervisor.start_child(
        App.WorkerSupervisor,
        {__MODULE__, :no_args}
      )

    pid
  end

  @impl true
  def terminate(reason, state) do
    Logger.info("worker terminating, reason: #{inspect reason}")
  end
end

application.ex:

defmodule App.Application do
  use Application

  def start(_type, _args) do
    children = [
      {DynamicSupervisor,
       name: App.WorkerSupervisor, strategy: :one_for_one}
    ]

    opts = [strategy: :one_for_one, name: App.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Thank you!

Showing Posts 1 to 4

kokolegorille

kokolegorille

Can You please provide your dynamic supervisor code?
Why do You have start_new_worker inside GenServer? (Instead of DynamicSupervisor)
Where do You trap exit? (It’s not the terminate function)
It looks a bit strange not to use name (via Registry)

There is an example of migrating from simple_one_for_one to dynamic supervisor.

BTW it looks like You are linking your workers together, and if one die, they all die :slight_smile:

al2o3cr

al2o3cr

Do any of the worker processes crash in normal operation? The behavior you’re describing sounds like what I’d expect if some workers were crashing and tripped the max_restarts circuit breaker in DynamicSupervisor - which terminates the supervisor and all of its children. In that situation, App.Supervisor will restart App.WorkerSupervisor but it won’t have any children.

with

with OP

Thank you, this did the trick! I’ve set max_restarts: 1_000_000_000, and the processes no longer restart. No, none of them crash, although they stop constantly via {:stop, :normal, state}. Seems like the DynamicSupervisor didn’t like the constant normal shutdowns.

ityonemo

ityonemo

Iirc if you’re shutting down normally you should use restart: :transient in your supervisor spec, instead of setting your max_restarts really high. (You currently have restart: :permanent which is usually for long lived service processes and the like)

Also unless you truly need to respond to messages, such temporary tasks that live in their own processes should probably be Tasks supervised by a Task.Supervisor, not a GenServer.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews