Skip to content

gh-126631: fix pre-loading of __main__ #135295

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
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 5 additions & 4 deletions Lib/multiprocessing/forkserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,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')
main_kws = {x: y for x, y in data.items() if x in desired_keys}
else:
main_kws = {}
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 Down
13 changes: 13 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6866,6 +6866,19 @@ 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')
rc, out, err = test.support.script_helper.assert_python_ok(name)
self.assertFalse(err, msg=err.decode())
self.assertEqual(rc, 0)

# TODO: Where is the extra empty line coming from?
out = out.decode().split("\n")
self.assertEqual(out, ['__main__', '__mp_main__', 'f', 'f', ''])

#
# Mixins
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/mp_preload_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import multiprocessing
import sys

print(f"{__name__}")

# TODO: This is necessary as the fork server doesn't flush output after
# preloading modules, and hence buffered output is inherited by child
# processes. See gh-135335 (this should be removed once that is fixed).
sys.stdout.flush()

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()
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.
Loading