2

In my azure function, at some point I would like to defer my message. But if I do, I get an exception:

[7/30/2020 5:59:02 PM] Message processing error (Action=Complete, ClientId=MessageReceiver1UserCreated/Subscriptions/MySubscription, EntityPath=UserCreated/Subscriptions/MySubscription, Endpoint=xxxxxxxxxxx.servicebus.windows.net)
[7/30/2020 5:59:02 PM] Microsoft.Azure.ServiceBus: 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.

This is my code

[FunctionName("UserCreated")]
public static async Task Run([ServiceBusTrigger("UserCreated", "MySubscription", Connection = "ServiceBusConnectionString")]UserCreated userCreated, ILogger log, string lockToken, MessageReceiver messageReceiver)
{
    //some logic.....

    await messageReceiver.DeferAsync(lockToken);
}

Honestly I have no clue what I am doing wrong. Code examples id found and also this StackOverflow: Azure Function V2 Service Bus Message Deferral post, does not help me out.

I understand that the message is automatically completed after the function completes. So I tried to disable autocomplete but also there I did not succeed to find a working solution.

Using packages:

  • Microsoft.Azure.WebJobs.Extensions.ServiceBus 4.1.0
    • (references) Microsoft.Azure.ServiceBus 4.1.1
hwcverwe
  • 5,287
  • 7
  • 35
  • 63

3 Answers3

2

As the error message states, the message may be losing the lock before reaching the Defer instruction. Try to extend the lock timeout on your service bus. I think it may fix the issue.

Hugo Barona
  • 1,303
  • 9
  • 21
2

Here is a bit of an explanation on what a lock does in a service bus queue, according to the error you describe, your lock is expiring before you are able to defer and autorenewal should be handled by the functions but it is not guaranteed, so the best way to tackle this is to extend the maximum duration of the lock.

The easiest way to achieve this is to navigate into the azure portal and find the service bus subscription you wish to change, once you select it you should see something like this screen:

enter image description here

By clicking on the Change button under the message lock duration you will be able to modify the duration based on your needs.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Armando Bracho
  • 659
  • 5
  • 21
2

Thanks for all answers however none actually explained the real cause.

TL;DR

If you want to complete, defer, abandon or remove the message yourself, you have to disable autocomplete in the host.json file.

Root cause

The reason why the lock is invalid states:

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.

In my case the message was already "removed" since I used messageReceiver.DeferAsync(lockToken);

So this means that after this statement, the function automatically completes the message (which is already deferred).

Therefore you have to disable autocompletion of the message.

Solution

disable autocomplete in host.json

"extensions": {
  "serviceBus": {
    "messageHandlerOptions": {
      "autoComplete": false
    }
  }
}

Be careful

When disabling autocomplete, you are responsible to do something with the message. You always have to make a decision otherwise the message will become available again after lock timeout.

hwcverwe
  • 5,287
  • 7
  • 35
  • 63