Ranch listener MyApp.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at ... exit with reason

My phoenix app starts up fine, but as soon as I modify any elixir source files the above error begins to occur repeatedly. This is the reason it gives:

{:undef, [{MusicJamGameWeb.RoomChannel, :child_spec, [{MusicJamGameWeb.Endpoint, {#PID<0.1281.0>, #Reference<0.623636450.1826357254.163247>}}], []}, {Phoenix.Channel.Server, :join, 4, [file: ‘lib/phoenix/channel/server.ex’, line: 24]}, {Phoenix.Socket, :handle_in, 4, [file: ‘lib/phoenix/socket.ex’, line: 598]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: ‘lib/phoenix/endpoint/cowboy2_handler.ex’, line: 145]}, {:cowboy_websocket, :handler_call, 6, [file: ‘/home/patrick/Code/music_jam_game/deps/cowboy/src/cowboy_websocket.erl’, line: 528]}, {:cowboy_http, :loop, 1, [file: ‘/home/patrick/Code/music_jam_game/deps/cowboy/src/cowboy_http.erl’, line: 257]}, {:proc_lib, :init_p_do_apply, 3, [file: ‘proc_lib.erl’, line: 226]}]}

So far I’ve just been following the guide on using channels (Channels — Phoenix v1.6.7) trying to get a channel up and running. I’ll push my code to GH in case that helps. Also possibly unrelated but the messages pushed on to the channel by the JS are not being received in my handle_in callback. I’m stumped.

Could be off but check your channel module name in GH: (music_jam_server/room_channel.ex at main · patreeceeo/music_jam_server · GitHub)

HelloWeb.RoomChannel != MusicJamGameWeb.RoomChannel

The clue was the module name in user_socket.ex: MusicJamGameWeb.RoomChannel,.

1 Like

That was certainly an issue but alas still seeing the above issues after fixing that.

Are you sure? I’ve pulled down your repository, updated room_channel.ex and the problem goes away.

1 Like

Yes, I’m sure.

Just curious if you were able to reproduce the issue? since “goes away” implies it was there. Please note the error doesn’t appear right away. I haven’t determined exactly what triggers it, but it takes some time (less than a minute) and/or interaction before it starts.

I’m going to start over with an earlier version of Elixir (using 1.13 now) and hope that I don’t run in to this again.

Also I’ve noticed a couple similar error reports around the internet. For one, the root cause had to do with the Elixir linter Vim plugin. For the other, the user had “an extra redirect.” Neither of these cases seem to apply here.

Also I’m using

  • Phoenix 1.6.7
  • Elixir 1.13
  • Erlang 24.2.1
  • Ubuntu 20.04.1
  • x86_64

FWIW I started a fresh new project, channels are working and the error is gone. I probably klutzed something, but I also switched to Elixir 1.12. Later I’ll try again with 1.13 just in case.

To my surprise, trying this same procedure again with Elixir 1.13 reproduced the same error message. I spun up a new project with phx_new, followed the official Channels guide, and the error started immediately. I pushed the code here: GitHub - patreeceeo/cowboy-opaque-error-min-repro: I believe this to be a minimal reproduction of a crash that occurs in Cowboy with correct (?) Phoenix code on Elixir 1.13. It wouldn’t be so bad if the error actually gave some useful info. Instead of a human-friendly message I just see :undef.

Here’s the error message again:

[error] Ranch listener MusicJamServerWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.5779.0> exit with reason: {:undef, [{MusicJamServerWeb.RoomChannel, :child_spec, [{MusicJamServerWeb.Endpoint, {#PID<0.5779.0>, #Reference<0.3249608392.111935491.56526>}}], []}, {Phoenix.Channel.Server, :join, 4, [file: 'lib/phoenix/channel/server.ex', line: 24]}, {Phoenix.Socket, :handle_in, 4, [file: 'lib/phoenix/socket.ex', line: 598]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 145]}, {:cowboy_websocket, :handler_call, 6, [file: '/home/patrick/Code/music_jam_server/deps/cowboy/src/cowboy_websocket.erl', line: 528]}, {:cowboy_http, :loop, 1, [file: '/home/patrick/Code/music_jam_server/deps/cowboy/src/cowboy_http.erl', line: 257]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 226]}]}

I’ve pulled your repo down as is and it fails, but once I add the channel module definition, it works as intended. In your reproduction repo, your channel module does not exist and any attempt to join a channel should fail. In fact, if you read your error message carefully, you can actually guess what it’s trying to do. Now, it does require some experience, but I can walk you through my thought process.

[error] Ranch listener MusicJamServerWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.5779.0> exit with reason: {:undef, [{MusicJamServerWeb.RoomChannel, :child_spec, [{MusicJamServerWeb.Endpoint, {#PID<0.5779.0>, #Reference<0.3249608392.111935491.56526>}}], []}, {Phoenix.Channel.Server, :join, 4, [file: 'lib/phoenix/channel/server.ex', line: 24]}, {Phoenix.Socket, :handle_in, 4, [file: 'lib/phoenix/socket.ex', line: 598]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 145]}, {:cowboy_websocket, :handler_call, 6, [file: '/home/patrick/Code/music_jam_server/deps/cowboy/src/cowboy_websocket.erl', line: 528]}, {:cowboy_http, :loop, 1, [file: '/home/patrick/Code/music_jam_server/deps/cowboy/src/cowboy_http.erl', line: 257]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 226]}]

There are two halves of these error that are important to us:

:undef, [{MusicJamServerWeb.RoomChannel, :child_spec, [{MusicJamServerWeb.Endpoint, {#PID<0.5779.0>, #Reference<0.3249608392.111935491.56526>}}], []},

and then every under it. What this looks like is a trace, specifically a function trace. You can tell since the output reads like a MFA (module-function-arguments). MFA are typically used with apply/3, so things like apply(String, :at, ["example", 2]) are used for somewhat dynamic calls. Now, take a look at first line:

{MusicJamServerWeb.RoomChannel, :child_spec, [{MusicJamServerWeb.Endpoint, {#PID<0.5779.0>, #Reference<0.3249608392.111935491.56526>}}]

This is definitely a MFA like structure, a module, a function atom and the leading arguments. So for one, if we assume that this is the case, the module MusicJamServerWeb.RoomChannel must exist, but it does not in your repository. Furthermore, the :child_spec function is critically important in Erlang; it defines the specification to start child processes. With that being said, we can check the Phoenix socket code for this behaviour: phoenix/server.ex at v1.6 · phoenixframework/phoenix · GitHub

Here the channel is expected to have a function child_spec/1, but since the module is undefiined, you’ll run into the exact problem you have. This can easily be replicated here:

iex(2)> apply(DoesNotExist, :hello, [])
** (UndefinedFunctionError) function DoesNotExist.hello/0 is undefined (module DoesNotExist is not available)
    DoesNotExist.hello()

If you go back to the Phoenix code, it attempts to start the child process based off that child specification call, but since you’re trying to call a function that doesn’t exist, it outright fails.

2 Likes