Repository navigation
Regression in multiprocessing example using venv on Windows in 3.11rc2 #98360
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Oct 17, 2022 There is a bug with using multiprocessing in a virtual environment, which I presume applies to development under PyCharm.
Under a virtual environment, multiprocessing bypasses the venv launcher to instead directly run
sys._base_executable, and it sets the "__PYVENV_LAUNCHER__" environment variable in order to propagate the virtual environment. It bypasses the launcher because the addition of a process in between the parent and the worker breaks the basic design of muliprocessing, which relies on manual duplication of handles from parent to child and vice versa.However, multiprocessing leaves the path of the venv launcher in the command line, which hasn't been a problem until 3.11. With the new implementation of path initialization in 3.11, the value of
sys._base_executableis now based on the command line if the parsedargv[0]path contains one or more backslashes 1. Of course this breaks spawning the next generation of worker processes.A high-level fix in
Popen.__init__(), when running in a virtual environment, would be to modify the result fromspawn.get_command_line()to replace the first item withsys._base_executable. (The venv launcher itself does this, in terms of modifying the command line of the launched process.) Current source:cpython/Lib/multiprocessing/popen_spawn_win32.py
Lines 55 to 68 in 5fe0431
cmd = spawn.get_command_line(parent_pid=os.getpid(), pipe_handle=rhandle) cmd = ' '.join('"%s"' % x for x in cmd) python_exe = spawn.get_executable() # bpo-35797: When running in a venv, we bypass the redirect # executor and launch our base Python. if WINENV and _path_eq(python_exe, sys.executable): python_exe = sys._base_executable env = os.environ.copy() env["__PYVENV_LAUNCHER__"] = sys.executable else: env = None Footnotes
-
In previous versions,
sys._base_executableis always based on the process image path, fromGetModuleFileNameW(NULL, ...). The new behavior allows the command line to override this if the command is a qualified path that contains at least one backslash (not forward slash). Refer to the source in Modules/getpath.py. ↩
-
- added3.11only security fixesonly security fixes3.12only security fixesonly security fixes
on Oct 17, 2022 Many Thanks,
I can confirm that using the System Interpreter in PyCharm (without Virtual Environment) py 3.11.0_rc2 works correctly.
(but not in a virtual environment)- changed the title
[-]in py 3.11.0_rc2 sample multiprocessing code not work, but same code in 3.10(.7) work[/-][+]Regression in multiprocessing example on Windows in 3.11rc2[/+]on Oct 19, 2022 - changed the title
[-]Regression in multiprocessing example on Windows in 3.11rc2[/-][+]Regression in multiprocessing example using venv on Windows in 3.11rc2[/+]on Oct 19, 2022 @zooba Can you have a look at this release blocker?
A high-level fix in
Popen.__init__(), when running in a virtual environment, would be to modify the result fromspawn.get_command_line()to replace the first item withsys._base_executable. (The venv launcher itself does this, in terms of modifying the command line of the launched process.)This sounds like the right fix here. It should be easy to test (in case someone else gets to it before I can):
if WINENV and _path_eq(python_exe, sys.executable): python_exe = sys._base_executable env = os.environ.copy() env["__PYVENV_LAUNCHER__"] = sys.executable cmd[0] = python_exe else: env = None cmd = ' '.join('"%s"' % x for x in cmd) # moved down from earlier- added a commit that references this issue
on Oct 19, 2022 PR posted.
@pablogsal, for your consideration for next week's release. I don't know how common it is to nest multiprocessing pools like this, but the fix is general goodness and seems unlikely to introduce other issues.
Reacted by Pablo Galindo Salgado and Jack- added a commit that references this issue
on Oct 20, 2022 - added a commit that references this issue
on Oct 20, 2022 - added a commit that references this issue
on Oct 22, 2022 This looks fixed. Closing.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
I'm programming in PyCharm in Virtual Environment on Win11 on a 12900k
I use 2 modules:
• mp_tst_all.py
• mp_tst_a.py
Details
mp_tst_all.py
mp_tst_a.py
Bug
first: with PyCharm
mp_tst_a.py works in py 3.11.0_rc2 and in py 3.10(.7)
but
when mp_tst_a.py is called from mp_tst_all.py there is an error in py 3.11.0_rc2,
however, in py 3.10(.7) it works
the error message varies depending on the 'rest' variable (mp_tst_a.py -> def run_main)
secondly: in Command Prompt Window (cmd) it runs:
is it a bug in 3.11.0_rc2 or do i need to change something in the code ?
Thank you