[erlang-questions] gen_server cast question

Logan, Martin <>
Wed Jan 31 17:20:33 CET 2007

-----Original Message-----
[mailto:] On Behalf Of Ladislav
Sent: Wednesday, January 31, 2007 9:20 AM
To: Erlang-Questions (E-mail)
Subject: [erlang-questions] gen_server cast question


I was browsing erlang (library) sources and found the
following in gen_server module:

do_send(Dest, Msg) ->
   case catch erlang:send(Dest, Msg, [noconnect]) of
     noconnect ->
       spawn(erlang, send, [Dest, Msg]);
     Other ->

do_send is called for example from gen_server:cast/2.
I know what the code above does, but don't understand
how the following can work:

   gen_server:cast(Ref, msg1),
   gen_server:cast(Ref, msg2),
   gen_server:cast(Ref, msg3)

Suppose that Ref points to a process which resides on
the node that is not yet connected to the current node.
When the first cast is evaluated, noconnect is returned
and new process is spawned that sends the message msg1
to Ref. If the second cast is also evaluated before the
connection to the node is established, second process
is spawned that sends the message msg2 to Ref.

I think that there is a possibility that Ref will not
receive messages msg1, msg2 and msg3 in expected order
because of the internal spawning.


Message order is not guaranteed with asynchronous messaging in any case.
You should never rely on it.  Also, as an aside, it would be strange to
have a reference to a process for a node that is not connected, that
suggests poor design. 

erlang-questions mailing list


More information about the erlang-questions mailing list