]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.12] gh-117378: Fix multiprocessing forkserver preload sys.path inheritance. (GH...
authorGregory P. Smith <greg@krypto.org>
Sun, 10 Nov 2024 00:13:26 +0000 (16:13 -0800)
committerGitHub <noreply@github.com>
Sun, 10 Nov 2024 00:13:26 +0000 (00:13 +0000)
commit7bb92ed505920009ca097948553adc846596b246
tree4bf11e3b841b9c7cd0f2272706990bbb12f6a752
parent5fb443d6254d53f43acb0b39a7615d56f4fc7355
[3.12] gh-117378: Fix multiprocessing forkserver preload sys.path inheritance. (GH-126538) (GH-126633)

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

(cherry picked from commit 9d08423b6e0fa89ce9cfea08e580ed72e5db8c70)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/multiprocessing/forkserver.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2024-11-07-01-40-11.gh-issue-117378.o9O5uM.rst [new file with mode: 0644]