Skip to content

Don't read PyPI cooldown upload time from stale JSON API documents - #860

Merged
simonchrz merged 2 commits into
git-pkgs:mainfrom
rvyyv-n:fix/issue-859
Dec 1, 2026
Merged

simonchrz merged 2 commits into
git-pkgs:mainfrom
rvyyv-n:fix/issue-859

Conversation

@rvyyv-n

@rvyyv-n rvyyv-n commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #859

Root Cause

With cache_metadata and cooldown enabled, PyPIHandler.versionInCooldown in internal/handler/pypi.go read release upload times from res.body even when res.stale was true. When the upstream JSON API failed (e.g. 404 or 500) and the metadata cache fell back to a stale copy past metadata_ttl, the stale copy's upload time decided whether the download was allowed or withheld (and could be written via storeUploadTime), even though that copy may have come from a previous upstream.

Fix

  • In internal/handler/pypi.go, return res.upstreamErr from the readListed callback in versionInCooldown whenever res.stale is true instead of parsing the stale document.
  • When res.upstreamErr is ErrUpstreamNotFound (404), versionInCooldown calls seen(nil) and allows the file through unchecked without storing a stale upload time.
  • For other upstream errors (such as 500), versionInCooldown returns the error so uncached downloads fail with 502 (failed to check cooldown).
  • Added regression test TestPyPICooldownDoesNotUseAStaleDocument in internal/handler/pypi_digest_test.go covering both 500 (stale doc outside cooldown -> 502) and 404 (stale doc inside cooldown -> 200 unchecked).

Verification

  • go test -v ./internal/handler -run TestPyPICooldownDoesNotUseAStaleDocument: PASS (2/2 subtests; also verified 10/10 with -count=10)
  • go test -count=1 ./internal/metadata/pypi/...: PASS
  • go vet ./...: PASS
  • gofmt -w internal/handler/pypi.go internal/handler/pypi_digest_test.go: clean
  • Note (Windows host): the pre-existing TestPyPIDownloadIsNotCheckedAgainstAStaleDocument intermittently flakes on Windows because its time.Nanosecond MetadataTTL can remain unexpired within a single Windows clock tick (verified to also fail on unmodified main and pass on other runs with -count=5); all other PyPI and cooldown handler tests pass.

Summary by CodeRabbit

  • Bug Fixes
    • PyPI downloads no longer rely on stale package metadata when the package API returns an error. Temporary upstream errors now cause the download to fail with an error, while a not-found response still allows the download to proceed without validating against stale metadata. This prevents outdated metadata from being used after temporary API failures.

When the JSON API fails and the metadata cache falls back to a stale copy, versionInCooldown now returns res.upstreamErr instead of parsing the stale document's upload time. ErrUpstreamNotFound continues to serve the file unchecked, while other upstream errors fail uncached downloads with 502.

Fixes git-pkgs#859
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The PyPI cooldown check returns upstream errors for stale JSON API results instead of checking release times from stale metadata. Tests cover upstream 500 and 404 responses.

Changes: PyPI cooldown handling

Layer / File(s) Summary
Stale metadata error handling
internal/handler/pypi.go, internal/handler/pypi_digest_test.go
versionInCooldown returns the upstream error for stale results before calling seen or parsing release times. Tests verify that a 500 fails with 502 without fetching the artifact, while a 404 allows the artifact to be served.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • git-pkgs/proxy#854 — This change addresses the cooldown path for stale documents. The issue also covers the declaredDigest path, which this change does not clearly modify.

Possibly related PRs

  • git-pkgs/proxy#845 — Both PRs define PyPI behavior when the JSON API document is stale or unavailable.
  • git-pkgs/proxy#760 — Both PRs affect stale-metadata decisions in the PyPI cooldown path and the resulting 502 response.

Suggested labels: bug

Suggested reviewers: simonchrz, andrew

Merge Risk: Minimal · up to 6dc9b

The change stops PyPI cooldown checks from using upload times in stale metadata after an upstream failure. A 404 now lets the download through unchecked, and other failures return 502, as the linked issue requests. No merge-blocking risk is evident.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: PyPI cooldown checks no longer read upload times from stale JSON API documents. The raw summary confirms that versionInCooldown now returns res.upstreamErr for stale …
Linked Issues check Passed [#859] Issue #859 asks that, with cache_metadata and cooldown on, versionInCooldown not read upload times from a stale JSON API document. It asks for these results: serve the file unchecked when res.u…
Out of Scope Changes check Passed The change set, per the summary, touches internal/handler/pypi.go (the versionInCooldown stale-result handling and its comments) and adds the regression test and helper pypiStaleMetadataSetup in inter…
Full details: Title check

Explanation

The title states the main change: PyPI cooldown checks no longer read upload times from stale JSON API documents. The raw summary confirms that versionInCooldown now returns res.upstreamErr for stale results instead of passing nil to seen, and the objectives describe the same behavior. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

[#859] Issue #859 asks that, with cache_metadata and cooldown on, versionInCooldown not read upload times from a stale JSON API document. It asks for these results: serve the file unchecked when res.upstreamErr is ErrUpstreamNotFound, and return the error otherwise (uncached downloads get 502). The change summary for internal/handler/pypi.go states that stale results now return res.upstreamErr before the callback or release-time parsing runs, and that seen receives nil only when the package has no JSON API document. The regression test TestPyPICooldownDoesNotUseAStaleDocument in internal/handler/pypi_digest_test.go covers a 500 case (502) and a 404 case (200 unchecked). The current head's declaredDigest code, read directly, already treats a stale result as unchecked and uses cmp.Or(res.err, res.upstreamErr) to classify the error, which matches the same stale-handling pattern. Limitation: the versionInCooldown body could not be read directly. The diff read failed because changed blobs are missing from the checkout, and the file read was truncated before that function. The verdict relies on the change summary, the PR description, and the test description. The author's test results were not re-run here. The issue does not request artifact-cache changes, and none are claimed.

Full details: Out of Scope Changes check

Explanation

The change set, per the summary, touches internal/handler/pypi.go (the versionInCooldown stale-result handling and its comments) and adds the regression test and helper pypiStaleMetadataSetup in internal/handler/pypi_digest_test.go. Both changes relate directly to the behavior requested by #859. The PR description's note on the flaky Windows test TestPyPIDownloadIsNotCheckedAgainstAStaleDocument describes a pre-existing issue and does not add code. No unrelated file or change appears in the summary. Limitation: the direct diff could not be read because changed blobs are missing from the checkout, so this verdict relies on the change summary, which lists only these two files.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1f772ef9-081d-4252-b93c-84fe113a78d5
Commits

Reviewing files that changed from the base of the PR and between e81b902 and 6dc9b87.

Files selected for processing: 1

  • internal/handler/pypi_digest_test.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@simonchrz
simonchrz merged commit 47d7d29 into git-pkgs:main Dec 1, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PyPI cooldown reads the upload time from a stale JSON API document

2 participants