FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.13] gh-126631: gh-137996: fix pre-loading of `__main__` (GH-135295) by gpshead · Pull Request #138609 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 6 additions & 5 deletions Lib/multiprocessing/forkserver.py
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
Expand Up @@ -127,12 +127,13 @@ def ensure_running(self):
cmd = ('from multiprocessing.forkserver import main; ' +
'main(%d, %d, %r, **%r)')

main_kws = {}
if self._preload_modules:
desired_keys = {'main_path', 'sys_path'}
data = spawn.get_preparation_data('ignore')
data = {x: y for x, y in data.items() if x in desired_keys}
else:
data = {}
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']

with socket.socket(socket.AF_UNIX) as listener:
address = connection.arbitrary_address('AF_UNIX')
Expand All @@ -147,7 +148,7 @@ def ensure_running(self):
try:
fds_to_pass = [listener.fileno(), alive_r]
cmd %= (listener.fileno(), alive_r, self._preload_modules,
data)
main_kws)
exe = spawn.get_executable()
args = [exe] + util._args_from_interpreter_flags()
args += ['-c', cmd]
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/_test_multiprocessing.py
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
Expand Up @@ -6545,6 +6545,18 @@ def child():
self.assertEqual(q.get_nowait(), "done")
close_queue(q)

def test_preload_main(self):
# gh-126631: Check that __main__ can be pre-loaded
if multiprocessing.get_start_method() != "forkserver":
self.skipTest("forkserver specific test")

name = os.path.join(os.path.dirname(__file__), 'mp_preload_main.py')
_, out, err = test.support.script_helper.assert_python_ok(name)
self.assertEqual(err, b'')

# The trailing empty string comes from split() on output ending with \n
out = out.decode().split("\n")
self.assertEqual(out, ['__main__', '__mp_main__', 'f', 'f', ''])

#
# Mixins
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/mp_preload_main.py
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,14 @@
import multiprocessing

print(f"{__name__}")

def f():
print("f")

if __name__ == "__main__":
ctx = multiprocessing.get_context("forkserver")
ctx.set_forkserver_preload(['__main__'])
for _ in range(2):
p = ctx.Process(target=f)
p.start()
p.join()
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,2 @@
Fix :mod:`multiprocessing` ``forkserver`` bug which prevented ``__main__``
from being preloaded.

Back | FazBrowse Home | New Git URL