Fl4m3Ph03n1x

Fl4m3Ph03n1x

Supervisor.start_link/2 documentation incomplete?

Background

I am trying to build an OTP compliant app using supervisors. However I am having trouble interpreting a part of the documentation.

Docs

The documentation states that Supervisor.start_link/2 returns the following:

{:ok, pid}
| :ignore 
| {:error, {:already_started, pid} | {:shutdown,term} | [term]}

Problem

The problem is that even though the documentation specifies when start_link returns {:ok, pid} and {:error, any} it doesn’t specify when it returns :ignore, leaving me to think that it can never return :ignore.

Question

In which cases does Supervisor.start_link/2 return :ignore?

Marked As Solved

LostKobrakai

LostKobrakai

When the supervisor itself (it’s init/1) returns :ignore then Supervisor.start_link returns :ignore.

E.g.

defmodule MyApp.Supervisor do
  # Automatically defines child_spec/1
  use Supervisor

  def start_link(init_arg) do
    Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl true
  def init(_init_arg) do
    :ignore
  end
end

Supervisor.start_link(MyApp.Supervisor, [])
# :ignore

Also Liked

LostKobrakai

LostKobrakai

It surely can if at least for the Supervisor.start_link/3 version, but skipping the optional options, which are the 3rd parameter.

LostKobrakai

LostKobrakai

https://hexdocs.pm/elixir/Supervisor.html#c:init/1

If init/1 returns :ignore then start_link will return :ignore. This is useful if you want to selectively start certain parts of your supervision tree / workers.

It’s explicitly documented here https://hexdocs.pm/elixir/GenServer.html#c:init/1

peerreynders

peerreynders

The init/1 callback is allowed to return {:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}} | :ignore

[Erlang] Designing for Scalability with Erlang/OTP (O'Reilly) p.81:

If the startup fails but you do not want to affect other processes started by the same supervisor, return ignore.

p.178

The init/1 callback function normally returns the whole tuple comprising the restart tuple and a list of child specifications. But if it instead returns ignore, the supervisor terminates with reason normal.

LostKobrakai

LostKobrakai

A supervisor has an init/1 callback as well as workers have. So both actually work the same in regards to returning :ignore from that callback.

sasajuric

sasajuric

Author of Elixir In Action

I believe you actually got it right initially. It seems that Supervisor.start_link/2 can’t return :ignore (unless I’m missing some nuance, since I didn’t bother to read the code of Supervisor). Even the docs for start_link/2 don’t mention it.

You can consider making a PR which would tighten the specification for start_link/2.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement