dimitarvp

dimitarvp

Why doesn't a Supervisor worker gets auto-restarted after `Supervisor.terminate_child` is called on it?

I am writing my own job pool library lately and I’ve stumbled upon something I didn’t expect.

It sets up a typical Supervisor with several GenServer workers attached to it. Supervisor.which_children works fine, Process.registered shows both the supervisor and all workers and I can use Process.whereis and Process.info on their names. I can send messages to each worker and it responds fine.

Doing Process.exit(worker_pid, :kill) results in the supervisor restarting the worker as expected. (Both the supervisor and the workers have the restart: :permanent option explicitly specified, too.)

What does surprise me however is that calling Supervisor.terminate_child leaves the worker’s child spec in the supervisor (meaning Supervisor.which_children still shows it but with an :undefined PID)… and the worker process is NOT restarted. The docs of Supervisor.terminate_child don’t seem to address this. I’d expect the normal OTP auto-restart guarantees to apply.

Any clues or pointers? As a future library author I’d be worried that my users can just find the workers via Process.registered and stop them via Supervisor.terminate_child and then have their job pool execution code crash because one or more of the worker processes are not there.

I mean, if they want to use the library they should refrain from such shenanigans, obviously, but is there a way to make sure the workers are restarted even when stopped with Supervisor.terminate_child?

Marked As Solved

lud

lud

If you explicitely ask the supervisor to terminate a child I find it logical that it does not restart it. You may rather use GenServer.stop or other means to tell the child to terminate, in which case the supervisor will do its job.

You can see the supervisor children specs as “the desired state of the application” and the supervisor job is to make sure that the actual state matches the desired state. Supervisor.terminate_child is a way to change the desired state.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement