Skip to content

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

Merged
warsaw merged 9 commits into
python:mainfrom
warsaw:gh-149504
May 13, 2026
Merged

gh-149504: Fix re-entrancy bug when .pth/.start file invokes site.addsitedir()#149659
warsaw merged 9 commits into
python:mainfrom
warsaw:gh-149504

Conversation

@warsaw
Copy link
Copy Markdown
Member

@warsaw warsaw commented May 11, 2026

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.

warsaw added 5 commits May 10, 2026 21:00
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.
Copy link
Copy Markdown
Member

@zsol zsol left a 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

@JelleZijlstra
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!

@warsaw
Copy link
Copy Markdown
Member Author

warsaw commented May 11, 2026

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

@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
@warsaw warsaw added 3.15 pre-release feature fixes, bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels May 13, 2026
@miss-islington-app
Copy link
Copy Markdown

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

@miss-islington-app
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

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 13, 2026

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants