Hi,
I'm having an issue with aborting receive operations from Service Bus for Windows Server 1.0. The same issue might occur when running against Azure but I have not tested that yet.
My scenario is this:
I have a receive loop in a separate thread which I start as a task (where I call the Receive method over and over again to get messages). In some cases I want to abort receiving, so then I call the Abort method from another thread.
The issues is that the Receive method doesn't abort in a timely fashion if I call Abort shortly after the Receive method was called. The Receive method continues to run until the serverWaitTime expires (the timeout period provided to the receive method, default 1 min if not specified).
I have created a small reproduction of the issue where I do a Thread.Sleep(...) before calling Abort. In this case the receive operation Aborts quickly after my sleep as expected. However I would prefer not to have a Thread.Sleep since I assume that the time for this delay will vary between different setups. In my scenario about 300ms seemed to be the magic number.
Does anyone have a better solution or workaround to this other than having a Thread.Sleep?
Or could anyone from the Service Bus team comment on this?
Below is a snippet of my reproduction of the issue:
public void TestAbort(string connectionString) { // Some delays I have tried with. var tooShortDelay = 100; // With this delay the stop watch shows about 10sec (the specified timeout) var delayWhichWorks = 400; // With this delay the stopwatch shows about 400ms var messageFactory = MessagingFactory.CreateFromConnectionString(connectionString); var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); var queueName = "MyQueueName"; if (namespaceManager.QueueExists(queueName)) { namespaceManager.DeleteQueue(queueName); } namespaceManager.CreateQueue(queueName); var queueClient = messageFactory.CreateQueueClient(queueName); var stopwatch = Stopwatch.StartNew(); var task = Task.Run(() => queueClient.Receive(TimeSpan.FromSeconds(10))); Thread.Sleep(tooShortDelay); queueClient.Abort(); task.Wait(); Console.WriteLine("Time to abort receive operation: " + stopwatch.ElapsedMilliseconds); messageFactory.Close(); }
Regards,
//Thomas