Skip to content

PULP-2717: Fix UnboundLocalError in content app _match_and_stream - #1712

Open
decko wants to merge 1 commit into
pulp:mainfrom
decko:pulp-2717-unbound-local-var
Open

decko wants to merge 1 commit into
pulp:mainfrom
decko:pulp-2717-unbound-local-var

Conversation

@decko

@decko decko commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Problem

Production content app returns 500 instead of 404 for distributions that have no repository, publication, or remote.

GlitchTip PULP-PROD-5J2: 1,433 events since 2026-11-02, still firing.

UnboundLocalError: cannot access local variable '_' where it is not associated with a value
  pulpcore/content/handler.py:1142 in _match_and_stream

Root cause

Patch 0080-Optimize-read-only-content-queries.patch used a bare _ as the throwaway target of three tuple unpacks inside Handler._match_and_stream:

rows, _ = await self._retry_content_artifact_query(...)        # :987
probe_rows, _ = await self._retry_content_artifact_query(...)  # :1035
rows, _ = await self._retry_content_artifact_query(...)        # :1065

Assigning _ anywhere in a function makes it a function-local name, shadowing gettext's _. A distribution with nothing configured never executes any of those unpacks, so the later call is unbound:

if not any([repository, repo_version, publication, distro.remote]):
    reason = _("Distribution is not pointing to a publication, repository, ...")  # :1142

Result: 500 instead of PathNotResolved (404). Triggered in prod by stale COPR pass-through distributions (public-copr* domains), which is why directory listings under those paths fail.

A second latent defect came from the same shadowing: :1136 in the except ClientResponseError branch would raise TypeError: 'QuerySet' object is not callable whenever _ was bound by an earlier unpack, reporting upstream fetch failures as a misleading 500. Both go away with the rename.

Change

  • images/assets/patches/0080-...patch: rename the discard target to _lazy_queryset in all three unpacks.
  • pulp_service/tests/functional/test_content_handler.py: regression test — a distribution with no repository/publication/remote must return 404 with the "Distribution is not pointing to" reason. This path was previously unreachable in CI, which is why the patch passed review.
  • CHANGES/2717.bugfix.

Validation

  • Patch verified to apply cleanly to pulpcore 3.123.1 with the full patch stack; resulting _match_and_stream no longer stores to _ (AST check) and still loads it only for gettext at 1136/1142.
  • ruff format + make lint-diff clean.
  • The functional test runs in the Tekton deploy-and-test job against the built (patched) image; it is not runnable locally without a Pulp stack.

Fixes PULP-2717

Assisted-by: Opencode (DeepSeek-V4.1-Flash-1017)

Summary by Sourcery

Fix content handler error handling for distributions with no configured content source.

Bug Fixes:

  • Prevent empty content distributions from returning HTTP 500 by restoring the expected 404 response and reason.
  • Avoid gettext failures that could misreport upstream content errors as server errors.

Tests:

  • Add functional coverage for distributions without a repository, publication, or remote.

@decko decko added the ai-assisted Changes assisted by AI label Nov 17, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Rename the tuple-unpack discard targets in the content handler patch so they no longer shadow gettext’s _ function.

  • images/assets/patches/0080-Optimize-read-only-content-queries.patch L44, L68, L89

Documentation

Record the PULP-2717 fix in the changelog.

  • CHANGES/2717.bugfix L1-8

Tests

Add a functional regression test confirming that a distribution without a configured content source returns 404 with the expected reason.

  • pulp_service/pulp_service/tests/functional/test_content_handler.py L2, L28-48

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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 no repository, publication, or remote reaches
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. Same shadowing turned upstream fetch failures into
TypeError: 'QuerySet' object is not callable.

Rename the discard target to `_lazy_queryset` and add a functional regression
test for the empty-distribution path.

Refs: GlitchTip PULP-PROD-5J2 (1,433 events since 2026-11-02)

Assisted-by: Opencode (DeepSeek-V4.1-Flash-1017)
@decko
decko force-pushed the pulp-2717-unbound-local-var branch from 1d4a92a to 802e476 Compare November 17, 2026 12:20

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="pulp_service/pulp_service/tests/functional/test_content_handler.py" line_range="48" />
<code_context>
+    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
</code_context>
<issue_to_address>
**Correct 404 response fails assertion**

When the handler correctly returns a 404 with the distribution message in the response body, `test_empty_distribution_returns_404` fails at line 48: `requests.Response.reason` is the HTTP status phrase (`Not Found`), not the response body containing the distribution message, so the assertion rejects the correct 404.

Check `response.text` for the distribution message instead of `response.reason`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: pulp_service/pulp_service/tests/functional/test_content_handler.py:48


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Correct 404 response fails assertion

When the handler correctly returns a 404 with the distribution message in the response body, test_empty_distribution_returns_404 fails at line 48: requests.Response.reason is the HTTP status phrase (Not Found), not the response body containing the distribution message, so the assertion rejects the correct 404.

Check response.text for the distribution message instead of response.reason.

Prompt for AI agents
In `pulp_service/pulp_service/tests/functional/test_content_handler.py` at line 48:

**Correct 404 response fails assertion**

When the handler correctly returns a 404 with the distribution message in the response body, `test_empty_distribution_returns_404` fails at line 48: `requests.Response.reason` is the HTTP status phrase (`Not Found`), not the response body containing the distribution message, so the assertion rejects the correct 404.

Check `response.text` for the distribution message instead of `response.reason`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted Changes assisted by AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant