Cowboy on Erlang crashes on shutdown

600 Views Asked by At

I'm getting a lot of errors on shutdown of my Erlang vm related to my cowboy handlers. I've got a simple_one_for_one supervisor running a start_listeners() function that runs cowboy:start_http().

Everything starts, no errors, handles requests normally.

If I shutdown the erlang VM, I get:

[error] Supervisor bitter_rpc_sup had child bitter_rpc_http_id started with bitter_rpc_sup:start_listeners() at undefined exit with reason killed in context shutdown_error

And a bunch of other errors related to the cowboy processes being killed and terminating abnormally. Does cowboy not follow OTP conventions for shutdown? Is there a way for me to intercept the shutdown at the supervisor and manually shut down all of the cowboy processes / ranch pool?

Where should I be looking to try and squash this error?

2

There are 2 best solutions below

0
On

Taking a hard look at the included Cowboy examples, the http server isn't supervised directly, but is running under the Cowboy application.

So I changed the supervisor for my rpc daemon to do nothing:

init([]) ->
    Procs = [],
    {ok, {{one_for_one, 10, 10}, Procs}}.

and instantiated the cowboy dispatcher in the main process, returning the empty supervisor from start(,)

0
On

You can create ranch child and add it in your supervisor:

init([]) ->
    %% define Ref, NbAcceptors, IP, Port, Dispatch
    ...
    WebChild = ranch:child_spec(Ref,
                                NbAcceptors,
                                ranch_tcp,
                                [{ip, IP}, {port, Port}],
                                cowboy_protocol,
                                [{env, [{dispatch, Dispatch}]}]),
    {ok, {{one_for_one, 10, 10}, [WebChild]}}.