gen_server documentation

Matthias Lang <>
Fri Apr 7 12:20:00 CEST 2006


The documentation is correct.

You have overlooked how linked processes must behave. If you link a
process and then exit, then your process (the shell, in your example)
will exit too.

If you don't want that to happen, you can either not link in the first
place, or you can trap exits. Example:

3> process_flag(trap_exit, true).
false
4> gen_server:start_link(dumb_server, [], []).
{error,foo}


Matthias

----------------------------------------------------------------------


Samuel Rivas writes:
 > Hi,
 > 
 > Documentation for gen_server:start_link reads:
 > 
 >   "  If Module:init/1 fails with Reason, the function returns
 >   {error,Reason}. If Module:init/1 returns {stop,Reason} or ignore, the
 >   process is terminated and the function returns {error,Reason} or ignore,
 >   respectively"
 > 
 > For gen_server:start it refers to previous definition.
 > 
 >   However, both functions behaves differently: in both cases start_link exits,
 >   while start returns {error, Reason}:
 > 
 > 
 > %%%%
 > -module(dumb_server).
 > -behaviour(gen_server).
 > 
 > -export([init/1]).
 > 
 > init(_) ->
 >         exit(foo).
 > 
 > 
 > 1> gen_server:start_link(dumb_server, [], []).
 > ** exited: foo **
 > 2> gen_server:start(dumb_server, [], []).
 > {error,foo}
 > 
 > %%%%
 > -module(dumb_server).
 > -behaviour(gen_server).
 > 
 > -export([init/1]).
 > 
 > init(_) ->
 >         {stop, foo}.
 > 
 > 
 > 1> gen_server:start_link(dumb_server, [], []).
 > ** exited: foo **
 > 2> gen_server:start(dumb_server, [], []).     
 > {error,foo}
 > 
 > -- 
 > 	Samuel



More information about the erlang-questions mailing list