From 3f924b59f2d9eee7059eaff896757289d2e59ece Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Thu, 24 Feb 2022 15:58:13 -0500 Subject: [PATCH] use same timeout for galaxy api interactions (#77088) Also, bump the default. A number of people have reported problems with this 20 second timeout for a couple of years on https://github.com/ansible/galaxy/issues/2302 It is mentioned in the issue that resolving dns can sometimes take up to 30 seconds, and that this timeout includes dns resolution. Includes a changelog message --- changelogs/fragments/ansible_galaxy_timeout.yml | 2 ++ lib/ansible/galaxy/api.py | 6 ++++-- lib/ansible/galaxy/collection/concrete_artifact_manager.py | 7 +++++-- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/ansible_galaxy_timeout.yml diff --git a/changelogs/fragments/ansible_galaxy_timeout.yml b/changelogs/fragments/ansible_galaxy_timeout.yml new file mode 100644 index 00000000000..aa3f82b7e7f --- /dev/null +++ b/changelogs/fragments/ansible_galaxy_timeout.yml @@ -0,0 +1,2 @@ +bugfixes: + - extend timeout for ansible-galaxy when communicating with the galaxy server api, and apply it to all interactions with the api diff --git a/lib/ansible/galaxy/api.py b/lib/ansible/galaxy/api.py index ea580af1055..b211ad18db6 100644 --- a/lib/ansible/galaxy/api.py +++ b/lib/ansible/galaxy/api.py @@ -263,6 +263,7 @@ class GalaxyAPI: available_api_versions=None, clear_response_cache=False, no_cache=True, priority=float('inf'), + timeout=60, ): self.galaxy = galaxy self.name = name @@ -271,6 +272,7 @@ class GalaxyAPI: self.token = token self.api_server = url self.validate_certs = validate_certs + self.timeout = timeout self._available_api_versions = available_api_versions or {} self._priority = priority @@ -382,7 +384,7 @@ class GalaxyAPI: try: display.vvvv("Calling Galaxy at %s" % url) resp = open_url(to_native(url), data=args, validate_certs=self.validate_certs, headers=headers, - method=method, timeout=20, http_agent=user_agent(), follow_redirects='safe') + method=method, timeout=self.timeout, http_agent=user_agent(), follow_redirects='safe') except HTTPError as e: raise GalaxyError(e, error_context_msg) except Exception as e: @@ -440,7 +442,7 @@ class GalaxyAPI: """ url = _urljoin(self.api_server, self.available_api_versions['v1'], "tokens") + '/' args = urlencode({"github_token": github_token}) - resp = open_url(url, data=args, validate_certs=self.validate_certs, method="POST", http_agent=user_agent()) + resp = open_url(url, data=args, validate_certs=self.validate_certs, method="POST", http_agent=user_agent(), timeout=self.timeout) data = json.loads(to_text(resp.read(), errors='surrogate_or_strict')) return data diff --git a/lib/ansible/galaxy/collection/concrete_artifact_manager.py b/lib/ansible/galaxy/collection/concrete_artifact_manager.py index 7276026e63c..f2b3a3d1ea6 100644 --- a/lib/ansible/galaxy/collection/concrete_artifact_manager.py +++ b/lib/ansible/galaxy/collection/concrete_artifact_manager.py @@ -68,7 +68,7 @@ class ConcreteArtifactsManager: * retrieving the metadata out of the downloaded artifacts """ - def __init__(self, b_working_directory, validate_certs=True, keyring=None): + def __init__(self, b_working_directory, validate_certs=True, keyring=None, timeout=60): # type: (bytes, bool, str) -> None """Initialize ConcreteArtifactsManager caches and costraints.""" self._validate_certs = validate_certs # type: bool @@ -80,6 +80,7 @@ class ConcreteArtifactsManager: self._b_working_directory = b_working_directory # type: bytes self._supplemental_signature_cache = {} # type: Dict[str, str] self._keyring = keyring # type: str + self.timeout = timeout # type: int @property def keyring(self): @@ -202,6 +203,7 @@ class ConcreteArtifactsManager: self._b_working_directory, expected_hash=None, # NOTE: URLs don't support checksums validate_certs=self._validate_certs, + timeout=self.timeout ) except URLError as err: raise_from( @@ -424,7 +426,7 @@ def _extract_collection_from_git(repo_url, coll_ver, b_path): # FIXME: use random subdirs while preserving the file names -def _download_file(url, b_path, expected_hash, validate_certs, token=None): +def _download_file(url, b_path, expected_hash, validate_certs, token=None, timeout=60): # type: (str, bytes, Optional[str], bool, GalaxyToken) -> bytes # ^ NOTE: used in download and verify_collections ^ b_tarball_name = to_bytes( @@ -447,6 +449,7 @@ def _download_file(url, b_path, expected_hash, validate_certs, token=None): validate_certs=validate_certs, headers=None if token is None else token.headers(), unredirected_headers=['Authorization'], http_agent=user_agent(), + timeout=timeout ) with open(b_file_path, 'wb') as download_file: # type: BinaryIO