Skip to content

Commit 9a6137a

Browse files
gpsheadduaneg
andauthored
[3.13] pythongh-126631: pythongh-137996: fix pre-loading of __main__ (pythonGH-135295) (python#138609)
pythongh-126631: pythongh-137996: fix pre-loading of `__main__` The `main_path` parameter was renamed `init_main_from_name`, update the forkserver code accordingly. This was leading to slower startup times when people were trying to preload the main module. --------- (cherry picked from commit 0912b3a) Co-authored-by: Duane Griffin <[email protected]>
1 parent bb17140 commit 9a6137a

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

Lib/multiprocessing/forkserver.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ def ensure_running(self):
127127
cmd = ('from multiprocessing.forkserver import main; ' +
128128
'main(%d, %d, %r, **%r)')
129129

130+
main_kws = {}
130131
if self._preload_modules:
131-
desired_keys = {'main_path', 'sys_path'}
132132
data = spawn.get_preparation_data('ignore')
133-
data = {x: y for x, y in data.items() if x in desired_keys}
134-
else:
135-
data = {}
133+
if 'sys_path' in data:
134+
main_kws['sys_path'] = data['sys_path']
135+
if 'init_main_from_path' in data:
136+
main_kws['main_path'] = data['init_main_from_path']
136137

137138
with socket.socket(socket.AF_UNIX) as listener:
138139
address = connection.arbitrary_address('AF_UNIX')
@@ -147,7 +148,7 @@ def ensure_running(self):
147148
try:
148149
fds_to_pass = [listener.fileno(), alive_r]
149150
cmd %= (listener.fileno(), alive_r, self._preload_modules,
150-
data)
151+
main_kws)
151152
exe = spawn.get_executable()
152153
args = [exe] + util._args_from_interpreter_flags()
153154
args += ['-c', cmd]

Lib/test/_test_multiprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,6 +6545,18 @@ def child():
65456545
self.assertEqual(q.get_nowait(), "done")
65466546
close_queue(q)
65476547

6548+
def test_preload_main(self):
6549+
# gh-126631: Check that __main__ can be pre-loaded
6550+
if multiprocessing.get_start_method() != "forkserver":
6551+
self.skipTest("forkserver specific test")
6552+
6553+
name = os.path.join(os.path.dirname(__file__), 'mp_preload_main.py')
6554+
_, out, err = test.support.script_helper.assert_python_ok(name)
6555+
self.assertEqual(err, b'')
6556+
6557+
# The trailing empty string comes from split() on output ending with \n
6558+
out = out.decode().split("\n")
6559+
self.assertEqual(out, ['__main__', '__mp_main__', 'f', 'f', ''])
65486560

65496561
#
65506562
# Mixins

Lib/test/mp_preload_main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import multiprocessing
2+
3+
print(f"{__name__}")
4+
5+
def f():
6+
print("f")
7+
8+
if __name__ == "__main__":
9+
ctx = multiprocessing.get_context("forkserver")
10+
ctx.set_forkserver_preload(['__main__'])
11+
for _ in range(2):
12+
p = ctx.Process(target=f)
13+
p.start()
14+
p.join()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`multiprocessing` ``forkserver`` bug which prevented ``__main__``
2+
from being preloaded.

0 commit comments

Comments
 (0)