Intermittent ranch ets error causing Phoenix app to crash on boot up

Occasionally our Phoenix app fails to boot due to the below error originating in ranch.

  ** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, MyAppWeb.Endpoint.HTTP}
      ** (EXIT) an exception was raised:
          ** (ArgumentError) errors were found at the given arguments:
* 2nd argument: not a key that exists in the table
              (stdlib 3.17.2) :ets.lookup_element(:ranch_server, {:addr, MyAppWeb.Endpoint.HTTP}, 2)
              (ranch 1.8.0) /tmp/build_5293a25c/deps/ranch/src/ranch_server.erl:113: :ranch_server.get_addr/1
              (phoenix 1.6.14) lib/phoenix/endpoint/cowboy2_adapter.ex:119: Phoenix.Endpoint.Cowboy2Adapter.bound_address/2
              (phoenix 1.6.14) lib/phoenix/endpoint/cowboy2_adapter.ex:115: Phoenix.Endpoint.Cowboy2Adapter.info/3
              (phoenix 1.6.14) lib/phoenix/endpoint/cowboy2_adapter.ex:101: Phoenix.Endpoint.Cowboy2Adapter.start_link/3
              (stdlib 3.17.2) supervisor.erl:414: :supervisor.do_start_child_i/3
              (stdlib 3.17.2) supervisor.erl:400: :supervisor.do_start_child/2
              (stdlib 3.17.2) supervisor.erl:384: anonymous fn/3 in :supervisor.start_children/2

It appears a specific key is expected to have been set in ets, but for whatever reason, that hasn’t been created yet.

As this doesn’t occur on every bootup, I’m having a hard time debugging and reproducing this issue.

If anyone has any thoughts or suggestions on this, it would be much appreciated.

1 Like

A shot in the dark - maybe a port collision on the listening socket? Typically I would expect an eaddrinuse error to be shown in the logs in such a case, though.

Hey, yeah I have seen that in the past and would see the eaddrinuse error in the stacktrace. That was the confusing part for me as this error is mostly the same.

I am logging used ports on the server when booting the app to confirm that the port was available.