[erlang-questions] supervisor does not restart my worker

Anders Nygren anders.nygren@REDACTED
Sat Jun 9 14:36:10 CEST 2007


On 6/9/07, Matej Kosik <kosik@REDACTED> wrote:
> Hi,
>
> While playing with Erlang, I have tried to check supervisor behavior. I have written a simple generic server (it is attached as `serv.erl' file) that is started by a simple supervisor (its source is in the attached `sup.erl' file).
>
> The API of the server is simple:
>
>         serv:question()
>         serv:fail()
>
> The second service intentionally fails. I wanted to see how the generic server is restarted so that I could continue using it (those services that does not cause failure). However, the `serv' worker is not restarted. What am I doing wrong?
>
>
Hi
The problem You have is that when You call serv:fail() the shell
process terminates,
and is immediately restarted. But since the supervisor was started
with sup:start_link
from the shell the supervisor is terminated.

One way to avoid this problem here is to make the call
> catch serv:fail().

Another useful thing is to start sasl, which will provide more
detailed error messages
>application:start(sasl).


/Anders

> <DETAILS>
> I start supervisor as follows
>
>         1> sup:start_link().
>
> Then I try to use services of my server:
>
>         2> serv:question().
>         42
>         3> serv:question().
>         42
>
> But when I call the service that causes failure of the server
>
>         4> serv:fail().
>
> I get
>
>         =ERROR REPORT==== 9-Jun-2007::13:34:51 ===
>         ** Generic server serv terminating
>         ** Last message in was fail
>         ** When Server state == []
>         ** Reason for termination ==
>         ** {{badmatch,2},[{serv,handle_call,3},{proc_lib,init_p,5}]}
>         ** exited: {{{badmatch,2},[{serv,handle_call,3},{proc_lib,init_p,5}]},
>             {gen_server,call,[serv,fail]}} **
>         5>
>         =ERROR REPORT==== 9-Jun-2007::13:34:51 ===
>         ** Generic server <0.32.0> terminating
>         ** Last message in was {'EXIT',<0.30.0>,
>                                {{{badmatch,2},
>                                  [{serv,handle_call,3},{proc_lib,init_p,5}]},
>                                 {gen_server,call,[serv,fail]}}}
>         ** When Server state == {state,{<0.32.0>,sup},
>                                one_for_all,
>                                [{child,
>                                     <0.37.0>,
>                                     serv,
>                                     {serv,start_link,[]},
>                                     permanent,
>                                     2000,
>                                     worker,
>                                     [serv]}],
>                                {dict,0,
>                                      16,
>                                      16,
>                                      8,
>                                      80,
>                                      48,
>                                      {[],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       [],
>                                       []},
>                                      {{[],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        [],
>                                        []}}},
>                                20,
>                                3600,
>                                [{1181,388891,757484}],
>                                sup,
>                                []}
>         ** Reason for termination ==
>         ** {{{badmatch,2},[{serv,handle_call,3},{proc_lib,init_p,5}]},
>             {gen_server,call,[serv,fail]}}
>
> and no restart happens.
> </DETAILS>
>
> Thanks for any clues.
> Cheers
> --
> Matej Kosik
>
> -module(serv).
> -behaviour(gen_server).
>
> -export([start_link/0]).
> -export([question/0, fail/0]).
> -export([init/1, handle_call/3, handle_cast/2]).
> -export([terminate/2, handle_info/2, code_change/3]).
>
> start_link() ->
>     gen_server:start_link({local, serv}, serv, [], []).
>
> question() ->
>     gen_server:call(serv, question).
>
> fail() ->
>     gen_server:call(serv, fail).
>
> init(_Args) ->
>     {ok, []}.
>
> handle_call(question, _From, State) ->
>     {reply, 42, State};
>
> handle_call(fail, _From, State) ->
>     X = 1,
>     % The following command will cause "bad match"
>     X = 2,
>     {reply, 42, State}.
>
> handle_cast(_, State) ->
>     {noreply, State}.
>
> handle_info(_, State) ->
>     {noreply, State}.
>
> terminate(_, _) ->
>     ok.
>
> code_change(_, State, _) ->
>     {ok, State}.
>
> -module(sup).
> -behaviour(supervisor).
>
> -export([start_link/0]).
> -export([init/1]).
>
> start_link() ->
>     supervisor:start_link(sup, []).
>
> init(_Args) ->
>     {ok, {{one_for_all, 20, 3600},
>           [{serv, {serv, start_link, []},
>             permanent, 2000, worker, [serv]}]}}.
>
> all: serv.beam sup.beam
>
> %.beam: %.erl
>         erlc "$<"
>
> clean:
>         rm -f *.beam
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>



More information about the erlang-questions mailing list