How can I catch throttling exceptions (429
status code) in Azure's Python SDK?
In the example code below, I would expect to quickly (after many iterations over the while
loop) be throttled. When a 429
throttled response is returned, I would expect the SDK to raise a HttpResponseError, that I can catch and handle appropriately. Instead, it just hangs until the retry_after
value in the response has elapsed - it neither raises an error nor returns any value to account_properties
.
Example code:
from azure.mgmt.storage import StorageManagementClient
from azure.core import exceptions
storage_client = StorageManagementClient("tokenCredential", "subscriptionId")
while True:
try:
account_properties = storage_client.storage_accounts.get_properties(account_name="myStorageAccount", resource_group_name="myResourceGroup")
print(account_properties)
except exceptions.HttpResponseError as httpErr:
print("###Caught exception###")
print(httpErr.message)
break
If I provide an invalid storage account name and a 404 'resource not found' response is returned, the SDK raises the expected error. Just not with a 429 'throttled' response. Is this intended behavior, and is there any way around it without querying the REST APIs directly?
Python3 version: 3.10.6, azure-mgmt-storage: 20.1.0, azure-core: 1.26.4,
Edit for extra info
Debug logs show the following request-response pair when hitting throttling: Request
{"asctime": "2023-05-05 09:14:10,271", "levelname": "DEBUG", "name": "azure.core.pipeline.policies._universal", "filename": "_universal.py", "lineno": 330, "threadName": "createstorageaccount", "message": "Request URL: 'https://management.azure.com/subscriptions/mySubscriptionId/providers/Microsoft.Storage/storageAccounts?api-version=2022-05-01'\nRequest method: 'GET'\nRequest headers:\n 'Accept': 'application/json'\n 'x-ms-client-request-id': 'requestId'\n 'User-Agent': 'azsdk-python-azure-mgmt-storage/20.1.0 Python/3.10.6 (Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.35)'\n 'Authorization': 'myBearerToken'\nRequest body:\nThis request has no body"}
Response
{"asctime": "2023-05-05 09:14:10,330", "levelname": "DEBUG", "name": "azure.core.pipeline.policies._universal", "filename": "_universal.py", "lineno": 375, "threadName": "createstorageaccount", "message": "Response status: '429'\nResponse headers:\n 'Cache-Control': 'no-cache'\n 'Pragma': 'no-cache'\n 'Content-Length': '226'\n 'Content-Type': 'application/json'\n 'Expires': '-1'\n 'Retry-After': '282'\n 'x-ms-request-id': 'requestId'\n 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'\n 'Server': 'Microsoft-Azure-Storage-Resource-Provider/1.0,Microsoft-HTTPAPI/2.0 Microsoft-HTTPAPI/2.0'\n 'x-ms-ratelimit-remaining-subscription-reads': '11799'\n 'x-ms-correlation-request-id': '84...'\n 'x-ms-routing-request-id': 'UKWEST:20...'\n 'X-Content-Type-Options': 'nosniff'\n 'Date': 'Fri, 05 May 2023 08:14:12 GMT'\n 'Connection': 'close'\nResponse content:\n{\"error\":{\"code\":\"TooManyRequests\",\"message\":\"The request is being throttled as the limit has been reached for operation type - List_ObservationWindow_00:05:00. For more information, see - https://aka.ms/srpthrottlinglimits\"}}"}
The client does not raise any error at all, the 429 response is logged only as a DEBUG event.