Pop receipt changed on update message

2.3k Views Asked by At

Our application dequeues the message from Azure queue and makes it invisible for a given period of time.

Worker than processes it and when all work is done it deletes it from the queue.

But sometimes delete fails with 404 error not found. The problem might be that message's pop receipt has been changed.

Because when message is dequeued separate thread also runs and increases invisibility of a message to prevent it to be picked by other consumer. But it calls UpdateMessage which actually changes pop receipt.

Because UpdateMesasge and DeleteMessage might run at the same time, DeleteMessage sometimes fails because its PopReceipt is no longer valid.

Is there any way to avoid PopReceipt change on UpdateMessage?

Code sample:

TimerCallback extenderHandler = new TimerCallback(async state =>
{
  try
  {
    var oldNextVisibleTime = queueMessage.NextVisibleTime.Value;

    // extend message lease to prevent it from being picked by another worker instances
    await returnMessage();
   }
   catch (Exception ex)
   {
     // NOTE: exceptions on Timer are not propagated to main thread; the error is only logged, because operation will be retried;
   }
});

// start message extender timer which extends message lease time when it's nearing its max hold time, timer runs until it's disposed
using (var messageExtenderTimer = new System.Threading.Timer(extenderHandler, null, 0, (int)MessageLeaseCheckInterval.TotalMilliseconds))
{
  processMessage();
}

In returnMessage method UpdateMessageAsync from Microsoft.WindowsAzure.Storage.Queue is called.

In processMessage method processing itself is done and at the end message is deleted using DeleteMessage method from Microsoft.WindowsAzure.Storage.Queue

And sometimes fails UpdateMessageAsync and sometimes DeleteMessage. Because of that I wonder that when these two concurrent threads make changes to the message - message is changed in the queue before PopReceipt is updated on message itself.

1

There are 1 best solutions below

9
On BEST ANSWER

Is there any way to avoid PopReceipt change on UpdateMessage?

Unfortunately no. Whenever a message is updated, a new PopReceipt will be returned. From the documentation link (#4 item):

The message has been updated with a new visibility timeout. When the message is updated, a new pop receipt will be returned.