Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit055a4c2

Browse files
XD TrolTrol
XD Trol
authored and
Trol
committed
bpo-46391: Library multiprocess leaks named resources.
1 parentf779fac commit055a4c2

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-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_bootstrap_util():
228+
return_default_context.get_context().Process._bootstrap_util()
229+
226230
classDefaultContext(BaseContext):
227231
Process=Process
228232

@@ -283,6 +287,11 @@ def _Popen(process_obj):
283287
from .popen_spawn_posiximportPopen
284288
returnPopen(process_obj)
285289

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

338+
@staticmethod
339+
def_bootstrap_util():
340+
# Process is spawned, no need to bootstrap util.
341+
pass
342+
329343
classSpawnContext(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
ifthreading._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
returnexitcode
338337

338+
@staticmethod
339+
def_bootstrap_util():
340+
from .importutil
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: 35 additions & 0 deletions
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+
classTestNamedResource(unittest.TestCase):
5710+
5711+
deftest_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+
importtempfileastf
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
classMiscTestCase(unittest.TestCase):
57105745
deftest__all__(self):
57115746
# 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)

[8]ページ先頭

©2009-2025 Movatter.jp