What's wrong with this ???

Joe Armstrong <>
Mon Jun 2 16:25:19 CEST 2003


I'm trying to make a supervision tree example.

I have a Key-Value server with a deliberate error. Looking up the key
'crash' with kv:lookup(crash) should case the server to crash  
with a divide by zero error and the supervisor should restart the server -
instead the supervisor crashes. 

If I do this:

   > simple_sup1:start().
   ...
   > kv:lookup(crash)

The KV server dies as expected, restarts and then the supervisor
itself dies.

Any ideas as to what I've done wrong

/Joe


---- here's simple_sup1.erl ----

-module(simple_sup1).
-behaviour(supervisor).

-export([start/0, init/1]).

start() ->
    supervisor:start_link({local, simple_supervisor},
			  ?MODULE, nil).

init(_) ->
    {ok,{{one_for_one, 5, 1000},
         [
          {server, 
	   {kv, start, []}, 
	   permanent, 5000, worker, [kv]}]}}.

---- here's kv.erl ----


-module(kv).
-behaviour(gen_server).

-export([start/0, stop/0, lookup/1, store/2]).

-export([init/1, handle_call/3, handle_cast/2, 
	 terminate/2]).

start() -> gen_server:start_link({local,kv},kv,arg1,[]).

stop()  -> gen_server:cast(kv, stop).
    
init(arg1) ->
    io:format("Key-Value server starting~n"),
    {ok, dict:new()}.

store(Key, Val) -> 
    gen_server:call(kv, {store, Key, Val}).
    
lookup(Key) -> gen_server:call(kv, {lookup, Key}).

handle_call({store, Key, Val}, From, Dict) ->
    Dict1 = dict:store(Key, Val, Dict),
    {reply, ack, Dict1};
handle_call({lookup, crash}, From, Dict) ->
    1/0;
handle_call({lookup, Key}, From, Dict) ->
    {reply, dict:find(Key, Dict), Dict}.

handle_cast(stop, Dict) -> {stop, normal, Dict}.

terminate(Reason, Dict) ->
    io:format("K-V server terminating~n").











More information about the erlang-questions mailing list