From 1ecfb4708a1dd3b2f6dc89ea9b803b72e135636a Mon Sep 17 00:00:00 2001 From: Srikanth Aithal Date: Fri, 29 Nov 2024 17:39:05 +0530 Subject: [PATCH] Add retry for urlopen Many a times open url returns errors including 503, handling it by introducing retries. Signed-off-by: Srikanth Aithal --- avocado/utils/download.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/avocado/utils/download.py b/avocado/utils/download.py index 740e7259ec..ab51ed3730 100644 --- a/avocado/utils/download.py +++ b/avocado/utils/download.py @@ -44,11 +44,18 @@ def url_open(url, data=None, timeout=5): :return: file-like object. :raises: `URLError`. """ - try: - result = urlopen(url, data=data, timeout=timeout) - except (socket.timeout, HTTPError) as ex: - msg = f"Failed downloading file: {str(ex)}" - log.error(msg) + retry_attempt = 0 + while retry_attempt < 10: + try: + result = urlopen(url, data=data, timeout=timeout) + break + except (socket.timeout, HTTPError) as ex: + msg = f"Failed downloading file: {str(ex)}" + log.error(msg) + retry_attempt += 1 + msg = f"retry attempt: {retry_attempt}" + log.debug(msg) + if "result" not in locals(): return None msg = (