dotnetcore / CAP

Distributed transaction solution in micro-service base on eventually consistency, also an eventbus with Outbox pattern
http://cap.dotnetcore.xyz
MIT License
6.71k stars 1.29k forks source link

Incorrect Handling of AutoCompleteMessages in AzureServiceBus Leads to Invalid Lock Error #1598

Open yang-xiaodong opened 1 month ago

yang-xiaodong commented 1 month ago

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?

scrapstation commented 1 week ago

Can we speed up the release of this change? The latest version cannot use AzureServiceBus.

yang-xiaodong commented 1 week ago

@scrapstation Try to use version 8.3.2-preview-248100009