ltsigauke

ltsigauke

What handles the 'call' request in Elixir's supervisor after start_child

I’ve been reading through the code for Elixir supervisors for my own curiosity; when start_child is called in the code it then passes through to the function ‘call’ and that in turn does a GenServer.call with whatever arguments I have passed; my questions now being; the module passed as ‘supervisor’ doesn’t implement a handle call and the macro invoked when you ‘use Supervisor’ doesn’t inject anything for this request, so what handles this request for start_child or any of the calls in the module; the only occurrence of start_child are those three lines around the one I just linked to so I’m a little bit dumbfounded by this.

Marked As Solved

peerreynders

peerreynders

Recall that Elixir’s code builds on top of OTP’s supervisor. So I suspect that the handle_calls that you are looking for are here.

Also Liked

sasajuric

sasajuric

Author of Elixir In Action

As a small addendum, you could also use tracing to discover the module of which :handle_call is invoked. Here’s a small script which proves that :supervisor is indeed that module:

# start a supervisor
{:ok, supervisor} = Supervisor.start_link([], strategy: :one_for_one)

# trace function calls in supervisor
:erlang.trace(supervisor, true, [:call])
:erlang.trace_pattern({:_, :_, :_}, [])

# start a child
Supervisor.start_child(supervisor, %{
  id: :child,
  start: {Agent, :start_link, [fn -> nil end]}
})

# Pick up all traces, and print the first `:handle_call`
Stream.repeatedly(fn ->
  receive do
    x -> x
  end
end)
|> Stream.filter(&match?({:trace, _pid, :call, {_mod, :handle_call, _args}}, &1))
|> Enum.take(1)
|> hd()
|> IO.inspect()

# Output:
#
# {:trace, #PID<0.76.0>, :call,
#  {:supervisor, :handle_call,
#   [
#     {:start_child,
#      %{
#        id: :child,
#        start: {Agent, :start_link, [#Function<0.1373404 in file:test.exs>]}
#      }},
#     {#PID<0.73.0>, #Reference<0.4263660754.4251713537.141908>},
#     {:state, {#PID<0.76.0>, Supervisor.Default}, :one_for_one, [], :undefined,
#      3, 5, [], 0, Supervisor.Default,
#      {:ok, {%{intensity: 3, period: 5, strategy: :one_for_one}, []}}}
#   ]}}

Where Next?

Popular in Questions Top

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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement