Skip to content

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

Open
warsaw wants to merge 6 commits intopython:mainfrom
warsaw:gh-149504
Open

gh-149504: Fix re-entrancy bug when .pth/.start file invokes site.addsitedir()#149659
warsaw wants to merge 6 commits intopython: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

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.

2 participants