[rabbitmq-discuss] .NET client SimpleRpcServer question

Ryan Davis ryan at acceleration.net
Tue Jun 9 23:03:33 BST 2009

Ok, last one today, I promise.

I solved this problem my adding some explicit locks in Subscription and
SimpleRpcServer.  I tried to checkout the latest source (on linux,
cygwin, and tortoiseHG) but kept getting a zlib error, so this patch is
against the source in rabbitmq-dotnet-client-1.5.3.zip downloaded from

This patch makes SimpleRpcServer.Close threadsafe by adding:

   1. locking SimpleRpcServer.m_subscription around the body of
      SimpleRpcServer.MainLoop's foreach (so we can't close while
   2. checking for a null Subscription.Consumer before processing a
      message in SimpleRpcServer.MainLoop, but after we've locked
      m_subscription (to ensure we don't process after we've closed)
   3. locking this around the body of Subscription.Close (to ensure we
      don't close that same subscription at the same time)

I *think* that solves the potential race conditions.  It certainly makes
my tests work, and running "nant unit" on the 1.5.3 source gives 2
failures in TestSharedQueue.cs that look timing-related.


Ryan Davis
Director of Programming Services
2831 NW 41st street, suite B
Gainesville, FL 32606

Office: 352-335-6500 x 124
Fax: 352-335-6506

Ryan Davis wrote:
> Ryan Davis wrote:
>> I'm going to add a flag and test out calling Close() from various points
>> in HandleCall/HandleCast/ProcessRequest.
> Wow, I really didn't think that one through. 
> HandleCall/HandleCast/ProcessRequest are only called when there is a
> message to process.  If there is no message to process, the
> SimpleRpcServer is blocked on Subscription.Next(), and will never
> exit.   My current test case starts a SimpleRpcServer, then tries to
> stop it, without sending any messages.
> I changed my code so if the SimpleRpcServer sees a certain byte[]
> message (stored as ShutdownKey), it will close itself:
> public override void HandleCast(bool isRedelivered, IBasicProperties
> requestProperties, byte[] body)
> {
>   for (var i = 0; i < body.Length; i++) {
>      if (body[i] != ShutdownKey[i]) return;
>   }
>   Close(); //we must have matched, shut down.
> }
> As I expected, this throws a different exception:
> System.InvalidOperationException: Operation is not valid due to the current state of the object.
>    at RabbitMQ.Client.MessagePatterns.Subscription.Next() in d:\Projects\rabbitmq-dotnet-client-1.5.3\src\client\messagepatterns\Subscription.cs:line 323
>    at RabbitMQ.Client.MessagePatterns.Subscription.System.Collections.IEnumerator.MoveNext() in d:\Projects\rabbitmq-dotnet-client-1.5.3\src\client\messagepatterns\Subscription.cs:line 437
>    at RabbitMQ.Client.MessagePatterns.SimpleRpcServer.MainLoop() in d:\Projects\rabbitmq-dotnet-client-1.5.3\src\client\messagepatterns\SimpleRpcServer.cs:line 206
> I'm going to start mucking about in SimpleRpcServer.cs/Subscription.cs
> to see what's the smalled change I can make to fix it.
> Thanks,
> Ryan Davis
> Acceleration.net
> Director of Programming Services
> 2831 NW 41st street, suite B
> Gainesville, FL 32606
> Office: 352-335-6500 x 124
> Fax: 352-335-6506
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> http://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20090609/a5f582cc/attachment.htm 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: threadsafe-rpcserver.patch
Url: http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20090609/a5f582cc/attachment.txt 

More information about the rabbitmq-discuss mailing list