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.
Using Azure.Messaging.ServiceBus v7.3.0. When calling
ServiceBusReceiver.CompleteMessageAsync()
, storing the resulting task into a collection and then awaiting all withawait Task.WhenAll(completionTasks);
eg:This will result in a MessageLock exception when running on Azure (and sometimes locally):
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:
This solved the issue and the locking behaves as expected.