My app crashes at the start

All of a sudden my app has started to crash:

    $ mix phx.server
    [................]

    Generated my_app app
    [info] Application my_app exited: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: MyAppWeb.Endpoint
        ** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Handler
            ** (EXIT) an exception was raised:
                ** (UndefinedFunctionError) function Phoenix.Endpoint.Cowboy2Handler.child_spec/3 is undefined (module Phoenix.Endpoint.Cowboy2Handler is not available)
                    Phoenix.Endpoint.Cowboy2Handler.child_spec(:http, MyAppWeb.Endpoint, [port: 4000, otp_app: :my_app])
                    (phoenix) lib/phoenix/endpoint/handler.ex:33: anonymous fn/5 in Phoenix.Endpoint.Handler.init/1
                    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
                    (phoenix) lib/phoenix/endpoint/handler.ex:31: Phoenix.Endpoint.Handler.init/1
                    (stdlib) supervisor.erl:295: :supervisor.init/1
                    (stdlib) gen_server.erl:374: :gen_server.init_it/2
                    (stdlib) gen_server.erl:342: :gen_server.init_it/6
                    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
    ** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: MyAppWeb.Endpoint
        ** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Handler
            ** (EXIT) an exception was raised:
                ** (UndefinedFunctionError) function Phoenix.Endpoint.Cowboy2Handler.child_spec/3 is undefined (module Phoenix.Endpoint.Cowboy2Handler is not available)
                    Phoenix.Endpoint.Cowboy2Handler.child_spec(:http, MyAppWeb.Endpoint, [port: 4000, otp_app: :my_app])
                    (phoenix) lib/phoenix/endpoint/handler.ex:33: anonymous fn/5 in Phoenix.Endpoint.Handler.init/1
                    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
                    (phoenix) lib/phoenix/endpoint/handler.ex:31: Phoenix.Endpoint.Handler.init/1
                    (stdlib) supervisor.erl:295: :supervisor.init/1
                    (stdlib) gen_server.erl:374: :gen_server.init_it/2
                    (stdlib) gen_server.erl:342: :gen_server.init_it/6
                    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

I’m using and have been using for a while the latest versions of everything: Cowboy - 2.5, Phoenix - 1.3. All has been working fine for over a half a year

Well this is definitely the issue. Cowboy2 handler means that something was updated very recently and not everything was updated entirely and properly and/or _build got corrupted, in which case wipe and rebuild?

2 Likes

removed _build and recompiled – didn’t help

Change your cowboy dependency to {:plug_cowboy, "~> 1.0"}.

3 Likes

They said they were already using cowboy2 successfully so that shouldn’t be needed.

Can you post your mix.exs file so I can drop it into a new project to give it a try?

3 Likes
  defp deps do
    [
      
      {:phoenix, github: "phoenixframework/phoenix", override: true},
      # {:phoenix, github: "phoenixframework/phoenix"},
      # {:phoenix, "~> 1.3.4"},


      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 3.5"},
      {:postgrex, "~> 0.13.5"},
      {:phoenix_html, "~> 2.12"},
      {:phoenix_live_reload, "~> 1.1", only: :dev},
      {:gettext, "~> 0.16"},
      {:cowboy, "~> 2.5", override: true},
      {:yamerl, "~> 0.7"},
      {:earmark, "~> 1.2"},
      {:edeliver, "~> 1.6"},
      {:distillery, "~> 2.0"},
      {:httpoison, "~> 1.4"},
      {:swoosh, "~> 0.20"},
      {:gen_smtp, "~> 0.13"},


      # {:poison, "~> 4.0.1"},
      # {:poison, "~> 4.0.1", override: true},
      {:poison, "~> 3.1"},
    ]
  end

The module Plug.Adapters.Cowboy2 was deprecated a few days ago and it’s now recommended to use {:plug_cowboy, "~> 2.0"} for cowboy 2.0

https://hexdocs.pm/plug/Plug.Adapters.Cowboy2.html

I ran into the same or similar problem yesterday and this is how I was able to solve it.

6 Likes

Why and how did it use to work without plug_cowboy at all in my project?

phoenix 1.3.4 of August 2018 - doesn’t work now either

Cowboy 2 is only supported on Phoenix 1.4.0-rc.2 and above via {:plug_cowboy, "~> 2.0"}

It is likely that you have updated plug to 1.7 in which case you will be prompted to replace cowboy with {:plug_cowboy, "~> 1.0"} in your mix.exs file.

4 Likes

I know where it’s suppoted.

Look at my mix, it’s been working since a half a year ago.

…How?

Right here uses the latest plug, this is using the github master instead of a proper release.

When I’m using phoenix 1.3.4 the error doesn’t go away

When I’m adding “plug 1.6.4” neither does it

Make sure you wipe _build when changing the phoenix 1.3.4, but at that point it really should work considering cowboy2 doesn’t exist in it, or you’d be getting a different error regardless?

rm mix.lock && rm -rf _build/ && mix deps.get --all

1 Like

What error are you getting though? If using phoenix 1.3.4 and older plug then it definitely wouldn’t be a cowboy2 thing since that doesn’t exist.

Did you check if your Endpoint configuration has some custom handler configuration?

I keep using cowboy 2x and phoenix 1.3.4. As I have been using cowboy 2x since a half a year ago.

The error is the same.

I don’t see how? Cowboy2 support wasn’t added until the (not yet released) v1.4?

What is your mix.exs in full as well as your config.exs and .exs files in full (sanitized if needed)?

I’ve posted my dependencies – that’s how.