Skip to content

Commit cd2a201

Browse files
committed
Issue #28779: multiprocessing.set_forkserver_preload() would crash the forkserver process if a preloaded module instantiated some multiprocessing objects such as locks.
1 parent 7a44783 commit cd2a201

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

Lib/multiprocessing/context.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def get_context(self, method=None):
195195
def get_start_method(self, allow_none=False):
196196
return self._name
197197

198-
def set_start_method(self, method=None):
198+
def set_start_method(self, method, force=False):
199199
raise ValueError('cannot set start method of concrete context')
200200

201201
def _check_available(self):

Lib/multiprocessing/spawn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def prepare(data):
218218
process.ORIGINAL_DIR = data['orig_dir']
219219

220220
if 'start_method' in data:
221-
set_start_method(data['start_method'])
221+
set_start_method(data['start_method'], force=True)
222222

223223
if 'init_main_from_name' in data:
224224
_fixup_main_from_name(data['init_main_from_name'])

Lib/test/_test_multiprocessing.py

+13
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,19 @@ def test_get_all(self):
37283728
self.assertTrue(methods == ['fork', 'spawn'] or
37293729
methods == ['fork', 'spawn', 'forkserver'])
37303730

3731+
def test_preload_resources(self):
3732+
if multiprocessing.get_start_method() != 'forkserver':
3733+
self.skipTest("test only relevant for 'forkserver' method")
3734+
name = os.path.join(os.path.dirname(__file__), 'mp_preload.py')
3735+
rc, out, err = test.support.script_helper.assert_python_ok(name)
3736+
out = out.decode()
3737+
err = err.decode()
3738+
if out.rstrip() != 'ok' or err != '':
3739+
print(out)
3740+
print(err)
3741+
self.fail("failed spawning forkserver or grandchild")
3742+
3743+
37313744
#
37323745
# Check that killing process does not leak named semaphores
37333746
#

Lib/test/mp_preload.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import multiprocessing
2+
3+
multiprocessing.Lock()
4+
5+
6+
def f():
7+
print("ok")
8+
9+
10+
if __name__ == "__main__":
11+
ctx = multiprocessing.get_context("forkserver")
12+
modname = "test.mp_preload"
13+
# Make sure it's importable
14+
__import__(modname)
15+
ctx.set_forkserver_preload([modname])
16+
proc = ctx.Process(target=f)
17+
proc.start()
18+
proc.join()

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ Core and Builtins
124124
Library
125125
-------
126126

127+
- Issue #28779: multiprocessing.set_forkserver_preload() would crash the
128+
forkserver process if a preloaded module instantiated some
129+
multiprocessing objects such as locks.
130+
127131
- Issue #28847: dbm.dumb now supports reading read-only files and no longer
128132
writes the index file when it is not changed.
129133

0 commit comments

Comments
 (0)