Confused why I am unable to locate a process via a reference

In working with umbrella apps like the acme_bank example, I noticed on my local machine and heroku in somecases, that I can’t run both mix run --no-halt and iex -S mix at the same time from /apps/master_proxy. More on this issue here – https://github.com/wojtekmach/acme_bank/issues/10

I think the problem is trying to bind two Plug.Adapters.Cowboy.child_spec's to the same port, since both command start the application. My initial idea is to check to see if MasterProxy.Plug.HTTP is already started and if it is don’t try to start it again. However :erlang.whereis(MasterProxy.Plug.HTTP) doesn’t seem to work and when I try the observer, I don’t see a reference to MasterProxy.Plug.HTTP either even though the error message and Plugs docs say that’s what the reference should be.

The error is -

[error] Failed to start Ranch listener MasterProxy.Plug.HTTP in :ranch_tcp:listen([port: 3333]) for reason :eaddrinuse (address already in use)

Thanks

If a process is registered from within an Elixir app it’ll be registered with an “Elixir.” prefix, so that would be “Elixir.MasterProxy.Plug.HTTP”.

You can start your IEx session and type :observer.start to spawn the Observer tool. There you’ll be able to check “Applications” to see which children and supervisors are registered by name.

Aliases ate resolved correctly, so there is no need to fully qualify it. Problem here is probably that both instances of the applications are running on difference nstances of the beam vm and as such are isolated from each other… They are like two nodes running on the same computer, but not connected. Perhaps node discovery abs communication can help you out. Perhaps snippy shelling out to ps and kill might be much easier…

1 Like

Oh and before I forget. Not to start the application might be the simplest solution ever.

Edit: Ah ok, I totally see the problem now. With heroku though I still don’t want to bind to the port twice. Is it possible to see if the port is already in use first?

Thanks for your help @NobbZ

The most simple way is just to try binding to it and catch if it fails, same in any language. :slight_smile:

1 Like