Skip to content

🐛 fix(seed): make seed images read-only - #3757

Merged
gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:fix/3620-read-only-seed-images
Nov 21, 2026
Merged

gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:fix/3620-read-only-seed-images

Conversation

@gaborbernat

Copy link
Copy Markdown
Contributor

With --symlink-app-data, every environment links to one cached copy of each seed package (pip, and setuptools where virtualenv seeds it), which virtualenv calls a seed image and keeps in its app-data cache directory. Running pip install --ignore-installed pip in one of them rewrote that shared image, and pip could stop working in another environment that had installed nothing (#3620). 🔒 That command now fails with a permission error, and the shared image and the other environments stay as they were.

Upgrades, --force-reinstall and uninstalls were never affected: pip removes the environment's links first and then installs a private copy. --ignore-installed skips that uninstall step, so the links were still in place when pip wrote its files.

Design

The seed image cache is now read-only once an image is built: its files on every platform, and its directories on POSIX, where replacing a file needs write permission on the directory that holds it. Two parts stay writable. Bytecode cache directories do, because interpreters compile into them when they load a module; pip fails on its first source file, before it compiles anything. The top directory of an image does, so a finished build can still be moved into place; environments link to the directories below it, never to the top directory itself.

The code that manages the cache restores write permission before it deletes: on --reset-app-data, for temporary app-data and when it rebuilds an image. Everything else virtualenv deletes, such as an environment being cleared, still refuses a read-only directory. Copy seeding writes new files with fixed permissions instead of copying them from the image, so copied seed packages stay writable.

The trade-off is that --ignore-installed fails where a private copy would be nicer. Giving each environment its own files is what #3755 does, and it reports creation of a linked environment going from 3.6 ms to 15 ms. This change adds one pass over an image when it is built, and no work when an environment is created.

Behavior changes

  • In a --symlink-app-data environment, pip install --ignore-installed of a seed package fails with Permission denied. Use --force-reinstall to get a private copy.
  • The permissions do not bind root, so root can still change the shared image this way.
  • On Windows only files are read-only, because a read-only directory there still accepts new files and cannot be deleted. That is enough for the install to fail there too.
  • Removing the cache by hand with rm -rf now fails on POSIX. Use --reset-app-data, or run chmod -R u+w on it first.
  • Images built by earlier releases keep writable directories until they are rebuilt. --reset-app-data rebuilds them; environments linked to the old images must then be recreated, which a reset already required.

Supersedes #3755.

Fixes #3620

With --symlink-app-data an environment links into the cached seed image.
pip install --ignore-installed skips the uninstall that would remove those
links, so pip wrote through them and changed the package for every
environment sharing the image.

Close the image's directories as well as its files, so that write fails
instead. Bytecode caches stay open because interpreters compile into them
on load, and the image root stays open so a build can still move it into
place. Deleting the cache goes through a separate helper that reopens the
directories first, while safe_delete keeps refusing a read-only directory.
Windows directories stay as they are: closing one does not stop new files
there and blocks deleting it. The permissions do not bind root.

Fixes pypa#3620
@gaborbernat
gaborbernat marked this pull request as ready for review November 21, 2026 18:15
@gaborbernat
gaborbernat enabled auto-merge (squash) November 21, 2026 18:15
@gaborbernat
gaborbernat marked this pull request as draft November 21, 2026 18:25
auto-merge was automatically disabled November 21, 2026 18:25

Pull request was converted to draft

@gaborbernat
gaborbernat marked this pull request as ready for review November 21, 2026 18:57
@gaborbernat
gaborbernat merged commit 22200b3 into pypa:main Nov 21, 2026
152 of 157 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pip install can change other environments' shared seed image

1 participant