The message in the Service bus queue unlocks automatically before the Function App completes the processing. After unlocking the message, another instance of the function app read the same message from the queue and start processing.

Sinhal, Nikhil 20 Reputation points
2025-10-29T13:45:45.8966667+00:00

The message in the Service bus queue unlocks automatically before the Queue triggered Function App completes the processing the message. After unlocking the message, another instance of the function app read the same message from the queue and start processing. Due to this, multiple instances try to insert same record in database table and gives primary key violation issue.

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

Answer accepted by question author
  1. Praveen Kumar Gudipudi 1,495 Reputation points Microsoft External Staff Moderator
    2025-11-07T13:52:07.5633333+00:00

    Hello Sinhal, Nikhil,

    To fix the issue, I programmatically refreshed the message lock on regular interval by calling the renew method as follows:

    Task.Run(async () => {

    while(flag)

    {

    await messageActions.RenewMessageLockAsync(message);
    

    }

    }

    Please accept as answer and do a Thumbs-up to upvote this response if you are satisfied with the community help. Your upvote will be beneficial for the community users facing similar issues.

    User's image

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Praveen Kumar Gudipudi 1,495 Reputation points Microsoft External Staff Moderator
    2025-10-30T11:28:32.11+00:00

    Hello Sinhal, Nikhil,

    The auto-renewal feature is available for Azure Functions when they are triggered by Service Bus and allows you to set the maxAutoRenewDuration in the host.json file.

    Maximum value: The maximum duration you can configure for auto-renew is 30 minutes.

    This means if your function is running for longer than 30 minutes, the auto-renew feature won’t be enough, and you'll have to implement manual lock renewal within your function to keep the message locked for longer periods.

    If your function is running for a long time (and less than 30 minutes), you can configure auto-renew in the host.json file like this:

    {

    "version": "2.0",

    "extensions": {

    "serviceBus": {
    
      "messageHandlerOptions": {
    
        "autoComplete": false,  // Don't auto-complete the message
    
        "maxAutoRenewDuration": "00:30:00"  // Max auto-renew duration (30 minutes)
    
      }
    
    }
    

    }

    }

    Please accept as answer and do a Thumbs-up to upvote this response if you are satisfied with the community help. Your upvote will be beneficial for the community users facing similar issues.

     User's image


  2. Sinhal, Nikhil 20 Reputation points
    2025-11-07T14:50:37.03+00:00

    To fix the issue, I programmatically refreshed the message lock on regular interval by calling the renew method as follows:

    Task.Run(async () => {

    while(flag)

    {

    SQL

    await messageActions.RenewMessageLockAsync(message);
    

    }

    }

    Please accept as answer and do a Thumbs-up to upvote this response if you are satisfied with the community help. Your upvote will be beneficial for the community users facing similar issues.

    0 comments No comments

  3. Sinhal, Nikhil 20 Reputation points
    2025-11-07T14:52:12.7366667+00:00

    Issue is fixed now.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.