[erlang-questions] gen_server behaviour

Christian Axelsson <>
Tue May 26 10:42:15 CEST 2009


Hello,

I'm having some trouble with the gen_server behaviour. I've constructed
a minimal testcase below:

  -module(gen_server_api).
  -behaviour(gen_server).

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

  -export([start/0, f/0]).

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

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

  handle_call(f, _From, State) ->
      {reply, huzza, State};
  handle_call(_Message, _From, State) ->
      {reply, error, State}.

  handle_cast(_Message, State) -> {noreply, State}.
  handle_info(_Message, State) ->
      gen_server:call(?MODULE, f), {noreply, State}.
  terminate(_Reason, _State) -> ok.
  code_change(_OldVersion, State, _Extra) -> {ok, State}.

Then I run the following in erl:

  1> c(gen_server_api).
  {ok,gen_server_api}
  2>
  2> {ok, P} = gen_server_api:start().
  {ok,<0.37.0>}
  3> gen_server:call(gen_server_api, f). % Works
  huzza
  4> P ! test. % Does not work
  test
  5>
  =ERROR REPORT==== 25-May-2009::17:33:34 ===
  ** Generic server gen_server_api terminating
  ** Last message in was dwqfwef
  ** When Server state == state
  ** Reason for termination ==
  ** {timeout,{gen_server,call,[gen_server_api,f]}}
  ** exception error: {timeout,{gen_server,call,[gen_server_api,f]}}

This is not the behaviour I expected. Can anyone care to explain why I
get a timeout when from within handle_info/2 I call gen_server:call/2 ?

-- 
Christian Axelsson




More information about the erlang-questions mailing list