CAP icon indicating copy to clipboard operation
CAP copied to clipboard

Incorrect Handling of AutoCompleteMessages in AzureServiceBus Leads to Invalid Lock Error

Open yang-xiaodong opened this issue 1 year ago • 0 comments

Hi @mviegas

I received a subscription sponsorship for Azure ASB from Microsoft. Today, while testing Azure Service Bus, I encountered a "The lock supplied is invalid" error. I searched for the same issue on StackOverflow: https://stackoverflow.com/questions/28127001/the-lock-supplied-is-invalid-either-the-lock-expired-or-the-message-has-alread. The answer pointed towards the AutoCompleteMessages setting.

I checked the definition of AutoCompleteMessages.

It seems there's a logic error when acknowledging messages.

if (_serviceBusProcessor?.AutoCompleteMessages ?? false)
    commitInput.CompleteMessageAsync().GetAwaiter().GetResult();

See https://github.com/dotnetcore/CAP/blob/4d69d3eac12c4da9dd179442dc9f63501f5e8f9d/src/DotNetCore.CAP.AzureServiceBus/AzureServiceBusConsumerClient.cs#L131-L132

I believe the correct behavior should be to manually call CompleteMessageAsync when AutoComplete is disabled. So, I think this line should be modified to if (!_serviceBusProcessor!.AutoCompleteMessages).

Additionally, the default value of the AutoCompleteMessages property in AzureServiceBusOptions should be set to false. It was changed to true in a previous commit.

What do you think about this and am I missing something?

yang-xiaodong avatar Oct 18 '24 02:10 yang-xiaodong