azure-sdk-for-python: Azure Service Bus SDK - long running process and failed lock renewal process/closed handler

  • Package Name: azure-servicebus
  • Package Version: 7.12.2
  • Operating System: Windows, Linux
  • Python Version: 3.9.13

Describe the bug My message consumer is a long running process. From time to time it have a problem with renewing message lock and after that with completing/rejecting that message. I started to investigate possible reasons for that and this is my findings:

  1. It happens when consumer take very long time to process the message (during my tests it was cases that take 45 minutes and 75 minutes)
  2. Log from lock renewal:
INFO - AMQP error occurred: (None), condition: (b'amqp:unauthorized-access'), description: ('Message lock renew failed. b"Unauthorized access. \'Listen\' claim(s) are required to perform this operation. Resource: \'sb://{namespace}.servicebus.windows.net/{topic}/subscriptions/{subscription}/$management\'. TrackingId:b700e3ac-fca7-42bf-b21f-bb9370b30651_G52, SystemTracker:NoSystemTracker, Timestamp:2024-05-10T07:02:12".').
[...]
DEBUG - Failed to auto-renew lock: ServiceBusAuthorizationError('Message lock renew failed. b"Unauthorized access. \'Listen\' claim(s) are required to perform this operation. Resource: \'sb://{namespace}.servicebus.windows.net/{topic}/subscriptions/{subscription}/$management\'. TrackingId:c9f7ef23-d8f1-42e4-80a8-dd1ec074f1fc_G55, SystemTracker:NoSystemTracker, Timestamp:2024-05-20T07:58:41". Error condition: amqp:unauthorized-access. Status Code: 401.'). Closing thread.
  1. After that when message is processed and service try to complete it I receive following error:
ERROR - Error during processing message: {"message": "Test message 5"}. Error: Failed to complete the message as the handler has already been shutdown.Please use ServiceBusClient to create a new instance.
Traceback (most recent call last):
  File "\sample.py", line 32, in run_consumer
    subscription_receiver.complete_message(msg)
  File "\.venv\lib\site-packages\azure\servicebus\_servicebus_receiver.py", line 879, in complete_message
    self._settle_message_with_retry(message, MESSAGE_COMPLETE)
  File "\.venv\lib\site-packages\azure\servicebus\_servicebus_receiver.py", line 498, in _settle_message_with_retry
    self._check_message_alive(message, settle_operation)
  File "\.venv\lib\site-packages\azure\servicebus\_common\receiver_mixins.py", line 103, in _check_message_alive
    raise ValueError(
ValueError: Failed to complete the message as the handler has already been shutdown.Please use ServiceBusClient to create a new instance.
  1. I tested that both by creating client from connection string (ServiceBusClient.from_connection_string(conn_str=connection_string)) and azure-identity library (ServiceBusClient(host_name, credential=DefaultAzureCredential()) - in both cases that error appear
  2. My tests cover various combinations of possible ways of implementing message consumers with various configurations:
    • sync code (with sleep())
    • async code using asyncio with await asyncio.sleep() and sleep()
    • Service Bus lock time set to 1, 3 and 5 minutes
    • Various time of simulated consumer work (5, 10, 15, 30, 45, 60, 75 minutes) + random time
  3. When I use consumer that wait 45 minutes it fail during processing second message. For 75 minutes I could reproduce that behavior each time app is trying to process the message. What is worth to mention is that sleep for 60 minutes works just fine even if running for several hours. For consumer processing 30 minutes or lower everything works just fine
  4. During testing it on random sleep time (which is more likely what is happening in my real application) I face that problem from time to time. This is how it looks like on sample test run:
    • First message took 1094 seconds (~18 minutes). Completed successfully
    • Second message took 1493 seconds (~25 minutes). Completed successfully
    • Third message took 1700 seconds (~28 minutes). During processing of that message I receive the error described in this issue.
  5. First logs related to error messages appear around 1h after consumer starts processing that long running operation (for example consumer starts processing message at 13:43:21,774. Message lock renewal failed at 14:48:45,475 with information about unauthorized access. This suggest that it happens when token is expired during that long running process. Previous point looks very similar when we sum the times. First and second message took in total 43 minutes. Next message took 28 minutes - during that time token probably expired.

To Reproduce Steps to reproduce the behavior:

  1. Run following code
import logging
from time import sleep
from azure.servicebus import ServiceBusClient, ServiceBusReceivedMessage, AutoLockRenewer
from azure.servicebus.exceptions import ServiceBusError
import logging

def run_consumer(connection_string: str, topic_name: str, subscription_name: str, processing_time_seconds: int):
    try:
        servicebus_client = ServiceBusClient.from_connection_string(conn_str=connection_string)
                    
        with servicebus_client:
            renewer = AutoLockRenewer(max_lock_renewal_duration=60000, on_lock_renew_failure=_lock_renew_failed)

            subscription_receiver = servicebus_client.get_subscription_receiver(topic_name=topic_name, subscription_name=subscription_name, auto_lock_renewer=renewer)
            
            with subscription_receiver:
                while True:
                    received_msgs: list[ServiceBusReceivedMessage] = subscription_receiver.receive_messages(max_wait_time=30, max_message_count=1)
                    
                    for msg in received_msgs:
                        try:
                            logging.info(f"Received message: {msg}")
                            sleep(processing_time_seconds)
                            logging.info(f"Processed message: {msg}")

                            subscription_receiver.complete_message(msg)
                        except Exception as e:
                            logging.exception(f"Error during processing message: {msg}. Error: {e}")
                            try:
                                subscription_receiver.abandon_message(msg)
                            except ServiceBusError as service_bus_exception:
                                logging.exception(f"Error during abandoning message: {msg}. Error: {service_bus_exception}")
                                raise
    except Exception as exception:
        logging.exception(f"Consumer for {topic_name}/{subscription_name} failed. Error: {exception}")

def _lock_renew_failed(msg, exception):
    logging.error(f"Failed to renew message lock. Message: {msg}. Error: {exception}")

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

connection_string='Endpoint=sb://{namespace}.servicebus.windows.net/;SharedAccessKeyName={key_name};SharedAccessKey={key};EntityPath={entity_path}'
topic_name = ''
subscription_name= ''
processing_time_minutes = 75

run_consumer(connection_string, topic_name, subscription_name, processing_time_minutes*60)
  1. Wait around 1h, after that time you should see in logs something like this:
INFO - AMQP error occurred: (None), condition: (b'amqp:unauthorized-access'), description: ('Message lock renew failed. b"Unauthorized access. \'Listen\' claim(s) are required to perform this operation. Resource: \'sb://{namespace}.servicebus.windows.net/{topic}/subscriptions/{subscription}/$management\'. TrackingId:b2e0e289-4788-4985-8119-35b8d7f69845_G28, SystemTracker:NoSystemTracker, Timestamp:2024-05-15T12:48:44".').
[...]
DEBUG - Failed to auto-renew lock: ServiceBusAuthorizationError('Message lock renew failed. b"Unauthorized access. \'Listen\' claim(s) are required to perform this operation. Resource: \'sb://{namespace}.servicebus.windows.net/{topic}/subscriptions/{subscription}/$management\'. TrackingId:b2e0e289-4788-4985-8119-35b8d7f69845_G28, SystemTracker:NoSystemTracker, Timestamp:2024-05-15T12:48:44". Error condition: amqp:unauthorized-access. Status Code: 401.'). Closing thread.
ERROR - Failed to renew message lock. Message: {"message": "Test message 0"}. Error: Failed to auto-renew lock
  1. After another 15 minutes, when message will be processed, you could observe following logs:
INFO - Processed message: {"message": "Test message 0"}
ERROR - Error during processing message: {"message": "Test message 0"}. Error: Failed to complete the message as the handler has already been shutdown.Please use ServiceBusClient to create a new instance.
Traceback (most recent call last):
  File "\sample.py", line 32, in run_consumer
    subscription_receiver.complete_message(msg)
  File "\.venv\lib\site-packages\azure\servicebus\_servicebus_receiver.py", line 879, in complete_message
    self._settle_message_with_retry(message, MESSAGE_COMPLETE)
  File "\.venv\lib\site-packages\azure\servicebus\_servicebus_receiver.py", line 498, in _settle_message_with_retry
    self._check_message_alive(message, settle_operation)
  File "\.venv\lib\site-packages\azure\servicebus\_common\receiver_mixins.py", line 103, in _check_message_alive
    raise ValueError(
ValueError: Failed to complete the message as the handler has already been shutdown.Please use ServiceBusClient to create a new instance.

Expected behavior Application will behave the same way no mater how long consumer will take. Refreshing tokens (if this is the issue) should be handled correctly during long running operations.

Additional context Currently I handle that situation by reconnecting to the Azure Service Bus in case of any errors and subscribe to messages one more time. Additionaly I implement inbox to know which messages were already processed (from application perspective, not broker). But this is workaround that will works only if I will have one instance of the consumer. If I will have multiple instances then when lock renewal process fail other instance will take the same message for processing when the first consumer is still working on it. Inbox will not help in that situation.

I attach full logs from sample run (processing time set to 75 minutes): log.log

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 19 (8 by maintainers)

Most upvoted comments

Hi @cgoma, the version with the fix has not been released yet. We will update this thread when it is

@gpiotrowski I was able to reproduce this on my end and am working on a PR to address it. Ill update the issue when we have it ready for release