SV: SV: Trouble with gen_server

Lennart Öhman <>
Tue Mar 28 16:09:57 CEST 2006


Ok, now I see.
The reason is actually not related to your (miss)understanding of
OTP, but rather to how the Erlang shell works.
 
What happens is that by using gen_server:start_link you create
a process link between the "parent" process and the newly created
server process. This is normally what you want if the server is
started by a supervisor. But now you start the server manually
from the shell, which is fine so far...
But then you experience a runtime error in the shell. Because
there is a standard timeout in the gen_server:call/3 function
not waiting for more than 5 seconds (if I remember correctly).
Since gen_server:call/3 is executed in your shell process, the
shell process (or more precisely a help process to it) terminates.
Links are bidirectional, making your server experience it as
its supervisor has terminated. Hence it terminates!
 
Try adding a start API for manual start using gen_server:start
instead = no link.
 
Best Regards
/Lennart
 
-------------------------------------------------------------
Lennart Ohman                   phone   : +46-8-587 623 27
Sjöland & Thyselius Telecom AB  cellular: +46-70-552 6735
Sehlstedtsgatan 6               fax     : +46-8-667 8230
SE-115 28 STOCKHOLM, SWEDEN     email   : 

________________________________

Från:  genom Mark Lee
Skickat: ti 2006-03-28 15:50
Till: 
Ämne: Re: SV: Trouble with gen_server



O nTue, Mar 28, 2006 at 03:31:38PM +0200, Lennart Öhman wrote:
> Hi, if you include a code example it becomes easier to help :-)
> 
> /Lennart


Ok, as simple as I can get.

1> test:start_link().
{ok,<0.33.0>}
2> test:test().
** exited: {timeout,{gen_server,call,[test,test]}} **
3> test:test().
** exited: {noproc,{gen_server,call,[test,test]}} **

or

1> test:start_link().
{ok,<0.33.0>}
2> oops().

=ERROR REPORT==== 28-Mar-2006::14:45:20 ===
Error in process <0.31.0> with exit value:
{undef,[{shell_default,oops,[]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {undef,[{shell_default,oops,[]},
                   {erl_eval,do_apply,5},
                   {shell,exprs,6},
                   {shell,eval_loop,3}]} **
3> test:test().
** exited: {noproc,{gen_server,call,[test,test]}} **

Here's the code:


-export([start_link/0]).

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

-record(state, {}).

-define(SERVER, ?MODULE).

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

init([]) ->
        {ok, #state{}}.

handle_call(test, _From, State) ->
        {noreply, State, 3000};
handle_call(_Request, _From, State) ->
        Reply = ok,
        {reply, Reply, State}.

handle_cast(_Msg, State) ->
        {noreply, State}.

handle_info(_Info, State) ->
        {noreply, State}.

terminate(_Reason, _State) ->
        ok.

code_change(_OldVsn, State, _Extra) ->
        {ok, State}.

test() ->
        gen_server:call(test, test).


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060328/e164a7bc/attachment.html>


More information about the erlang-questions mailing list