Supervisor.start_child error :undef?

I’m having trouble getting Supervisor.start_child to work and I get the following error when running these commands (the supervisor is started automatically. Any help is appreciated. I’m likely missing something simple.

import Supervisor.Spec
sup_pid = sup_pid = Process.whereis(App.SessionSupervisor)
Supervisor.start_child(sup_pid,["a"])

{:error,
   {:EXIT,
    {:undef,
      [{App.Session, :start_link, [["a"]], []},
      {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 374]},
      {:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 399]},
      {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 629]},
      {:gen_server, :handle_msg, 5, [file: 'gen_server.erl', line: 661]},
      {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 240]}]}}}

session_supervisor.ex

defmodule App.SessionSupervisor do
  use Supervisor

  def start_link do
    Supervisor.start_link(__MODULE__, :ok, name: App.SessionSupervisor)
  end

  def init(:ok) do
    children = [worker(App.Session, [], restart: :transient)]
    worker(App.Session, [], restart: :transient)

    supervise(children, strategy: :simple_one_for_one)
  end
end

session.ex

defmodule App.Session do

  use GenServer

  @doc """
  Starts a new session.
  """
  def start_link(uuid, opts \\ []) do
    GenServer.start_link(__MODULE__, %{uuid: uuid})
  end

… rest of the code.

Hi Alan, bumped into this just a few hours ago myself. The docs on this are terrible at telling you what you should provide. (Thankfully, that is a rarity!)

In my case, I had printed a start_link/1 function, but I needed to provide a start_link/2 function. Took me a while to get that right.

In your code, in see three different models name prefixes: App, Jaeger, and Jeager. Perhaps this is as simple as a typo?

1 Like

Thanks for the reply.

The different models were just failed attempts at obfuscating/generalizing the code after initially pasting it in. They are now changed to all be App. I went back and double checked all of the code and it isn’t a typo.

I thought I had a start_link/2 function but perhaps I’m missing something in the proper parameters

Are you sure that App.Session is compiled AND loaded?

  1. Take a look at the _build/$MIX_ENV-folder and search for a file named Elixir.App.Session.beam, if it is not there than your code doesn’t get compiled.
  2. If that file does exist, please make sure your application and all of its dependencies are starting up correctly.

@Nobbz: Thanks for the debugging tip… that was new learning for me.

the file is there and I’m able to start the session Genserver from iex -S mix

{:ok, pid} = App.Session.start_link("uuid", [])
{:ok, #PID<0.468.0>}

Running :observer.start I can see Elixir.App.Supervisor -> Elixir.App.SessionSupervisor. I don’t know if it is possible to inspect models from :observer.

Fix found - formatting the params for start_link as

["uuid", []]

vs

"uuid", []

allows it to properly started a linked child process

>iex -S mix
Erlang/OTP 18 [erts-7.3] [source-d2a6d81] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> sup_pid = Process.whereis(App.SessionSupervisor)
#PID<0.304.0>
iex(2)> Supervisor.count_children(sup_pid)
%{active: 0, specs: 1, supervisors: 0, workers: 0}
iex(3)> {:ok, pid} = Supervisor.start_child(sup_pid, ["uuid", []])
{:ok, #PID<0.311.0>}
iex(4)> Supervisor.count_children(sup_pid)
%{active: 1, specs: 1, supervisors: 0, workers: 1}

Thanks @NobbZ and @scouten for offering good pointers.

1 Like

In my case, I had printed a start_link/1 function, but I needed to provide a start_link/2 function.

Because there are a lot of start_link functions in this example, I thought I’d mention that you want start_link/2 in the GenServer supervised by your DynamicSupervisor (App.Session in the above example).

For those who might run into this problem, in my case it was a typo in the module name.

child_spec = %{
  id: WrongModuleName, # << typo here!
  start: {WrongModuleName, :start_link, [arg]},
  restart: :transient
}

case DynamicSupervisor.start_child(__MODULE__, child_spec) do
  {:error, {:already_started, pid}} -> {:ok, pid}
  {:ok, pid} -> {:ok, pid}
end