[erlang-questions] Re: testing asynchronous code

Jay Nelson jay@REDACTED
Thu Apr 22 02:55:08 CEST 2010


On Apr 21, 2010, at 3:38 PM, Matthew Sackman wrote:

> On Wed, Apr 21, 2010 at 12:09:09PM -0700, Jay Nelson wrote:
>> 1) Make sure you cast before the Pid leaks out to any callers
>
> Why? Do you think that gives you any guarantee that that message will
> arrive in the mailbox before a message sent from a different  
> process? I
> would be a little surprised if that's guaranteed.

You're right, the cast call spawns a process with a single call to  
erlang:send/2, but that process may not get a chance to run before  
the initializing process makes a gen_server:call.  I have done this  
in past for a large data load initialization, and I need to find and  
fix that, because it can cause a race condition error between the  
final initialization and the first gen_server:call made by the  
process which created the gen_server.

What is guaranteed is if you make a gen_server:call (which is what I  
should have said in the first place, and therefore doesn't apply to  
Scott's original comment about casting to self) inside the start  
function after init returns the Pid, but before the start function  
hands it back (provided another process is not able to guess the Pid  
before it is told what it is).  The language does guarantee that two  
calls from the same process to another process will maintain their  
order in the message queue.

If the route of a message goes through an intermediate process (as  
cast does), there is no ordering guarantee at the final recipient.

jay



More information about the erlang-questions mailing list