Skip to content

gh-149819: fix .pth and .start file processing in subprocess when inheriting PYTHONPATH#150177

Open
warsaw wants to merge 6 commits into
python:mainfrom
warsaw:gh-149819-fix-pth-subprocess
Open

gh-149819: fix .pth and .start file processing in subprocess when inheriting PYTHONPATH#150177
warsaw wants to merge 6 commits into
python:mainfrom
warsaw:gh-149819-fix-pth-subprocess

Conversation

@warsaw
Copy link
Copy Markdown
Member

@warsaw warsaw commented May 21, 2026

Followup to #149888 and #149819 - Fix by @IntentBug in the original PR, with test cases and review by @warsaw

BugBounty Mind and others added 2 commits May 20, 2026 11:57
After PR pythongh-149583 (Fix double evaluation of .pth and .site files in
venvs), .pth files are no longer loaded in subprocesses started with
subprocess.run([sys.executable, ...]).  The root cause: main() seeds
known_paths from removeduppaths() with all sys.path entries inherited
from the parent process.  addsitedir() then skips .pth processing for
every directory already in known_paths.

Fix:
- main(): call removeduppaths() for dedup but start known_paths as a
  fresh empty set, so that addsitedir() processes .pth files in every
  site-packages directory regardless of inherited sys.path.
- addsitedir(): move known_paths.add() before the sys.path.append and
  guard the append with 'sitedir not in sys.path' to avoid creating
  duplicate entries when called with a fresh known_paths.

This preserves the pythongh-75723 dedup guarantee while allowing subprocesses
to load .pth files.
* Extend _make_start() and _make_pth() to take an optional `basedir` which is used instead of
 `site.tmpdir` if given.
* Add test_pth_processed_when_sitedir_already_on_path() to test the core GH#149819 bug: .pth files
  in subprocesses aren't handled if PYTHONPATH pointing to the .pth directory is inherited.
* Similarly add test_start_processed_when_sitedir_already_on_path() to verify that .start files in
  the same circumstances are also now processed.
@warsaw warsaw self-assigned this May 21, 2026
@warsaw warsaw requested a review from FFY00 as a code owner May 21, 2026 05:44
@warsaw warsaw added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 21, 2026
@warsaw
Copy link
Copy Markdown
Member Author

warsaw commented May 21, 2026

Also, reproducer by @tcely

@warsaw
Copy link
Copy Markdown
Member Author

warsaw commented May 21, 2026

Also, original bug report by @scoder

@tcely
Copy link
Copy Markdown

tcely commented May 21, 2026

This looks good to me. Thanks!

Comment thread Lib/site.py Outdated
@scoder
Copy link
Copy Markdown
Contributor

scoder commented May 21, 2026

Thanks for working on the site.py issues, BTW. That tends to be hairy business, regularly risking to break the setup of random people in some spot of the globe. I do appreciate it.

Co-authored-by: scoder <stefan_ml@behnel.de>
@warsaw
Copy link
Copy Markdown
Member Author

warsaw commented May 21, 2026

Thanks for working on the site.py issues, BTW. That tends to be hairy business, regularly risking to break the setup of random people in some spot of the globe. I do appreciate it.

And we had few core team members committed to maintaining site.py, but it seems like a natural place for me to get back involved in. Thank you both for engaging on this issue. I'm close to committing the branch.

@warsaw warsaw enabled auto-merge (squash) May 21, 2026 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 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