2

Sometimes lock is not being renewed automatically. Please let me know if i am doing any mistake. I am using Microsoft.Azure.ServiceBus library (v3.4.0) and below is my code snippet.

var client = new QueueClient(connectionString, queueName, ReceiveMode.PeekLock);
            client.RegisterMessageHandler(async (message, token) =>
            {
                var logger = new AzureLogger();
                logger.Debug("----------------------------------------------------------------------------");
                logger.Debug("Message received.");
                var body = Encoding.UTF8.GetString(message.Body);
                logger.Debug($"Message : {body}");
                try
                {
                    for (int i = 1; i <= 30; i++)
                    {
                        logger.Debug(i.ToString());
                        logger.Debug("Lock time: " + message.SystemProperties.LockedUntilUtc.ToString("yyyy-MM-dd hh:mm:ss"));
                        logger.Debug("Lock token info: " + message.SystemProperties.IsLockTokenSet);
                        logger.Debug("Lock token: " + message.SystemProperties.LockToken);
                        Thread.Sleep(60000);
                    }
                    logger.Debug("Processing completed.");
                    await client.CompleteAsync(message.SystemProperties.LockToken);
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                    logger.Error(ex.StackTrace);
                    await client.DeadLetterAsync(message.SystemProperties.LockToken);
                }
            }, new MessageHandlerOptions(LogMessageHandlerException)
            {
                AutoComplete = false,
                MaxAutoRenewDuration = TimeSpan.FromMinutes(120)
            });

log enter image description here

Hasnu zama
  • 156
  • 8
  • From this [answer and the comment](https://stackoverflow.com/a/21435308/10383250), it could be caused by clock drift. – George Chen Nov 20 '19 at 02:28

2 Answers2

3

Lock renewal is not a guaranteed operation. It's a client-side operation/request which when fails, will not be able to extend the lock. You have to keep that in mind when relying on this feature.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • What is the best way to run long running processes without getting these kind of errors? – Hasnu zama Nov 21 '19 at 10:25
  • My advice is not to rely on the feature and redesign how the long processing is implemented. – Sean Feldman Nov 22 '19 at 01:10
  • Here's an idea of how this could be achieved ([link](https://docs.particular.net/samples/azure/azure-service-bus-long-running/)). Note that this is not the only option or the best. You'll need to figure out what works better for your case. – Sean Feldman Nov 22 '19 at 04:15
0

A Lock on a Service Bus Message can be renewed only before the lock on the Service Bus Message has not expired. If the lock on the message has expired, even before the lock renewal is triggered then an exception will be raised, which will not guarantee that the lock on all the messages will be renewed as @Sean Feldman mentioned.

If the operation performed on your locked message takes more time than the lock duration of your message you will get the Lock supplied is invalid or expired exception when you try to complete or dead-letter the message

One more recommendation is that your MaxAutoRenewDuration should be in sync with the lock duration of your messages.

Ranjith Eswaran
  • 333
  • 2
  • 12