forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-117378: Fix multiprocessing forkserver preload sys.path inhe…
…ritance. (pythonGH-126538) pythongh-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 inherit `sys.path` the same way the spawn method does. Observable behavior change: A `''` value in `sys.path` will now be replaced in the forkserver's `sys.path` with an absolute pathname `os.path.abspath(os.getcwd())` saved at the time that `multiprocessing` 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. Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Misc/NEWS.d/next/Library/2024-11-07-01-40-11.gh-issue-117378.o9O5uM.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Fixed the :mod:`multiprocessing` ``"forkserver"`` start method forkserver | ||
process to correctly inherit the parent's :data:`sys.path` during the importing | ||
of :func:`multiprocessing.set_forkserver_preload` modules in the same manner as | ||
:data:`sys.path` is configured in workers before executing work items. | ||
|
||
This bug caused some forkserver module preloading to silently fail to preload. | ||
This manifested as a performance degration in child processes when the | ||
``sys.path`` was required due to additional repeated work in every worker. | ||
|
||
It could also have a side effect of ``""`` remaining in :data:`sys.path` during | ||
forkserver preload imports instead of the absolute path from :func:`os.getcwd` | ||
at multiprocessing import time used in the worker ``sys.path``. | ||
|
||
Potentially leading to incorrect imports from the wrong location during | ||
preload. We are unaware of that actually happening. The issue was discovered | ||
by someone observing unexpected preload performance gains. | ||
|