Azure / azure-service-bus

☁️ Azure Service Bus service issue tracking and samples
https://azure.microsoft.com/services/service-bus
MIT License
585 stars 781 forks source link

MessageLock Exception if awaiting all CompleteAsync tasks #449

Closed jblanchet closed 3 years ago

jblanchet commented 3 years ago

Using Azure.Messaging.ServiceBus v7.3.0. When calling ServiceBusReceiver.CompleteMessageAsync(), storing the resulting task into a collection and then awaiting all with await Task.WhenAll(completionTasks); eg:

foreach (var message in messagesToComplete)
{
  completionTasks.Add(ServiceBusReceiver.CompleteMessageAsync(message));
}
await Task.WhenAll(completionTasks);

This will result in a MessageLock exception when running on Azure (and sometimes locally):

2021-09-16T18:48:00Z   [Information]   Attempting to complete 576 messages.
2021-09-16T18:48:04Z   [Error]   Exception while completing messages: Azure.Messaging.ServiceBus.ServiceBusException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance. (MessageLockLost)
2021-09-16T18:48:04Z   [Verbose]   Oldest message is locked until: 09/16/2021 18:48:30 +00:00
2021-09-16T18:48:04Z   [Verbose]   Youngest message is locked until: 09/16/2021 18:48:30 +00:00

Note the timestamp of when that happened and the locked until messages below. They still had 26 seconds before timing out.

Switching to looping through the collection of message to complete and awaiting each one, eg:

foreach (var message in messagesToComplete)
{
  await ServiceBusReceiver.CompleteMessageAsync(message);
}

This solved the issue and the locking behaves as expected.

SeanFeldman commented 3 years ago

@jblanchet wrong repo. You should move the post to https://github.com/Azure/azure-sdk-for-net/ if you want SDK team to look into it. image