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
commented
May 11, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This ensures that future whitebox internal test changes do not regress the
public surface semantics.
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.
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
|
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!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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!).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks @warsaw for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
warsaw
added a commit
that referenced
this pull request
May 13, 2026
…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
Uh oh!
There was an error while loading. Please reload this page.
Labels
3.15
bugs and security fixes
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.
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.
|