1

I have an Erlang supervisor that supervises a process of a worker-server based on gen_server, I start form the shell my supervisor, which in turn starts my worker-server with no problems, It looks like this:

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

But I when I crash my worker server, my supervisor crashes with it for unknown reason.

I found on the internet a fix for this, I use this:

start_link_shell() ->
    {ok,Pid} = supervisor:start_link({local, ?SERVER}, ?MODULE, []),
    unlink(Pid).

Now it works fine, but I don't understand why, Can anyone explain ?


**

Update

**

This is my init function

%%%===================================================================

init([]) ->

    % Building Supervisor specifications

    RestartStrategy = one_for_one,
    MaxRestarts = 2,
    MaxSecondsBetweenRestarts = 5000,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},


    % Building Child specifications

    Restart = permanent,
    Shutdown = 2000,    % Number of seconds the child is allowed to run after receiving shutdown message
    Type = worker,

    ChildSpec = {'db_server', 
                {'db_server', start_link, []},
                Restart, 
                Shutdown, 
                Type, 
                ['db_server']},


    % Putting Supervisor and Child(ren) specifications in the return

    {ok, {SupFlags, [ChildSpec]}}.
securecurve
  • 5,589
  • 5
  • 45
  • 80

1 Answers1

1

As per this link:

The problem testing supervisors from the shell is that the supervisor process is linked to the shell process. When gen_server process crashes the exit signal is propagated up to the shell which crashes and get restarted .. and that will be used for testing only, otherwise, the OTP application should start the supervisor and get linked to it.

Community
  • 1
  • 1
securecurve
  • 5,589
  • 5
  • 45
  • 80