Search code examples
elixirerlang-otpcowboy

Application.stop(...) in Elixir shell


I am programming a simple application in Elixir using Plug and Cowboy and mix. After executing:

$ iex -S mix

the application starts. If I execute

iex(1)> Application.start(:web)
{:error, {:already_started, :web}}

I get the expected output. However, when I try to stop the application using the following command:

iex(2)> Application.stop(:web)
:ok

I get the :ok but then I get lots of messages including the following:

iex(3)>
09:36:43.689 [info]  Application web exited: :stopped

09:36:43.691 [error] GenServer #PID<0.189.0> terminating
** (stop) killed
Last message: {:EXIT, #PID<0.187.0>, :killed}
State: {:state, {#PID<0.189.0>, :ranch_acceptors_sup}


:undefined, 1, 5, [], 0, :ranch_acceptors_sup, [ServeRequests.HTTP, 100, 
:ranch_tcp, [port: 4000]]}
09:36:43.691 [error] Failed to start Ranch listener ServeRequests.HTTP in :ranch_tcp:listen([port: 4000]) for reason :eaddrinuse (address already in use)
09:36:43.691 [info]  Application ranch exited: shutdown
nil

Am I doing something wrong in the code? Is this the normal behaviour?

The contents of the mix.exs file are the following:

defmodule Web.Mixfile do
  use Mix.Project

  def project do
    [app: :web,
     version: "0.1.1",
     elixir: "~> 1.2",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  def application do
    [applications:
        [:logger, :cowboy, :plug],
        mod: {Web, []}
    ]
  end

  defp deps do
  [{:cowboy, "~> 1.0.3"},
  {:plug, "~> 1.1.2"}]
  end
end

The contents of lib/web.ex are the following:

defmodule Web do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      worker(Web.Router, [])
    ]

    opts = [strategy: :one_for_one, name: Web.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

thanks, Mihalis.


Solution

  • This is a bit subdocumented in the docs, and I now also see that I should have explained this in EiA (my bad). The gist is that you should use Plug.Adapters.Cowboy.child_spec to properly insert the server in the supervision tree. Then, the server will be correctly stopped with the application.

    Here's how it would look in your example (in web.ex):

    children = [
      Plug.Adapters.Cowboy.child_spec(:http, Web.Router, [])
    ]
    

    With that, you can get rid of my_web_start and Web.Router.start_server functions completely.

    Trying it in the shell gives me:

    iex(1)> Application.stop(:web)
    :ok
    16:44:29.929 [info]  Application web exited: :stopped
    
    iex(2)> Application.start(:web)
    :ok
    

    And after that I can again access the page.