azure-sdk-for-net: [BUG] Service Bus - OperationCanceledException in exception received handler while closing the message receiver

Describe the bug I have a QueueClient for receiving messages (basically using it just as MessageReceiver). It is working as expected - the messages are being processed. When I want to stop receiving messages, I call QueueClient.CloseAsync(). There are still messages in the queue, so I will stop the receiver in the middle of the work. So the receiver/client is in the closing state, and I (almost) alway get an error in my ExceptionReceivedHandler - it is OperationCanceledException with the simple message “The operation was canceled.”.

Exception or Stack Trace I do not have a direct exception. Just the exception from ExceptionReceivedEventArgs. The call stack in the received exception is:

at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.SemaphoreSlim.WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, Int32 millisecondsTimeout, CancellationToken cancellationToken)
at Microsoft.Azure.ServiceBus.MessageReceivePump.MessagePumpTaskAsync()

To Reproduce Just stop MessageReceiver while there still are messages in the queue to process and look in your exception handler.

Expected behavior I am not really sure about it. But I think that if the client is in closing state, I should not get this type of error in my error handler (ExceptionReceivedHandler). It is just annoying, because basically, I will get this error every time I am stopping my receiver.

Setup (please complete the following information):

  • OS: Windows 10
  • IDE : Visual Studio 2019
  • Version of the Library used: Microsoft.Azure.ServiceBus 3.4.0

Additional context I attached the source of Microsoft.Azure.ServiceBus to dig deeper what is going on.

  • The system throws while trying to get semaphore lock. The receiver is in closing state: pumpCancellationToken.IsCancellationRequested == true. So the semaphore throws OperationCanceledException.
  • In the catch block, there is info about not propagating ObjectDisposedException when the pump is stopping.
  • I think, that this situation with OperationCanceledException is the same and it should not be reported too.
  • The final note is, that it later comes into the finally block. The condition message == null is true, because loading of the message was skipped. And so it enters the if and releases semaphore lock. I am not sure about this, but I think the lock was not taken (semaphore threw exception), so probably it should not be released.

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Bug Description Added
  • Repro Steps Added
  • Setup information Added

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 23 (8 by maintainers)

Commits related to this issue

Most upvoted comments

This is something that would need to be implemented on the GitHub side. Alternatively, you can create a filter in your inbox or subscribe to a NuGet notification with a feed I’ve created https://libraries.io/nuget/Microsoft.Azure.ServiceBus/versions.atom. I’m using IFTTT with the feed to notify in Slack @nemakam.

@inkel This is not about version of .NET Core, but about version of ServiceBus Library. According to the merged PR above your comment, the fix will be in version 4.1.1 of Microsoft.Azure.ServiceBus. Current published version is 4.1.0. So you have to wait for the next release.

Is there a way to get notified of Microsoft.Azure.ServiceBus releases specifically instead of getting notifications for everything in the azure-sdk-for-net repo?