[erlang-questions] gen_server behaviour

Christian Axelsson smiler@REDACTED
Tue May 26 13:25:37 CEST 2009


Matthias Lang wrote:
> On Tuesday, May 26, Christian Axelsson wrote:
> 
>> 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 ?
> 
> Ulf Wiger already gave you the short answer. But since I already started
> writing the long answer, here it is anyway:
> 
> The gen_server behaviour hides the underlying message passing, making
> it harder to reason about this deadlock. Here's the same problem
> illustrated in terms of plain message passing (code for illustration,
> not intended to compile):
> 
>               loop(State) ->
>                  receive
>                     {request, From, X} when is_pid(From) -> 
> 		    	      Answer = f(State, X),
> 			      From ! {reply, Answer};
> 
>                     Info -> 
>  		    	      self() ! {request, X},
>                               receive
>                                  {reply, Answer} -> 
> 				    io:fwrite("got: ~p\n", [Answer])
>                               end
>                   end
> 
> 'receive' will hang forever. gen_server is just a more complicated
> version of the same idea.

Ah, ofcourse! Thank you and Ulf for the quick response. The problem that
I'm facing here is to make some legacy code that uses standard message
passing (using !) to our OTP-styled app and I though that this would've
been a clean enough way to do that.

Any suggestions on how to accomplish that? I suspect that it isn't a too
uncommon problem that people face.

> p.s. great that you posted a minimal and complete example, that cuts
> out a lot of guesswork.

:)

-- 
Christian Axelsson
smiler@REDACTED



More information about the erlang-questions mailing list