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)

  1. 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)
  1. 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.

  1. 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)

Most upvoted comments

Just poking in briefly: reqwest doesn’t do any caching of responses at all. What you see is what the server sent.

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” a HEAD error response for a subsequent GET request 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

[package]
name = "reqwest-test"
version = "0.1.0"
edition = "2024"

[dependencies]
reqwest = { version = "0.12.7", features = ["json"] }
tokio = { version = "1", features = ["full"] }

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // whatever package URL, you can also use any pre-signed S3 URL here, the behavior will be
    // reproducible (i.e., the 302 redirect is not relevant)
    let url = "https://pypi.example.com/api/package/my-package/my_package-0.13.0-py3-none-any.whl";
    println!("Downloading: {}", url);
    
    let client = reqwest::ClientBuilder::new()
        .pool_max_idle_per_host(20) // same pool size as uv
        .build()?;
    let head_response = client.head(url).send().await;
    println!("HEAD response: {:?}", head_response); // returns 403 for pre-signed S3 urls
    let response = client.get(url).send().await?;
    println!("GET response: {:?}", &response); // sometimes returns 403 because it's caching the HEAD request(?)
    response.bytes().await?;
    Ok(())
}

This code is also non-deterministic. Multiple runs will sometimes return 403 and sometimes 200. But if you remove the HEAD request, 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 uv does. 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:

  • Setting .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 in uv. One workaround would be to make this configurable, but that feels like a bad long-term solution.
  • An alternative, more elegant solution, would be to simply add .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 with pool_max_idle_per_host(20).
  • A more robust solution, but I’m not sure how it would be implemented, is to have it only close the connection when it gets an error status, instead of re-using it.

I’ve looked at #2025, but that seems unrelated. It is true that you cannot make a HEAD request 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 a uv version that works, but I also haven’t looked particularly hard.

Update: I went all the way back to uv==0.2.0 and every minor version had the same error:

creston@creston-macbook-pro uv-test % uv clean && uv venv && uv pip install uv==0.2.0 && uv run uv --version && uv run uv pip install -r requirements.txt
Clearing cache at: /Users/creston/.cache/uv
Removed 24 files (22.8MiB)
Using CPython 3.13.2
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
Resolved 1 package in 205ms
Prepared 1 package in 789ms
Installed 1 package in 1ms
 + uv==0.2.0
uv 0.2.0 (0a87391d5 2024-05-22)
⠼ my-package==0.13.0                                                                                                                                                    error: Failed to download `my-package==0.13.0`
  Caused by: HTTP status client error (403 Forbidden) for url ...
  • pypicloud generates a URL for each package (example.com/my-package/my_package-0.13.0-py3-none-any.whl
  • This is a 302 Found redirect to a pre-signed S3 URL. Anyone with the URL can download the object even if there is no public ACL
  • I can confirm that clicking the pre-signed S3 URL and opening it in my web browser returns a 200 success and downloads the package
  • The tokens expire after 24 hours, which is longer than the tests. We have seen issues where things that cache full S3 url (and not the redirect URL) will fail after 24 hours, but that’s not the case here since we’re dealing with time scales on the order of seconds.
  • I have been playing around with uv’s caching flags to try and get it to consistently fail, but even the most aggressive flags (--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-cache is a lie

Digging in the history… found a related issue