Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Lib/multiprocessing/forkserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ def ensure_running(self):
main_kws['sys_path'] = data['sys_path']
if 'init_main_from_path' in data:
main_kws['main_path'] = data['init_main_from_path']
if 'sys_argv' in data:
main_kws['sys_argv'] = data['sys_argv']
if self._preload_on_error != 'ignore':
main_kws['on_error'] = self._preload_on_error

Expand All @@ -197,6 +195,8 @@ def ensure_running(self):
exe = spawn.get_executable()
args = [exe] + util._args_from_interpreter_flags()
args += ['-c', cmd]
if self._preload_modules:
args += data["sys_argv"]
pid = util.spawnv_passfds(exe, args, fds_to_pass)
except:
os.close(alive_w)
Expand Down Expand Up @@ -282,7 +282,7 @@ def _handle_preload(preload, main_path=None, sys_path=None, sys_argv=None,


def main(listener_fd, alive_r, preload, main_path=None, sys_path=None,
*, sys_argv=None, authkey_r=None, on_error='ignore'):
*, authkey_r=None, on_error='ignore'):
"""Run forkserver."""
if authkey_r is not None:
try:
Expand All @@ -293,6 +293,11 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None,
else:
authkey = b''

if preload:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind-of inclined to think that this logic should be in _handle_preload, but I'm not sure how to combine that with the tests that were introduced in gh-141859.

sys_argv = sys.argv[1:]
else:
sys_argv = None

_handle_preload(preload, main_path, sys_path, sys_argv, on_error)

util._close_stdin()
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
except ImportError:
msvcrt = None

try:
import resource
except ImportError:
resource = None


if support.HAVE_ASAN_FORK_BUG:
# gh-89363: Skip multiprocessing tests if Python is built with ASAN to
Expand Down Expand Up @@ -7106,6 +7111,28 @@ def test_preload_main_sys_argv(self):
'',
])

def test_preload_main_sys_argv_limits(self):
# gh-144503: Check that sys.argv is set before __main__ is pre-loaded
if multiprocessing.get_start_method() != "forkserver":
self.skipTest("forkserver specific test")

max_str_arglen = 32 * resource.getpagesize()
argv = ["a" * (max_str_arglen - 1), "b"]
name = os.path.join(os.path.dirname(__file__), 'mp_preload_sysargv.py')
_, out, err = test.support.script_helper.assert_python_ok(
name, *argv)
self.assertEqual(err, b'')

out = out.decode().split("\n")
expected_argv = str(argv)
self.assertEqual(out, [
f"module:{expected_argv}",
f"fun:{expected_argv}",
f"module:{expected_argv}",
f"fun:{expected_argv}",
'',
])

#
# Mixins
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :mod:`multiprocessing` ``forkserver`` bug which prevented starting of
the forkserver if the total length of command line arguments in ``sys.argv``
exceeded the maximum length of a single command line argument.
Loading