gen_server documentation

Fredrik Thulin <>
Fri Apr 7 12:20:46 CEST 2006


On Friday 07 April 2006 12:02, Samuel Rivas wrote:
> 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}

What happens here is that the started gen_server _terminates_ - it does 
not return a value to the process that starts it (your shell). Since 
you use start_link the termination of the gen_server is propagated to 
your shell, which also terminates.

This can be observed by checking the pid of the shell before and after.


1> self().
<0.30.0>
2> gen_server:start_link(dumb_server, [], []).
** exited: foo **
3> self().
<0.34.0>
4>      

You see? Different pid, because you crashed the first shell process.

/Fredrik



More information about the erlang-questions mailing list