uv: Non-deterministic 403 errors using private pypi server with `uv pip install -r requirements.txt`
Summary
Hello, we are witnessing an issue with uv pip install -r requirements.txt. We are using an instance of pypicloud backed by S3 (we are aware it’s unmaintained, but it still works with pip, so it should work with uv pip too.)
To reproduce, we can have a very simple requirements.txt file that looks like this:
--index-url https://pypi.example.com
package-from-private-registry
Set up uv:
% uv --version
uv 0.7.2 (Homebrew 2025-04-30)
% uv venv
Using CPython 3.13.2
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
(Outputs edited)
- Now invoke
uv pip install -r requirements.txt:
% uv pip install -r requirements.txt
Using Python 3.11.9 environment at: /Users/creston/.local/share/virtualenvs/creston-VATBnwns
⠴ package-from-private-registry==1.61.0
error: Failed to fetch: `https://pypi.example.com/api/package/package-from-private-registry/package-from-private-registry-1.61.0-py3-none-any.whl`
Caused by: HTTP status client error (403 Forbidden) for url (https://pypi-example.s3.amazonaws.com/pypi/a7ea/package-from-private-registry/package-from-private-registry-1.61.0-py3-none-any.whl?AWSAccessKeyId=scrubbed&Signature=scrubbed&x-amz-security-token=scrubbed&Expires=1746729457)
- Now invoke it again (identical command, don’t do anything else)
% uv pip install -r requirements.txt
Using Python 3.11.9 environment at: /Users/creston/.local/share/virtualenvs/creston-VATBnwns
⠴ package-from-private-registry==1.61.0
error: Failed to fetch: `https://pypi.example.com/api/package/package-from-private-registry/package-from-private-registry-1.61.0-py3-none-any.whl`
Caused by: HTTP status client error (403 Forbidden) for url (https://pypi-example.s3.amazonaws.com/pypi/a7ea/package-from-private-registry/package-from-private-registry-1.61.0-py3-none-any.whl?AWSAccessKeyId=scrubbed&Signature=scrubbed&x-amz-security-token=scrubbed&Expires=1746729457)
Exact same result.
- But do it again a third time (again, identical command) and it suddenly works!
% uv pip install -r requirements.txt
Using Python 3.11.9 environment at: /Users/creston/.local/share/virtualenvs/creston-VATBnwns
Resolved 38 packages in 2.79s
Prepared 29 packages in 575ms
Uninstalled 2 packages in 6ms
Installed 29 packages in 99ms
+ attrs==25.3.0
+ boto3==1.38.10
+ botocore==1.38.10
+ cffi==1.17.1
+ deprecated==1.2.18
+ deprecation==2.1.0
+ importlib-metadata==8.6.1
+ jmespath==1.0.1
+ minject==1.5.0
+ opentelemetry-api==1.32.1
+ prometheus-client==0.21.1
+ pycparser==2.22
+ pyjwt==2.10.1
+ pymemcache==4.0.0
+ pynacl==1.5.0
+ redis==6.0.0
- requests==2.31.0
+ requests==2.32.3
+ s3transfer==0.12.0
+ sentry-sdk==2.27.0
+ types-protobuf==6.30.2.20250506
+ types-pyyaml==6.0.12.20250402
+ types-requests==2.32.0.20250328
- typing-extensions==4.12.2
+ typing-extensions==4.13.2
+ wrapt==1.17.2
+ zipp==3.21.0
This issue started recently (perhaps in the last 1-2 days). Before we never had trouble with this setup. But it’s not clear that anything has changed with uv. Regardless, it’s reproducible on multiple versions of uv including the latest 0.7.2.
Sometimes the failure happens 0 or 1 times, but never more than twice. Not sure if that’s a clue or just coincidence.
Platform
macOS arm64 15.3.2 (24D81)
Version
0.7.2 (and others)
Python version
3.11.9 (and others)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 15 (8 by maintainers)
Thanks!
So, it sounds like, server-side, there’s non-deterministic return behavior when a connection is re-used.
I believe it is a bug (feature?) of
reqwest. It appears to “cache” aHEADerror response for a subsequentGETrequest to the same URL. It’s reasonable to assume the response would be the same. The nondeterminism is because it only happens when it reuses a connection. I haven’t looked into the reqwest source to confirm, but I can reproduce the issue with the following code:Cargo.toml
This code is also non-deterministic. Multiple runs will sometimes return 403 and sometimes 200. But if you remove the
HEADrequest, it’s always a 200 response.I suspect this is probably desirable behavior in 99.9% of cases that use reqwest, and it’s a bad idea to depend on HEAD and GET returning different status codes in the way that
uvdoes. However, it’s also clearly a niche situation that only applies to URLs like S3 pre-signed URLs. Arguably S3 is doing the wrong thing by rejecting a HEAD request.In the uv source, the corresponding requests are:
EDIT:
.pool_max_idle_per_host(0)reliably fixes the issue. The connection can never be reused, so the GET request never returns 403. This is not configurable inuv. One workaround would be to make this configurable, but that feels like a bad long-term solution..header(reqwest::header::CONNECTION, "close")to the HEAD request so that it is forced to close the connection. (The response will not be re-used.) This also reliably solves the problem, even withpool_max_idle_per_host(20).I’ve looked at #2025, but that seems unrelated. It is true that you cannot make a
HEADrequest for pre-signed S3 URLs. The linked PR fixes that issue by making the HEAD request optional. My (uninformed) tour through the code makes me think that is still the case, but perhaps there was a regression somewhere else. I haven’t found auvversion that works, but I also haven’t looked particularly hard.Update: I went all the way back to
uv==0.2.0and every minor version had the same error:example.com/my-package/my_package-0.13.0-py3-none-any.whl302 Foundredirect to a pre-signed S3 URL. Anyone with the URL can download the object even if there is no public ACL--reinstall --refresh --no-cache) will still succeed after a few attempts, which makes me think it’s either (1) not a caching issue or (2)--no-cacheis a lieDigging in the history… found a related issue