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

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement