8000 bpo-46391: Library multiprocess leaks named resources. · python/cpython@f202fff · GitHub
[go: up one dir, main page]

Skip to content

Commit f202fff

Browse files
XD TrolTrol
XD Trol
authored and
Trol
committed
bpo-46391: Library multiprocess leaks named resources.
1 parent 70690c7 commit f202fff

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

Lib/multiprocessing/context.py

Lines changed: 14 additions & 0 deletions
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 _after_fork():
228+
return _default_context.get_context().Process._after_fork()
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 _after_fork():
292+
# process is spawned, nothing to do
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 _after_fork():
340+
# process is spawned, nothing to do
341+
pass
342+
329343
class SpawnContext(BaseContext):
330344
_name = 'spawn'
331345
Process = SpawnProcess

Lib/multiprocessing/process.py

Lines changed: 8 additions & 2 deletions
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._after_fork()
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 _after_fork():
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5760,6 +5760,33 @@ def test_namespace(self):
57605760
self.run_worker(self._test_namespace, o)
57615761

57625762

5763+
class TestNamedResource(unittest.TestCase):
5764+
def test_global_named_resource_spawn(self):
5765+
#
5766+
# Check that global named resources in main module
5767+
# will not leak by a subprocess, in spawn context.
5768+
#
5769+
with os_helper.temp_dir() as tmp_dir:
5770+
source = os.path.join(tmp_dir, 'source.py')
5771+
with open(source, 'w') as f:
5772+
f.write('''if 1:
5773+
import multiprocessing as mp
5774+
5775+
ctx = mp.get_context('spawn')
5776+
5777+
global_resource = ctx.Semaphore()
5778+
5779+
def submain(): pass
5780+
5781+
if __name__ == '__main__':
5782+
p = ctx.Process(target=submain)
5783+
p.start()
5784+
p.join()
5785+
''')
5786+
rc, out, err = test.support.script_helper.assert_python_ok(source)
5787+
self.assertNotRegex(err, b'resource_tracker: There appear to be .* leaked')
5788+
5789+
57635790
class MiscTestCase(unittest.TestCase):
57645791
def test__all__(self):
57655792
# Just make sure names in not_exported are excluded
Lines changed: 2 additions & 0 deletions
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)
0