The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.

Sourav 165 Reputation points
2024-05-09T14:38:43.9166667+00:00

Getting this error in service bus. The error image is from log analytics.
The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.

The detailed error message is:
The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. For more information please see https://aka.ms/ServiceBusExceptions . Reference:9848cdb8-91b6-4c64-9068-b960a002418b, TrackingId:308df39c0000001b01fd05f2663b6136_G22_B16, SystemTracker:gi::G22:404006094:amqps://sb-planability-demo.servicebus.windows.net/-2f3da1c9;0:24:67:source(address:/queue-ndis-claim-status-checker,filter:[]), bi::in-connection3594(G22-257498372)::session3685::link33359346, Timestamp:2024-05-08T11:33:28 (MessageLockLost). For troubleshooting information, see https://aka.ms/azsdk/net/servicebus/exceptions/troubleshoot.
ServiceBusError

After studying numerous ms docs and stack overflow questions, what I've found out is, these following reasons could be the main reason of this error:

MessageLockLost :

And this can happen due to these reasons:
i. The lock timer has expired before it was renewed by the client application.
ii. The client application acquired the lock, saved it to a persistent store and then restarted. Once it restarted, the client application looked at the inflight messages and tried to complete the messages.

And also might happen for these reasons:
i. Service Update

ii. OS update

iii. Changing properties on the entity (queue, topic, subscription) while holding the lock.
Reference: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-exceptions-latest#reason-messagelocklost

And from the stackoverflow I've come accross a few more causes of this error to happen:
ServiceBusProcessor options config:

  • AutoCompleteMessages is by default set to true, set it to false
  • PrefetchCount is too high, for diagnostics set it to 0
  • ReceiveMode set it to ServiceBusReceiveMode.PeekLock

All these information led me to these question:
If I'm facing MessageLockLost, why this is happening?
After receiving the message I'm completing the message using

CompleteMessageAsync(message, stoppingToken);


And my host.json, where autocomplete is set as false

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "serviceBus": {
    "autoComplete": false
  },
  "functionTimeout": "-1"
}

After marking the message as completed I'm doing other activities in the function which might take 1-10 minutes depending on external dependencies. Is MessageLockLost can happen even after setting the message as completed?

Another Question is:
What is the default PrefetchCount , can anyone direct me to a reference of default PrefetchCount ?

I apologise we did not catch the excepetion reason, which would have been really helpful to pinpoint the exception

try 
{ 
// Receive messages using the receiver client 
} catch (ServiceBusException ex) 
when (ex.Reason == ServiceBusFailureReason.ServiceTimeout) 
{ 
// Take action based on a service timeout 
}


Any information would be very helpful.
Thanks in advance

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
556 questions
{count} vote