[erlang-questions] gen_server behaviour

Matthias Lang matthias@REDACTED
Tue May 26 11:11:04 CEST 2009


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.

Matt

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



More information about the erlang-questions mailing list