[3.12] gh-117378: Fix multiprocessing forkserver preload sys.path inheritance. (GH-126538) #126633
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.
gh-117378: Fix multiprocessing forkserver preload sys.path inheritance.
sys.path
was not properly being sent from the parent process when launching the multiprocessing forkserver process to preload imports. This bug has been there since the forkserver start method was introduced in Python 3.4. It was always supposed to inheritsys.path
the same way the spawn method does.Observable behavior change: A
''
value insys.path
will now be replaced in the forkserver'ssys.path
with an absolute pathnameos.path.abspath(os.getcwd())
saved at the time thatmultiprocessing
was imported in the parent process as it already was when using the spawn start method. This will only be observable during forkserver preload imports.The code invoked before calling things in another process already correctly sets
sys.path
. Which is likely why this went unnoticed for so long as a mere performance issue in some configurations.A workaround for the bug on impacted Pythons is to set PYTHONPATH in the environment before multiprocessing's forkserver process was started. Not perfect as that is then inherited by other children, etc, but likely good enough for many people's purposes.
(cherry picked from commit 9d08423)