[erlang-questions] Is gen_server handle_info synchronous?

Chris Duesing <>
Wed Mar 3 22:30:57 CET 2010


Yep, it was a bad code path. If a single hunk of data had more than
one message, the last one was incomplete, and there was no listeners
then the state wasn't getting updated.

-notify(Listener, [{message, Message} | Messages]) when is_pid(Listener) ->
-    Listener ! Message,
+notify(Listener, [{message, Message} | Messages]) ->
+    if is_pid(Listener) ->
+           Listener ! Message;
+       true ->
+           ok
+    end,
     notify(Listener, Messages);

oops.

Thanks for the help everyone!

Chris

On Wed, Mar 3, 2010 at 3:05 PM, Andrew Stone <> wrote:
> What you describe is exactly how a gen_server should work. It retrieves requests off the queue serially.
>
> My only guess is that you aren't returning the *new* state in the second parameter of the return value from handle_info/2. Remember data is immutable in erlang and that state parameter is not global.
>
> You need to do something like this:
>
> handle_info(Info, State) ->
>        NewState = [State | Info],
>    {noreply, NewState}.
>
> -Andrew
>
>
>
>
> ----- Original Message ----
> From: Chris Duesing <>
> To: 
> Sent: Wed, March 3, 2010 3:37:06 PM
> Subject: [erlang-questions] Is gen_server handle_info synchronous?
>
> I am working on some code that connects to a streaming data service
> via gen_tcp:connect(). I am doing this from inside a gen_server to
> benefit from OTP goodness. But, since it didn't seem to fit the
> paradigm to have an infinite loop of reading from the socket with
> recv, I left it as {active, once} and use handle_info to receive
> blocks of data. Whenever I receive an incomplete message in
> handle_info I just add it to the state, and then prepend it to the
> data I receive the next time handle_info is called. Once I started
> testing this code I am seeing that the data saved in the state is not
> available the next time handle_info is called. I guess I assumed that
> the gen_server would complete requests serially, is this not the case
> or am I missing something?
>
> Thanks,
>
> Chris
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>


More information about the erlang-questions mailing list