user6324692
user6324692

Reputation:

shutdown: failed to start child: MyApp.Endpoint in Phoenix/Elixir

In my phoenix app I have this error on a server and locally when I'm running the Phoenix console:

[info] Application MyApp exited: MyApp.start(:normal, []) returned an error: shutdown: failed to start child: MyApp.Endpoint
    ** (EXIT) already started: #PID<0.1012.0>
{"Kernel pid terminated",application_controller,"{application_start_failure,MyApp,{{shutdown,
{failed_to_start_child,'Elixir.MyApp.Endpoint',
{already_started,<0.1012.0>}}},{'Elixir.MyApp',start,[normal,[]]}}}"}

Crash dump is being written to: erl_crash.dump...done
Kernel pid terminated (application_controller) ({application_start_failure,MyApp,{{shutdown,{failed_to_start_child,'Elixir.MyApp.Endpoint',{already_started,<0.1012.0>}}},{'Elixir.MyApp',start,[no....

What is that about? How to fix it?

UPDATE:

Here's the full obfuscated stack trace:

$ ./bin/my_app console     
Using /home/my_name/my_app_com_webite2/releases/0.0.2/my_app.sh
Exec: /home/my_name/my_app_com_webite2/erts-7.3.1/bin/erlexec -boot /home/my_name/my_app_com_webite2/releases/0.0.2/my_app -mode embedded -config /home/my_name/my_app_com_webite2/running-config/sys.config -boot_var ERTS_LIB_DIR /home/my_name/my_app_com_webite2/erts-7.3.1/../lib -env ERL_LIBS /home/my_name/my_app_com_webite2/lib -pa /home/my_name/my_app_com_webite2/lib/my_app-0.0.2/consolidated -args_file /home/my_name/my_app_com_webite2/running-config/vm.args -user Elixir.IEx.CLI -extra --no-halt +iex -- console
Root: /home/my_name/my_app_com_webite2
/home/my_name/my_app_com_webite2
Erlang/OTP 18 [erts-7.3.1] [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]

[info] Application my_app exited: my_app.start(:normal, []) returned an error: shutdown: failed to start child: my_app.Endpoint
    ** (EXIT) already started: #PID<0.1012.0>
{"Kernel pid terminated",application_controller,"{application_start_failure,my_app,{{shutdown,{failed_to_start_child,'Elixir.my_app.Endpoint',{already_started,<0.1012.0>}}},{'Elixir.my_app',start,[normal,[]]}}}"}

Crash dump is being written to: erl_crash.dump...done
Kernel pid terminated (application_controller) ({application_start_failure,my_app,{{shutdown,{failed_to_start_child,'Elixir.my_app.Endpoint',{already_started,<0.1012.0>}}},{'Elixir.my_app',start,[no

I created the release it by mix clean, compile, release.

UPDATE2:

# lib/my_app
defmodule MyApp123 do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      supervisor(MyApp123.Endpoint, []),
      supervisor(MyApp123.Repo, []),
    ]


    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp123.Supervisor]
    Supervisor.start_link(children, opts)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    MyApp123.Endpoint.config_change(changed, removed)
    :ok
  end
end

Upvotes: 5

Views: 12254

Answers (4)

kigila
kigila

Reputation: 1

You already have another instance of the BEAN/Process in the same port. Just close all the terminal and relunch the server again. Realy simple.

Upvotes: 0

adamscott
adamscott

Reputation: 853

I came across this when I realized I had another app running on the port my phoenix app was trying to run on. Make sure you don't have an app running on the same port you're trying to start this app on.

Upvotes: 1

agix
agix

Reputation: 31

I had the same issue and figured it out it was because I forgot to specify PORT environment variable...

PORT=4000 bin/my_app foreground

Upvotes: 3

tkowal
tkowal

Reputation: 9289

This means that you probably started MyApp.Endpoint manually somewhere. Inside your lib/my_app.ex there should be a piece of code like this.

children = [
  # Start the endpoint when the application starts
  supervisor(MyApp.Endpoint, []),
  # Start the Ecto repository
  supervisor(MyApp.Repo, []),
  # Here you could define other workers and supervisors as children
  # worker(MyApp.Worker, [arg1, arg2, arg3]),
]

This code means that starting MyApp requires starting Endpoint. Supervision trees are really strict about the order. They need to monitor started processes, so they return error if someone else did start the process. This shuts down the MyApp and entire VM, because it doesn't make sense to run it without main app. Try looking for Endpoint.start calls somewhere in your code.

Upvotes: 2

Related Questions