-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-144503: Pass sys.argv via a route less limited by single command line argument length
#144508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sjoerdjob
wants to merge
6
commits into
python:main
Choose a base branch
from
sjoerdjob:issue-144503-multiprocessing-forkserver-arglen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−42
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c6a3bea
gh-144503: Pass `sys.argv` as separate command line arguments.
sjoerdjob 5b8ff7b
gh-144503: Add news entry
sjoerdjob 0507aa0
gh-144503: Accept non-importable resource
sjoerdjob be2e1f0
gh-144503: Send initialization data for preload over pipe instead of …
sjoerdjob 80e7a0e
gh-144503: Harden init pipe I/O in forkserver
gpshead 13acba8
Merge remote-tracking branch 'upstream/main' into issue-144503-multip…
sjoerdjob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import atexit | ||
| import errno | ||
| import json | ||
| import os | ||
| import selectors | ||
| import signal | ||
|
|
@@ -162,27 +163,20 @@ def ensure_running(self): | |
| self._forkserver_alive_fd = None | ||
| self._forkserver_pid = None | ||
|
|
||
| # gh-144503: sys_argv is passed as real argv elements after the | ||
| # ``-c cmd`` rather than repr'd into main_kws so that a large | ||
| # parent sys.argv cannot push the single ``-c`` command string | ||
| # over the OS per-argument length limit (MAX_ARG_STRLEN on Linux). | ||
| # The child sees them as sys.argv[1:]. | ||
| cmd = ('import sys; ' | ||
| 'from multiprocessing.forkserver import main; ' | ||
| 'main(%d, %d, %r, sys_argv=sys.argv[1:], **%r)') | ||
|
|
||
| main_kws = {} | ||
| sys_argv = None | ||
| cmd = ('from multiprocessing.forkserver import main; ' + | ||
| 'main(listener_fd=%d, alive_r=%d, init_r=%d)') | ||
|
|
||
| if self._preload_modules: | ||
| data = spawn.get_preparation_data('ignore') | ||
| if 'sys_path' in data: | ||
| 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: | ||
| sys_argv = data['sys_argv'] | ||
| if self._preload_on_error != 'ignore': | ||
| main_kws['on_error'] = self._preload_on_error | ||
| preload_kwargs = { | ||
| "preload": self._preload_modules, | ||
| "sys_path": data.get("sys_path"), | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| "main_path": data.get("init_main_from_path"), | ||
| "sys_argv": data.get("sys_argv"), | ||
| "on_error": self._preload_on_error, | ||
| } | ||
| else: | ||
| preload_kwargs = None | ||
|
|
||
| with socket.socket(socket.AF_UNIX) as listener: | ||
| address = connection.arbitrary_address('AF_UNIX') | ||
|
|
@@ -195,32 +189,34 @@ def ensure_running(self): | |
| # when they all terminate the read end becomes ready. | ||
| alive_r, alive_w = os.pipe() | ||
| # A short lived pipe to initialize the forkserver authkey. | ||
| authkey_r, authkey_w = os.pipe() | ||
| init_r, init_w = os.pipe() | ||
| try: | ||
| fds_to_pass = [listener.fileno(), alive_r, authkey_r] | ||
| main_kws['authkey_r'] = authkey_r | ||
| cmd %= (listener.fileno(), alive_r, self._preload_modules, | ||
| main_kws) | ||
| fds_to_pass = [listener.fileno(), alive_r, init_r] | ||
| cmd %= (listener.fileno(), alive_r, init_r) | ||
| exe = spawn.get_executable() | ||
| args = [exe] + util._args_from_interpreter_flags() | ||
| args += ['-c', cmd] | ||
| if sys_argv is not None: | ||
| args += sys_argv | ||
| pid = util.spawnv_passfds(exe, args, fds_to_pass) | ||
gpshead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except: | ||
| os.close(alive_w) | ||
| os.close(authkey_w) | ||
| os.close(init_w) | ||
| raise | ||
| finally: | ||
| os.close(alive_r) | ||
| os.close(authkey_r) | ||
| os.close(init_r) | ||
| # Authenticate our control socket to prevent access from | ||
| # processes we have not shared this key with. | ||
| try: | ||
| self._forkserver_authkey = os.urandom(_AUTHKEY_LEN) | ||
| os.write(authkey_w, self._forkserver_authkey) | ||
| preload_data = json.dumps(preload_kwargs).encode() | ||
| # Use a buffered writer so that payloads larger than | ||
| # PIPE_BUF are written fully (os.write may short-write). | ||
| with os.fdopen(init_w, 'wb', closefd=False) as f: | ||
| f.write(self._forkserver_authkey) | ||
| f.write(struct.pack("Q", len(preload_data))) | ||
| f.write(preload_data) | ||
| finally: | ||
| os.close(authkey_w) | ||
| os.close(init_w) | ||
| self._forkserver_address = address | ||
| self._forkserver_alive_fd = alive_w | ||
| self._forkserver_pid = pid | ||
|
|
@@ -290,19 +286,22 @@ def _handle_preload(preload, main_path=None, sys_path=None, sys_argv=None, | |
| util._flush_std_streams() | ||
|
|
||
|
|
||
| def main(listener_fd, alive_r, preload, main_path=None, sys_path=None, | ||
| *, sys_argv=None, authkey_r=None, on_error='ignore'): | ||
| def main(listener_fd, alive_r, init_r): | ||
| """Run forkserver.""" | ||
| if authkey_r is not None: | ||
| try: | ||
| authkey = os.read(authkey_r, _AUTHKEY_LEN) | ||
| assert len(authkey) == _AUTHKEY_LEN, f'{len(authkey)} < {_AUTHKEY_LEN}' | ||
| finally: | ||
| os.close(authkey_r) | ||
| else: | ||
| authkey = b'' | ||
|
|
||
| _handle_preload(preload, main_path, sys_path, sys_argv, on_error) | ||
| try: | ||
| # Buffered reader handles short reads on the length prefix and body. | ||
| with os.fdopen(init_r, 'rb', closefd=False) as f: | ||
| authkey = f.read(_AUTHKEY_LEN) | ||
| assert len(authkey) == _AUTHKEY_LEN, ( | ||
| f'{len(authkey)} < {_AUTHKEY_LEN}') | ||
| preload_data_len, = struct.unpack("Q", | ||
| f.read(struct.calcsize("Q"))) | ||
| preload_kwargs = json.loads(f.read(preload_data_len)) | ||
| finally: | ||
| os.close(init_r) | ||
|
|
||
| if preload_kwargs: | ||
| _handle_preload(**preload_kwargs) | ||
|
|
||
| util._close_stdin() | ||
|
|
||
|
|
||
This file contains hidden or 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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-02-05-16-29-45.gh-issue-144503.f9sl_I.rst
This file contains hidden or 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,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. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
if ... in datahave been removed, because a cursory reading ofget_preparation_datashows that thesys_path,sys_argvare always present in the returned dictionary.The
init_main_from_pathis not always present, but the default argument in_handle_preloadisNone, so I think using that same value here is fine.Similarly to
on_error: this was checking if it was equal to the default value of_handle_preload, and if so, would not pass it. If the default value of_handle_preloadwould be changed, and this statement here would not be, that would cause an inconsistency. I think it's better to just always pass the value here to prevent future drift.