<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7638.1">
<TITLE>Re: SV: Trouble with gen_server</TITLE>
</HEAD>
<BODY>
<DIV id=idOWAReplyText337 dir=ltr>
<DIV dir=ltr><FONT face="Courier New" color=#000000 size=2>Ok, now I
see.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>The reason is actually not related
to your (miss)understanding of</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>OTP, but rather to how the Erlang
shell works.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>What happens is that by using
gen_server:start_link you create</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>a process link between the "parent"
process and the newly created</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>server process. This is normally
what you want if the server is</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>started by a supervisor. But now
you start the server manually</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>from the shell, which is fine so
far...</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>But then you experience a runtime
error in the shell. Because</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>there is a standard timeout in the
gen_server:call/3 function</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>not waiting for more than 5 seconds
(if I remember correctly).</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Since gen_server:call/3 is executed
in your shell process, the</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>shell process (or more precisely a
help process to it) terminates.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Links are bidirectional, making
your server experience it as</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>its supervisor has terminated.
Hence it terminates!</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Try adding a start API for manual
start using gen_server:start</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>instead = no link.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Best Regards</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>/Lennart</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" color=#000000
size=2> </DIV></FONT></DIV>
<DIV id=idSignature75458 dir=ltr>
<DIV><FONT face="Courier New" color=#000000
size=2>-------------------------------------------------------------</FONT></DIV>
<DIV><FONT face="Courier New" color=#000000 size=2>Lennart
Ohman
phone : +46-8-587 623 27</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Sjöland & Thyselius Telecom
AB cellular: +46-70-552 6735</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Sehlstedtsgatan
6
fax : +46-8-667 8230</FONT></DIV>
<DIV><FONT face="Courier New" size=2>SE-115 28 STOCKHOLM,
SWEDEN email : <A
href="mailto:lennart.ohman@st.se">lennart.ohman@st.se</A></FONT></DIV></DIV>
<DIV dir=ltr><BR>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>Från:</B> owner-erlang-questions@erlang.org genom
Mark Lee<BR><B>Skickat:</B> ti 2006-03-28 15:50<BR><B>Till:</B>
erlang-questions@erlang.org<BR><B>Ämne:</B> Re: SV: Trouble with
gen_server<BR></FONT><BR></DIV>
<DIV>
<P><FONT size=2>O nTue, Mar 28, 2006 at 03:31:38PM +0200, Lennart Öhman
wrote:<BR>> Hi, if you include a code example it becomes easier to help
:-)<BR>> <BR>> /Lennart<BR><BR><BR>Ok, as simple as I can
get.<BR><BR>1> test:start_link().<BR>{ok,<0.33.0>}<BR>2>
test:test().<BR>** exited: {timeout,{gen_server,call,[test,test]}} **<BR>3>
test:test().<BR>** exited: {noproc,{gen_server,call,[test,test]}}
**<BR><BR>or<BR><BR>1> test:start_link().<BR>{ok,<0.33.0>}<BR>2>
oops().<BR><BR>=ERROR REPORT==== 28-Mar-2006::14:45:20 ===<BR>Error in process
<0.31.0> with exit
value:<BR>{undef,[{shell_default,oops,[]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}<BR><BR>**
exited:
{undef,[{shell_default,oops,[]},<BR>
{erl_eval,do_apply,5},<BR>
{shell,exprs,6},<BR>
{shell,eval_loop,3}]} **<BR>3> test:test().<BR>** exited:
{noproc,{gen_server,call,[test,test]}} **<BR><BR>Here's the
code:<BR><BR><BR>-export([start_link/0]).<BR><BR>-export([init/1, handle_call/3,
handle_cast/2, handle_info/2,<BR>
terminate/2, code_change/3,
test/0]).<BR><BR>-record(state, {}).<BR><BR>-define(SERVER,
?MODULE).<BR><BR>start_link()
-><BR>
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).<BR><BR>init([])
-><BR> {ok,
#state{}}.<BR><BR>handle_call(test, _From, State)
-><BR> {noreply, State,
3000};<BR>handle_call(_Request, _From, State)
-><BR> Reply =
ok,<BR> {reply, Reply,
State}.<BR><BR>handle_cast(_Msg, State)
-><BR> {noreply,
State}.<BR><BR>handle_info(_Info, State)
-><BR> {noreply,
State}.<BR><BR>terminate(_Reason, _State)
-><BR>
ok.<BR><BR>code_change(_OldVsn, State, _Extra)
-><BR> {ok, State}.<BR><BR>test()
-><BR> gen_server:call(test,
test).<BR></FONT></P></DIV>
</BODY>
</HTML>