Skip to content

Invalidate interpreter cache when virtualenv configuration changes - #1818

Closed
zaniebot wants to merge 8 commits into
mainfrom
zb/interpreter-pyvenv-cache
Closed

zaniebot wants to merge 8 commits into
mainfrom
zb/interpreter-pyvenv-cache

Conversation

@zaniebot

@zaniebot zaniebot commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

Recreating a virtual environment or changing its pyvenv.cfg can leave cached interpreter metadata valid even though Python now reports different site packages or a different base interpreter. In particular, uv run --with can expose system packages after include-system-site-packages is disabled. Windows virtual environment launchers can also retain the same path and copied modification time across Python-version changes.

Include the contents of the applicable pyvenv.cfg files in the interpreter cache key, alongside the executable paths and launcher overrides. Queried and inferred interpreter metadata use the same configuration-sensitive key. Queries bypass the cache when configuration cannot be read, and only publish a result if the executable and configuration remain unchanged while Python runs.

Fixes astral-sh#18510. Related to astral-sh#16165 and #1674, and the earlier approaches in astral-sh#18537 and astral-sh#19570.

@astral-automations-bot astral-automations-bot Bot added area:cache Caching of packages and metadata bug Something isn't working labels Sep 15, 2026
@codspeed

codspeed Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 8 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 54 untouched benchmarks
⏩ 35 skipped benchmarks1


Comparing zb/interpreter-pyvenv-cache (9b70893) with main (06bf2e8)2

Open in CodSpeed

Footnotes

  1. 35 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (b757f15) during the generation of this report, so 06bf2e8 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@zaniebot zaniebot added risk:2 Low, contained risk of minor incorrect behavior with straightforward recovery. priority:2 Important: bounded correctness, release reliability, committed work, or proven performance gains. size:3 Substantial: interacting behaviors, difficult compatibility questions, or a careful migration. labels Sep 22, 2026
@zanieb zanieb self-assigned this Sep 22, 2026
@konsti-openai konsti-openai assigned konstin and unassigned zanieb Sep 25, 2026
@astral-automations-bot
astral-automations-bot Bot force-pushed the zb/interpreter-pyvenv-cache branch from ef323b6 to 7be2dba Compare October 7, 2026 18:52
@zaniebot
zaniebot force-pushed the zb/interpreter-pyvenv-cache branch from 7be2dba to ef934f2 Compare October 7, 2026 19:46
@zaniebot
zaniebot deployed to automations October 7, 2026 20:08 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 7, 2026 20:13 — with GitHub Actions Active
Comment on lines +253 to +255
Err(uv_python::Error::Query(err))
if matches!(root.join("pyvenv.cfg").try_exists(), Ok(false)) =>
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Preserve unrelated interpreter query errors

A missing pyvenv.cfg is normal for system environments supported through UV_PROJECT_ENVIRONMENT. If such an interpreter loses execute permission, this arm now converts PermissionDenied into Ok(None) on Unix too. Environment initialization then selects another interpreter and reports that the directory cannot be recreated, hiding the actionable permission error. Query also includes cache I/O failures unrelated to virtualenv configuration. Restrict this fallback to the Windows launcher failure being handled and propagate unrelated errors.

Comment on lines +112 to +118
context
.venv()
.arg("--clear")
.arg("--python")
.arg("3.13")
.assert()
.success();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Recreate the venv without refreshing the cache under test

context.venv() shares the cache used by context.run(), and the venv command already calls cache_virtualenv, which writes current metadata or evicts the entry for an upgradeable interpreter. Consequently, this recreation removes the stale Windows metadata that the test intends to exercise. Resetting the launcher mtime afterward can additionally invalidate the freshly seeded entry through the existing timestamp check. Recreate with --no-cache or a separate cache while retaining the matching launcher timestamp, so removing configuration-based invalidation causes this regression test to fail.

let absolute = std::path::absolute(executable)?;
let canonical = canonicalize_executable(&absolute)?;
let cache_entry = Self::cache_entry(&absolute, &canonical, cache);
let cache_entry = Self::cache_entry(&absolute, &canonical, cache)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Bypass inferred caching when configuration reads fail

This ? makes configuration-read failures fatal through PythonEnvironment::cache_virtualenv, unlike query_cached, which bypasses caching. For example, a valid Unix venv can still run Python with a directory at bin/pyvenv.cfg alongside its valid root configuration. uv venv --allow-existing retains that directory, creates the environment, then fails here while priming the cache. Trace the key-construction error and skip the inferred cache write, matching the query path. Add coverage through venv creation; the new unreadable-configuration test only exercises Interpreter::query.

Comment thread crates/uv/tests/project/run.rs Outdated
Comment on lines +2027 to +2028
let cached = PythonEnvironment::from_root(context.venv.path(), &cache)?;
let queried = PythonEnvironment::from_root(context.venv.path(), &fresh_cache)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P3] Populate the recreated environment’s cache before comparing metadata

The preceding venv --allow-existing --relocatable changes pyvenv.cfg, producing a new cache key. In this head, create_venv constructs an in-memory interpreter without seeding that entry. Consequently, both cached and queried execute Python, so the field-by-field assertions compare two fresh queries rather than exercising cached metadata. Populate the recreated environment’s entry first and compare a subsequent load against the fresh query, or remove this block from the overlay test.

@zaniebot
zaniebot deployed to automations October 13, 2026 19:37 — with GitHub Actions Active
@astral-automations-bot
astral-automations-bot Bot force-pushed the zb/interpreter-pyvenv-cache branch from 9b70893 to 755bf9d Compare October 23, 2026 13:19
konstin added a commit to astral-sh/uv that referenced this pull request Nov 17, 2026
Record the libc path reported by the interpreter probe and refresh when
something changes. The cache mechanism added here is generic, so we
can't add more paths in the future, possibly without bumping the cache
format.

Bumps the interpreter cache format to store the new metadata. The stack
above needs to be merged as one, so we don't bump multiple times.

Related issue [#14268](#14268).

Closes astral-sh#1818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cache Caching of packages and metadata bug Something isn't working priority:2 Important: bounded correctness, release reliability, committed work, or proven performance gains. risk:2 Low, contained risk of minor incorrect behavior with straightforward recovery. size:3 Substantial: interacting behaviors, difficult compatibility questions, or a careful migration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interpreter cache pollution by system-site-packages venvs

3 participants