diff --git a/CHANGES/2717.bugfix b/CHANGES/2717.bugfix new file mode 100644 index 000000000..c5bb7303c --- /dev/null +++ b/CHANGES/2717.bugfix @@ -0,0 +1,8 @@ +Fixed a 500 (``UnboundLocalError: cannot access local variable '_'``) in the content app +``pulpcore/content/handler.py`` ``_match_and_stream``. Patch 0080 used a bare ``_`` as the +throwaway target of three tuple unpacks, which made ``_`` a function-local name and shadowed +gettext's ``_``. Requests for a distribution with no repository, publication, or remote reached +the "Distribution is not pointing to..." branch without executing any of those unpacks, so the +gettext call raised ``UnboundLocalError`` and the content app returned 500 instead of 404 +(PULP-2717). The same shadowing turned upstream fetch failures into +``TypeError: 'QuerySet' object is not callable``. The unpacks now use ``_lazy_queryset``. diff --git a/images/assets/patches/0080-Optimize-read-only-content-queries.patch b/images/assets/patches/0080-Optimize-read-only-content-queries.patch index 7294e974c..8aac89244 100644 --- a/images/assets/patches/0080-Optimize-read-only-content-queries.patch +++ b/images/assets/patches/0080-Optimize-read-only-content-queries.patch @@ -41,7 +41,7 @@ - ) - .filter( - content__in=publication.repository_version.content, -+ rows, _ = await self._retry_content_artifact_query( ++ rows, _lazy_queryset = await self._retry_content_artifact_query( + lambda: sync_to_async(content_query_experiment)( + "pass_through_file_lookup", + publication.repository_version, @@ -65,7 +65,7 @@ - lambda: ContentArtifact.objects.filter( - content__in=repo_version.content, relative_path=index_path - ).aexists() -+ probe_rows, _ = await self._retry_content_artifact_query( ++ probe_rows, _lazy_queryset = await self._retry_content_artifact_query( + lambda: sync_to_async(content_query_experiment)( + "index_probe", + repo_version, @@ -86,7 +86,7 @@ - lambda: ContentArtifact.objects.select_related( - "artifact", "artifact__pulp_domain" - ).aget(content__in=repo_version.content, relative_path=original_rel_path) -+ rows, _ = await self._retry_content_artifact_query( ++ rows, _lazy_queryset = await self._retry_content_artifact_query( + lambda: sync_to_async(content_query_experiment)( + "file_lookup", + repo_version, diff --git a/pulp_service/pulp_service/tests/functional/test_content_handler.py b/pulp_service/pulp_service/tests/functional/test_content_handler.py index 2c7f5868f..ff8c0f9ff 100644 --- a/pulp_service/pulp_service/tests/functional/test_content_handler.py +++ b/pulp_service/pulp_service/tests/functional/test_content_handler.py @@ -1,4 +1,5 @@ import json +import uuid import requests @@ -24,3 +25,24 @@ def test_artifact_size_header_on_pull_through_cache( response = requests.get(f"{distro.base_url}{package_filename}") assert response.headers.get("X-PULP-ARTIFACT-SIZE") + + +def test_empty_distribution_returns_404(file_bindings, gen_object_with_cleanup): + """A distribution with no repository, publication, or remote must 404, not 500. + + Regression test for PULP-2717. Patch 0080 used a bare ``_`` as the throwaway target of + three tuple unpacks in ``Handler._match_and_stream``, which made ``_`` a function-local + name and shadowed gettext's ``_``. A distribution with nothing configured reaches the + "Distribution is not pointing to..." branch without executing any of those unpacks, so + the gettext call raised ``UnboundLocalError`` and the content app answered 500. In + production this hit every request for a stale COPR pass-through distribution. + """ + distro = gen_object_with_cleanup( + file_bindings.DistributionsFileApi, + {"name": str(uuid.uuid4()), "base_path": str(uuid.uuid4())}, + ) + + response = requests.get(f"{distro.base_url}missing/path", timeout=30) + + assert response.status_code == 404, response.text + assert "Distribution is not pointing to" in response.reason