n1cK
n1cK

Reputation: 368

Erlang supervisor silently failing to start

I am an Erlang beginner, learning OTP. I received a noproc error when trying to talk to a supervisor in runtime. In fact, that supervisor's start_link (implemented by me, not supervisor:start_link()) seemed not to be executed, as the io:fwrite in its first line wasn't even put out.

It turned out the issue was that one of the supervisor's children was missing some gen_server callback functions (which were not used by the application). The compiler logged a warning about that and that's it, no errors when I started the application. I'm not sure what is going on, but shouldn't there be some error indication happening?

In case it's important, that's how the child spec of the supervisor itself looks like:

ElementSup = {sc_element_sup,
                    {sc_element_sup, start_link, []},
                    permanent, 2000, supervisor, [sc_element]},

Upvotes: 1

Views: 95

Answers (1)

7stud
7stud

Reputation: 48599

First, I get a compiler error:

2> c(my_worker).
my_worker.erl:5: function terminate/2 undefined
error

Then, the supervisor lets me know that it didn't start because it couldn't start all its children:

3> my_sup:start_link().                  
** exception exit: {shutdown,
                    {failed_to_start_child,my_worker, 
                     {'EXIT',
                      {undef,
                       [{my_worker,start_link,[],[]},
                        {supervisor,do_start_child,2,
                         [{file,"supervisor.erl"},{line,365}]},
                        {supervisor,start_children,3,
                         [{file,"supervisor.erl"},{line,348}]},
                        {supervisor,init_children,2, 
                         [{file,"supervisor.erl"},{line,314}]},
                        {gen_server,init_it,2,
                         [{file,"gen_server.erl"},{line,365}]},
                        {gen_server,init_it,6,
                         [{file,"gen_server.erl"},{line,333}]},
                        {proc_lib,init_p_do_apply,3,
                         [{file,"proc_lib.erl"},{line,247}]}]}}}}

In fact, that supervisor's start_link seemed not to be executed, as the io:fwrite in its first line wasn't even put out.

The supervisor's start_link() function is defined in the OTP library, so you can't have an io:fwrite() in that function. You can add an io:fwrite() line to a function that you define, which in turn calls supervisor:start_link(), for instance:

start_my_supervisor() ->
    io:format("hello~n"),
    Args = [],
    supervisor:start_link({local, ?MODULE}, ?MODULE, Args).

The "hello" message displays just fine in my erlang shell output.

Upvotes: 1

Related Questions