GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-149504: Fix re-entrancy bug when .pth/.start file invokes site.addsitedir() by warsaw · Pull Request #149659 · python/cpython

Conversation

@warsaw

@warsaw warsaw commented May 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Fix site.addsitedir() to allow re-entrant calls from within startup files. Previously, a .pth file containing an import line that called site.addsitedir() (or a .start entry point doing the same) could crash with RuntimeError: dictionary changed size during iteration during site initialization, breaking tools such as uv run --with.

mkdir -p /tmp/site-repro/root /tmp/site-repro/overlay
printf 'import site; site.addsitedir("/tmp/site-repro/overlay")\n' > /tmp/site-repro/root/reenter.pth
./python.exe -S -c 'import site; site.addsitedir("/tmp/site-repro/root"); print("done")'

prints "done" for me.

uv run --python /Users/jelle/py/cpython/python.exe --with iniconfig python -c 'import iniconfig'

does not crash.

The implementation is more comprehensive than #149516 and is more inline with a suggestion @ncoghlan made on the original PR. Thanks @zsol for providing your PR. @JelleZijlstra please take a look.

  • Issue: [3.15] Re-entrant calls to site.addsitedir() can fail (affecting uv run) #149504

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
warsaw added 5 commits May 10, 2026 21:00
@warsaw
Add re-entrant tests for pythongh-149504
7108fb7
@warsaw
Add end-to-end integration test coverage
aae5378
This ensures that future whitebox internal test changes do not regress the
public surface semantics.
@warsaw
Implement a state class to process .pth and .start files
dc96d5d
By using this state class and managing implicit and explicit batching, we make it structurally
impossible to get bitten by re-entrant site startup processing.

Fixes python#149504

Update all the implementation and integration tests.
@warsaw
Add a blurb
c6cfcb0
@warsaw
One small round of cleanup.
04b67f7
@warsaw
warsaw requested a review from FFY00 as a code owner May 11, 2026 07:18
@warsaw warsaw self-assigned this May 11, 2026
@bedevere-app bedevere-app Bot added the awaiting core review label May 11, 2026
@bedevere-app bedevere-app Bot mentioned this pull request May 11, 2026
[3.15] Re-entrant calls to site.addsitedir() can fail (affecting uv run) #149504
Closed
@warsaw
warsaw requested review from JelleZijlstra, StanFromIreland and ncoghlan May 11, 2026 07:19
@warsaw
Merge branch 'main' into pythongh-149504
c38ec2b
zsol
zsol approved these changes May 11, 2026
View reviewed changes

@zsol zsol left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

FWIW, I reviewed the changes and checked that uv run --with ... works fine with these. Encapsulating all the state into a class is nice :) I'll close #149516 in favor of this

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

warsaw reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@warsaw
Merge branch 'main' into pythongh-149504
ea34446
@JelleZijlstra

JelleZijlstra commented May 11, 2026

Copy link
Copy Markdown
Member

I'll try to take a look if you want more reviews but I don't really know site.py. Thanks for working on this!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@warsaw

warsaw commented May 11, 2026

Copy link
Copy Markdown
Member Author

I'll try to take a look if you want more reviews but I don't really know site.py. Thanks for working on this!

That's okay. I just tagged you to check that the uv case works with this branch, but @zsol confirmed it fixed the problem (thanks!).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@warsaw warsaw mentioned this pull request May 12, 2026
gh-75723: Fix double evaluation of .pth and .site files in venvs #149583
Merged
warsaw added 2 commits May 12, 2026 13:02
@warsaw
Merge branch 'main' into pythongh-149504
558ece4
@warsaw
Merge branch 'main' into pythongh-149504
9c488d7
@warsaw
warsaw enabled auto-merge (squash) May 13, 2026 15:54
@warsaw
warsaw merged commit b162307 into python:main May 13, 2026
52 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label May 13, 2026
@warsaw warsaw added 3.15 bugs and security fixes needs backport to 3.15 bugs and security fixes labels May 13, 2026
@miss-islington-app

miss-islington-app Bot commented May 13, 2026

Copy link
Copy Markdown

Thanks @warsaw for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented May 13, 2026

Copy link
Copy Markdown

Sorry, @warsaw, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker b162307d7f216e87976d76c9b8f4a932961cb2d4 3.15
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@warsaw
warsaw deleted the gh-149504 branch May 13, 2026 18:29
@warsaw warsaw mentioned this pull request May 13, 2026
[3.15] gh-149504: Fix re-entrancy bug when .pth/.start file invokes site.add sitedir() (#149659) #149799
Merged
@bedevere-app

bedevere-app Bot commented May 13, 2026

Copy link
Copy Markdown

GH-149799 is a backport of this pull request to the 3.15 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label May 13, 2026
@zsol zsol mentioned this pull request May 13, 2026
Remove site startup patch astral-sh/python-build-standalone#1130
Closed
warsaw added a commit that referenced this pull request May 13, 2026
@warsaw
[3.15] gh-149504: Fix re-entrancy bug when .pth/.start file invokes s…
45fc9ac
…ite.add sitedir() (#149659) (#149799)

* gh-149504: Fix re-entrancy bug when .pth/.start file invokes site.addsitedir() (#149659)

* Add re-entrant tests for gh-149504
* Add end-to-end integration test coverage

This ensures that future whitebox internal test changes do not regress the
public surface semantics.

* Implement a state class to process .pth and .start files

By using this state class and managing implicit and explicit batching, we make it structurally
impossible to get bitten by re-entrant site startup processing.

Fixes #149504

(cherry picked from commit b162307)

* Add myself back to CODEOWNERS
@scoder scoder mentioned this pull request May 19, 2026
New site.py implementation fails to load .pth files in subprocesses #149819
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan
@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra
@StanFromIreland StanFromIreland Awaiting requested review from StanFromIreland
1 more reviewer
@zsol zsol zsol approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@warsaw warsaw

Labels

3.15 bugs and security fixes

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@warsaw @JelleZijlstra @zsol
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.