Skip to content

[ci] Ask the mirror for one index representation - #65593

Merged
elliot-barn merged 2 commits into
masterfrom
elliot-barn/proxy-accept-html
Aug 19, 2026
Merged

elliot-barn merged 2 commits into
masterfrom
elliot-barn/proxy-accept-html

Conversation

@elliot-barn

Copy link
Copy Markdown
Collaborator

reef: raydepsets tests went red on master in postmerge 19272, and the mirror caused
it rather than failing to prevent it:

WARNING: Skipping page http://172.16.0.2:35999/simple/exceptiongroup/ because the
GET request got Content-Type: application/vnd.pypi.simple.v1+json. The only
supported Content-Type is text/html
ERROR: Could not find a version that satisfies the requirement exceptiongroup==1.3.1
(from versions: none)

The mirror caches /simple/ keyed on the URL alone, with no Vary, so whichever
representation is fetched first is what every later client receives. The proxy
forwarded the client's Accept, so uv asking for PEP 691 JSON stored JSON, and then
whl_library's pip -- which reads only text/html -- skipped the page entirely. The
"from versions: none" that follows looks like a bad pin, which is what makes this worth
a comment in the code: exceptiongroup 1.3.1 is the current release and is present in
PyPI's HTML index.

Pins Accept to text/html. HTML rather than the JSON that product's copy of this proxy
pins, because that pip cannot read JSON at all while uv reads both.

Pinning stops new entries going in but cannot evict the ones already cached, since
Accept is not part of the mirror's cache key. Clearing those means deleting the
cache/pypi.org/simple/ prefix from the bucket, at a cost of one re-fetch per page. So
the proxy now logs loudly when it asks for HTML and gets something else, because the
client-side symptom otherwise points at the wrong thing entirely.

Testing: started the proxy against a stub upstream that records the Accept it receives
and replies with JSON. A client requesting application/vnd.pypi.simple.v1+json results
in the upstream seeing text/html, and the stale-entry warning fires. Both asserted.

AI assistance (Claude Code) was used for this change.

`reef: raydepsets tests` went red on master in postmerge 19272, and the mirror caused
it rather than failing to prevent it:

  WARNING: Skipping page http://172.16.0.2:35999/simple/exceptiongroup/ because the
  GET request got Content-Type: application/vnd.pypi.simple.v1+json. The only
  supported Content-Type is text/html
  ERROR: Could not find a version that satisfies the requirement exceptiongroup==1.3.1
         (from versions: none)

The mirror caches `/simple/` keyed on the URL alone, with no Vary, so whichever
representation is fetched first is what every later client receives. The proxy
forwarded the client's Accept, so uv asking for PEP 691 JSON stored JSON, and then
whl_library's pip -- which reads only text/html -- skipped the page entirely. The
"from versions: none" that follows looks like a bad pin, which is what makes this worth
a comment in the code: exceptiongroup 1.3.1 is the current release and is present in
PyPI's HTML index.

Pins Accept to text/html. HTML rather than the JSON that product's copy of this proxy
pins, because that pip cannot read JSON at all while uv reads both.

Pinning stops new entries going in but cannot evict the ones already cached, since
Accept is not part of the mirror's cache key. Clearing those means deleting the
cache/pypi.org/simple/ prefix from the bucket, at a cost of one re-fetch per page. So
the proxy now logs loudly when it asks for HTML and gets something else, because the
client-side symptom otherwise points at the wrong thing entirely.

Testing: started the proxy against a stub upstream that records the Accept it receives
and replies with JSON. A client requesting application/vnd.pypi.simple.v1+json results
in the upstream seeing text/html, and the stale-entry warning fires. Both asserted.

AI assistance (Claude Code) was used for this change.

Signed-off-by: elliot-barn <[email protected]>
@elliot-barn
elliot-barn requested review from a team as code owners August 19, 2026 17:30

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the PyPI index proxy to always request HTML representations from the upstream mirror, preventing cache poisoning issues where JSON responses are cached and subsequently break clients like pip that only support HTML. It also adds a warning log when a non-HTML content type is returned. The review feedback recommends making the content-type check more robust by handling case-insensitivity and ensuring a fallback value to prevent potential TypeErrors or false-positive warnings.

Comment thread ci/pypi_index_proxy.py Outdated
Comment on lines +94 to +107
content_type = response.headers.get("content-type", "text/html")
# Asking for HTML and getting something else means the mirror is holding a
# representation cached before this pinning landed. Pinning stops new entries going
# in but cannot evict old ones, since the Accept header is not part of the mirror's
# cache key -- those have to be deleted from the cache prefix. Say so, because the
# symptom at the client is the misleading "from versions: none".
if response.status_code == 200 and "html" not in content_type:
print(
f"pypi proxy: {upstream} returned {content_type} for an HTML request; "
"this is a stale mirror cache entry and clients that only read HTML will "
"skip it. Delete the cache/pypi.org/simple/ prefix to clear it.",
flush=True,
)
return Response(body, status_code=response.status_code, media_type=content_type)

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

To make the content-type check more robust, we should handle two potential edge cases:

  1. Case Insensitivity: HTTP header values are case-insensitive. If a server returns TEXT/HTML or Text/Html, "html" not in content_type would evaluate to True and trigger a false-positive warning. Converting the string to lowercase before checking avoids this.
  2. None/Empty Values: If the content-type header is present but empty or None (which can happen in some mock/test environments), .get("content-type", "text/html") will return None, leading to a TypeError when performing the in check. Using or "text/html" ensures we always have a valid string.
Suggested change
content_type = response.headers.get("content-type", "text/html")
# Asking for HTML and getting something else means the mirror is holding a
# representation cached before this pinning landed. Pinning stops new entries going
# in but cannot evict old ones, since the Accept header is not part of the mirror's
# cache key -- those have to be deleted from the cache prefix. Say so, because the
# symptom at the client is the misleading "from versions: none".
if response.status_code == 200 and "html" not in content_type:
print(
f"pypi proxy: {upstream} returned {content_type} for an HTML request; "
"this is a stale mirror cache entry and clients that only read HTML will "
"skip it. Delete the cache/pypi.org/simple/ prefix to clear it.",
flush=True,
)
return Response(body, status_code=response.status_code, media_type=content_type)
content_type = response.headers.get("content-type") or "text/html"
# Asking for HTML and getting something else means the mirror is holding a
# representation cached before this pinning landed. Pinning stops new entries going
# in but cannot evict old ones, since the Accept header is not part of the mirror's
# cache key -- those have to be deleted from the cache prefix. Say so, because the
# symptom at the client is the misleading "from versions: none".
if response.status_code == 200 and "html" not in content_type.lower():
print(
f"pypi proxy: {upstream} returned {content_type} for an HTML request; "
"this is a stale mirror cache entry and clients that only read HTML will "
"skip it. Delete the cache/pypi.org/simple/ prefix to clear it.",
flush=True,
)
return Response(body, status_code=response.status_code, media_type=content_type)

@elliot-barn elliot-barn added the go add ONLY when ready to merge, run all tests label Aug 19, 2026
Review feedback on #65593. Header values are case-insensitive, so an upstream replying
TEXT/HTML would have tripped the warning and told the reader to delete cache entries
that are in fact correct. `or` rather than a default additionally covers a header that
is present but empty.

Tested: added a case where the stub upstream replies "TEXT/HTML; charset=UTF-8" and
asserted the warning does not fire; the existing JSON case still fires it.

Signed-off-by: elliot-barn <[email protected]>
@ray-gardener ray-gardener Bot added the devprod label Aug 19, 2026
elliot-barn added a commit that referenced this pull request Aug 19, 2026
install_pypi_proxy.sh and pypi_index_proxy.py were pulled onto this branch while testing
the agent hook, and git checkout <branch> -- <file> stages what it writes, so a later
commit swept them in. They belong to #65597 and #65593. This branch changes the profile
script only.

Signed-off-by: elliot-barn <[email protected]>
@elliot-barn
elliot-barn merged commit 84a62d2 into master Aug 19, 2026
5 of 6 checks passed
@elliot-barn
elliot-barn deleted the elliot-barn/proxy-accept-html branch August 19, 2026 21:11
elliot-barn added a commit that referenced this pull request Aug 19, 2026
## Description

Bazel's downloader is a separate surface from pip's index, and nothing
was pointing it at
the mirror. `rules_python` declares its bootstrap wheels as
`http_archive` with literal
`files.pythonhosted.org` URLs, and `http_archive` never consults
`PIP_INDEX_URL` — so no
index setting we have reaches them.

That is what failed microcheck 52470 and the docs-example jobs on
postmerge 19272 —
bazel's own Java downloader, not pip:

```
WARNING: Download from https://files.pythonhosted.org/packages/…/click-8.0.1-py3-none-any.whl failed
Error in download_and_extract: java.io.IOException: Error downloading […click-8.0.1…]
ERROR: Error computing the main repository mapping: … no such package '@@py_deps_py310//'
```

It fails during repository mapping, so a single 502 takes the whole
analysis phase with
it rather than one target.

## What changed

When the mirror is reachable, the profile script now writes a bazel
downloader config and
points `~/.bazelrc` at it:

```
rewrite files\.pythonhosted\.org/(.*) mirror.ci.ray.io/files.pythonhosted.org/$1
```

```
common --experimental_downloader_config=/tmp/rayci_bazel_downloader.cfg
```

Bazel rewrites the URL before fetching, so those archives come from the
mirror. This is a
different mechanism from the index proxy: no proxy process, no content
negotiation, no
per-job address — which is also why it is set up **before** the proxy
checks. An image
that carries no proxy still gets its bazel downloads mirrored.

Written per job rather than committed to `.bazelrc` because the hostname
is VPC-internal:
a checkout outside CI would rewrite its downloads to a name that does
not resolve,
turning a working build into a broken one.

## Why this is worth having beyond the observed failure

Bazel's downloader also fetches the github.com release tarballs, and the
mirror is
host-prefixed so it already serves those. Extending the same config to
github is a
one-line follow-up, and would have covered the `rules_python` tarball
429s from the
GitHub incident on 2026-08-17. Left out here to keep the change to the
surface actually
measured.

## Related issues

Upstream cause: pypi/support#11895. Not a duplicate: no open PR touches
bazel's downloader
configuration — `grep -rn experimental_downloader_config` comes back
empty on master.
#65593, #65597 and #65598 all concern the pip/uv index path and do not
overlap with this
mechanism.

## Testing

Verified against bazel 7.5.0 locally, with a throwaway workspace whose
`http_archive`
points at the same `click-8.0.1` wheel that failed in CI:

- **The rewrite applies and the scheme is preserved.** With the target
host set to a
  deliberately nonexistent name, bazel reports
`Download from https://mirror.invalid/files.pythonhosted.org/packages/…`
— confirming
  both the URL shape and that `https` carries over.
- **The generated config is accepted by bazel**, escaping intact: the
config this script
  writes produced
`Download from
https://127.0.0.1:38120/files.pythonhosted.org/packages/…`, i.e. parsed
  and applied rather than rejected.
- **An identity rewrite does not break a working download**:
`All external dependencies for the requested targets fetched
successfully`.
- **`common` is a safe scope** for the flag — `bazel version` and `bazel
query` both run
  clean with it in `~/.bazelrc`, no unrecognized-option error.
- **Guards**: an `http://` mirror prints
`mirror is not https, leaving bazel downloads on the origin` and writes
no config,
because bazel would otherwise address an http-only host as https and
fail in a way that
looks like a broken mirror. An unreachable mirror bails at the existing
probe before any
  bazel setup. The bazelrc line is appended only once.
- `pre-commit` (shellcheck) clean.

Not verified: a real CI job against the real mirror, which is what the
first build on this
branch shows. The failure mode if the mirror cannot serve an artifact is
a bazel download
error naming `mirror.ci.ray.io`, which is distinguishable at a glance
from the origin
failing.

AI assistance (Claude Code) was used for this change.

---------

Signed-off-by: elliot-barn <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
`reef: raydepsets tests` went red on master in postmerge 19272, and the
mirror caused
it rather than failing to prevent it:

WARNING: Skipping page http://172.16.0.2:35999/simple/exceptiongroup/
because the
GET request got Content-Type: application/vnd.pypi.simple.v1+json. The
only
  supported Content-Type is text/html
ERROR: Could not find a version that satisfies the requirement
exceptiongroup==1.3.1
         (from versions: none)

The mirror caches `/simple/` keyed on the URL alone, with no Vary, so
whichever
representation is fetched first is what every later client receives. The
proxy
forwarded the client's Accept, so uv asking for PEP 691 JSON stored
JSON, and then
whl_library's pip -- which reads only text/html -- skipped the page
entirely. The
"from versions: none" that follows looks like a bad pin, which is what
makes this worth
a comment in the code: exceptiongroup 1.3.1 is the current release and
is present in
PyPI's HTML index.

Pins Accept to text/html. HTML rather than the JSON that product's copy
of this proxy
pins, because that pip cannot read JSON at all while uv reads both.

Pinning stops new entries going in but cannot evict the ones already
cached, since
Accept is not part of the mirror's cache key. Clearing those means
deleting the
cache/pypi.org/simple/ prefix from the bucket, at a cost of one re-fetch
per page. So
the proxy now logs loudly when it asks for HTML and gets something else,
because the
client-side symptom otherwise points at the wrong thing entirely.

Testing: started the proxy against a stub upstream that records the
Accept it receives
and replies with JSON. A client requesting
application/vnd.pypi.simple.v1+json results
in the upstream seeing text/html, and the stale-entry warning fires.
Both asserted.

AI assistance (Claude Code) was used for this change.

---------

Signed-off-by: elliot-barn <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## Description

Bazel's downloader is a separate surface from pip's index, and nothing
was pointing it at
the mirror. `rules_python` declares its bootstrap wheels as
`http_archive` with literal
`files.pythonhosted.org` URLs, and `http_archive` never consults
`PIP_INDEX_URL` — so no
index setting we have reaches them.

That is what failed microcheck 52470 and the docs-example jobs on
postmerge 19272 —
bazel's own Java downloader, not pip:

```
WARNING: Download from https://files.pythonhosted.org/packages/…/click-8.0.1-py3-none-any.whl failed
Error in download_and_extract: java.io.IOException: Error downloading […click-8.0.1…]
ERROR: Error computing the main repository mapping: … no such package '@@py_deps_py310//'
```

It fails during repository mapping, so a single 502 takes the whole
analysis phase with
it rather than one target.

## What changed

When the mirror is reachable, the profile script now writes a bazel
downloader config and
points `~/.bazelrc` at it:

```
rewrite files\.pythonhosted\.org/(.*) mirror.ci.ray.io/files.pythonhosted.org/$1
```

```
common --experimental_downloader_config=/tmp/rayci_bazel_downloader.cfg
```

Bazel rewrites the URL before fetching, so those archives come from the
mirror. This is a
different mechanism from the index proxy: no proxy process, no content
negotiation, no
per-job address — which is also why it is set up **before** the proxy
checks. An image
that carries no proxy still gets its bazel downloads mirrored.

Written per job rather than committed to `.bazelrc` because the hostname
is VPC-internal:
a checkout outside CI would rewrite its downloads to a name that does
not resolve,
turning a working build into a broken one.

## Why this is worth having beyond the observed failure

Bazel's downloader also fetches the github.com release tarballs, and the
mirror is
host-prefixed so it already serves those. Extending the same config to
github is a
one-line follow-up, and would have covered the `rules_python` tarball
429s from the
GitHub incident on 2026-08-17. Left out here to keep the change to the
surface actually
measured.

## Related issues

Upstream cause: pypi/support#11895. Not a duplicate: no open PR touches
bazel's downloader
configuration — `grep -rn experimental_downloader_config` comes back
empty on master.
ray-project#65593, ray-project#65597 and ray-project#65598 all concern the pip/uv index path and do not
overlap with this
mechanism.

## Testing

Verified against bazel 7.5.0 locally, with a throwaway workspace whose
`http_archive`
points at the same `click-8.0.1` wheel that failed in CI:

- **The rewrite applies and the scheme is preserved.** With the target
host set to a
  deliberately nonexistent name, bazel reports
`Download from https://mirror.invalid/files.pythonhosted.org/packages/…`
— confirming
  both the URL shape and that `https` carries over.
- **The generated config is accepted by bazel**, escaping intact: the
config this script
  writes produced
`Download from
https://127.0.0.1:38120/files.pythonhosted.org/packages/…`, i.e. parsed
  and applied rather than rejected.
- **An identity rewrite does not break a working download**:
`All external dependencies for the requested targets fetched
successfully`.
- **`common` is a safe scope** for the flag — `bazel version` and `bazel
query` both run
  clean with it in `~/.bazelrc`, no unrecognized-option error.
- **Guards**: an `http://` mirror prints
`mirror is not https, leaving bazel downloads on the origin` and writes
no config,
because bazel would otherwise address an http-only host as https and
fail in a way that
looks like a broken mirror. An unreachable mirror bails at the existing
probe before any
  bazel setup. The bazelrc line is appended only once.
- `pre-commit` (shellcheck) clean.

Not verified: a real CI job against the real mirror, which is what the
first build on this
branch shows. The failure mode if the mirror cannot serve an artifact is
a bazel download
error naming `mirror.ci.ray.io`, which is distinguishable at a glance
from the origin
failing.

AI assistance (Claude Code) was used for this change.

---------

Signed-off-by: elliot-barn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

devprod go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant