Im running into an issue with the message lock expiry in SerivceBus using Python SDK.
I've created a simple tool that will clear dead-lettered messages in queues and subscriptions. We have various amount of queues and subscriptions and the tool acts as a clean-up job which I'm just running locally.
The tool works fine however because its a "clean-up" tool we might have messages banked in DLQ which have been there for months.
Im running into a issue where im trying to complete these messages however its throwing azure.servicebus.exceptions.ServiceBusError: The lock on the message lock has expired
- exception.
I thought I was able to resolve this issue but using AutoLockRenewer
and actually renewing the lock on the message before completing it, however, the exception seems to still be getting thrown.
It's strange because say the exception gets thrown the tool will stop running, once I re-run the tool it's able to complete messages where it previously couldn't, but it will eventually find a message in another queue/subscription where it will break due to the lock. So after each re-run it's able to clear DLQ in more and more queues/subscriptions, it doesn't break at the same point as the previous run.
This is a snippet of my code:
renewer = AutoLockRenewer()
with ServiceBusClient.from_connection_string(shared_access_key["primaryConnectionString"]) as client:
for queue in queues:
if queue["countDetails"]["deadLetterMessageCount"] > 0:
with client.get_queue_receiver(queue_name=queue["name"], sub_queue="deadletter") as receiver:
while len(receiver.receive_messages(max_wait_time=60)) > 0:
messages = receiver.receive_messages(max_message_count=50, max_wait_time=60)
for message in messages:
renewer.register(receiver, message, max_lock_renewal_duration=300)
receiver.complete_message(message)