Skip to content

Commit 055a4c2

Browse files
XD TrolTrol
XD Trol
authored and
Trol
committed
bpo-46391: Library multiprocess leaks named resources.
1 parent f779fac commit 055a4c2

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

Lib/multiprocessing/context.py

+14
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class Process(process.BaseProcess):
223223
def _Popen(process_obj):
224224
return _default_context.get_context().Process._Popen(process_obj)
225225

226+
@staticmethod
227+
def _bootstrap_util():
228+
return _default_context.get_context().Process._bootstrap_util()
229+
226230
class DefaultContext(BaseContext):
227231
Process = Process
228232

@@ -283,6 +287,11 @@ def _Popen(process_obj):
283287
from .popen_spawn_posix import Popen
284288
return Popen(process_obj)
285289

290+
@staticmethod
291+
def _bootstrap_util():
292+
# Process is spawned, no need to bootstrap util.
293+
pass
294+
286295
class ForkServerProcess(process.BaseProcess):
287296
_start_method = 'forkserver'
288297
@staticmethod
@@ -326,6 +335,11 @@ def _Popen(process_obj):
326335
from .popen_spawn_win32 import Popen
327336
return Popen(process_obj)
328337

338+
@staticmethod
339+
def _bootstrap_util():
340+
# Process is spawned, no need to bootstrap util.
341+
pass
342+
329343
class SpawnContext(BaseContext):
330344
_name = 'spawn'
331345
Process = SpawnProcess

Lib/multiprocessing/process.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ def _bootstrap(self, parent_sentinel=None):
304304
if threading._HAVE_THREAD_NATIVE_ID:
305305
threading.main_thread()._set_native_id()
306306
try:
307-
util._finalizer_registry.clear()
308-
util._run_after_forkers()
307+
self._bootstrap_util()
309308
finally:
310309
# delay finalization of the old process object until after
311310
# _run_after_forkers() is executed
@@ -336,6 +335,13 @@ def _bootstrap(self, parent_sentinel=None):
336335

337336
return exitcode
338337

338+
@staticmethod
339+
def _bootstrap_util():
340+
from . import util
341+
util._finalizer_registry.clear()
342+
util._run_after_forkers()
343+
344+
339345
#
340346
# We subclass bytes to avoid accidental transmission of auth keys over network
341347
#

Lib/test/_test_multiprocessing.py

+35
Original file line numberDiff line numberDiff line change
@@ -5706,6 +5706,41 @@ def test_namespace(self):
57065706
self.run_worker(self._test_namespace, o)
57075707

57085708

5709+
class TestNamedResource(unittest.TestCase):
5710+
5711+
def test_global_named_resource_spawn(self):
5712+
#
5713+
# Check that global named resources in main module
5714+
# will not leak by a subprocess, in spawn context.
5715+
#
5716+
import tempfile as tf
5717+
py = tf.NamedTemporaryFile('w', delete=False)
5718+
try:
5719+
py.write('''if 1:
5720+
import multiprocessing as mp
5721+
5722+
ctx = mp.get_context('spawn')
5723+
5724+
global_resource = ctx.Semaphore()
5725+
5726+
def submain(): pass
5727+
5728+
if __name__ == '__main__':
5729+
p = ctx.Process(target=submain)
5730+
p.start()
5731+
p.join()
5732+
''')
5733+
py.close()
5734+
5735+
p = subprocess.run([sys.executable, py.name],
5736+
stderr=subprocess.PIPE,
5737+
text=True)
5738+
5739+
self.assertNotRegex(p.stderr, 'resource_tracker: There appear to be .* leaked')
5740+
finally:
5741+
os.unlink(py.name)
5742+
5743+
57095744
class MiscTestCase(unittest.TestCase):
57105745
def test__all__(self):
57115746
# Just make sure names in not_exported are excluded
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix that Library multiprocess leaks named resources when global named
2+
resources are used in the main module in spawn context.

0 commit comments

Comments
 (0)