0

Facing below service bus queue trigger lock exception if the c# Azure queue trigger functions taking/running more than 5 minutes( because the azure function long running process). How to avoid this exception or solve? if you have some samples related to lock renewal and settings please suggest.

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

  • 1
    Try increasing 'message lock duration' parameter for your queue, or the other things mentioned in this SO [answers](https://stackoverflow.com/questions/28127001/the-lock-supplied-is-invalid-either-the-lock-expired-or-the-message-has-alread) – Anand Sowmithiran Nov 08 '21 at 13:37

1 Answers1

0

Firstly, we need to make sure that we are at latest version, if not, please upgrate it.

Make sure you add retry policy in function.json as below:

{
    "disabled": false,
    "bindings": [
        {
            ....
        }
    ],
    "retry": {
        "strategy": "fixedDelay",
        "maxRetryCount": 4,
        "delayInterval": "00:00:10"
    }
}

Along with it check for dead letter and configure it with help of MS Docs.

We need to mentioned the lock duration for renewal after it expires in host.json by including the parameter in JSON structure "maxAutoLockRenewalDuration": "00:05:00"

Below is the example for host.json

{
    "version": "2.0",
    "extensions": {
        "serviceBus": {
            "clientRetryOptions":{
                "mode": "exponential",
                "tryTimeout": "00:01:00",
                "delay": "00:00:00.80",
                "maxDelay": "00:01:00",
                "maxRetries": 3
            },
            "prefetchCount": 0,
            "autoCompleteMessages": true,
            "maxAutoLockRenewalDuration": "00:05:00",
            "maxConcurrentCalls": 16,
            "maxConcurrentSessions": 8,
            "maxMessages": 1000,
            "sessionIdleTimeout": "00:01:00"
        }
    }
}

From the above JSON, change the parameter of autoCompleteMessages from true to false as "autoCompleteMessages": false.

When set to false, you are responsible for calling MessageReceiver methods to complete, abandon, or deadletter the message. If an exception is thrown (and none of the MessageReceiver methods are called), then the lock remains. Once the lock expires, the message is re-queued with the DeliveryCount incremented and the lock is automatically renewed.

Using ReceiveandLock has solved the issue in this case

Refer to these SO thread: SO1 and SO2 (thanks to the answered authors for detailed explanations)

SaiKarri-MT
  • 1,174
  • 1
  • 3
  • 8