]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-126631: gh-137996: fix pre-loading of `__main__` (GH-135295) (#138607)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 8 Oct 2025 00:37:04 +0000 (02:37 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Oct 2025 00:37:04 +0000 (00:37 +0000)
gh-126631: gh-137996: fix pre-loading of `__main__` (GH-135295)

gh-126631: gh-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 0912b3a6dbd226bea79eb8d70d5a06230804d4cb)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Lib/multiprocessing/forkserver.py
Lib/test/_test_multiprocessing.py
Lib/test/mp_preload_main.py [new file with mode: 0644]
Misc/NEWS.d/next/Library/2025-06-10-21-00-48.gh-issue-126631.eITVJd.rst [new file with mode: 0644]

index c91891ff162c2d4fdd73a1122b9179c2b6c652d1..cc8947c5e04fb11b0ea3ef39db3a599851e940a3 100644 (file)
@@ -145,12 +145,13 @@ class ForkServer(object):
             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')
index 9a72362c022343a1876de30fbb34806f9601ea40..a7fa862d4cf1006f998f041ab23f9f20f3caa21e 100644 (file)
@@ -6883,6 +6883,18 @@ class MiscTestCase(unittest.TestCase):
         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
diff --git a/Lib/test/mp_preload_main.py b/Lib/test/mp_preload_main.py
new file mode 100644 (file)
index 0000000..acb3428
--- /dev/null
@@ -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()
diff --git a/Misc/NEWS.d/next/Library/2025-06-10-21-00-48.gh-issue-126631.eITVJd.rst b/Misc/NEWS.d/next/Library/2025-06-10-21-00-48.gh-issue-126631.eITVJd.rst
new file mode 100644 (file)
index 0000000..195253b
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :mod:`multiprocessing` ``forkserver`` bug which prevented ``__main__``
+from being preloaded.