summaryrefslogtreecommitdiffstats
path: root/Lib/test/_test_multiprocessing.py
diff options
context:
space:
mode:
authorLeo Trol <milestone.jxd@gmail.com>2022-06-09 16:55:12 (GMT)
committerGitHub <noreply@github.com>2022-06-09 16:55:12 (GMT)
commit30610d28374f5a9698d456cebf3ae496ac01af51 (patch)
tree92599812169f8f0239cc9010c8e2e4ab2ded98f1 /Lib/test/_test_multiprocessing.py
parent6099611af5b9688f015ae4796501ce101a1c2f32 (diff)
downloadcpython-30610d28374f5a9698d456cebf3ae496ac01af51.zip
cpython-30610d28374f5a9698d456cebf3ae496ac01af51.tar.gz
cpython-30610d28374f5a9698d456cebf3ae496ac01af51.tar.bz2
gh-90549: Fix leak of global named resources using multiprocessing spawn (#30617)
Co-authored-by: XD Trol <milestonejxd@gmail.com> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Diffstat (limited to 'Lib/test/_test_multiprocessing.py')
-rw-r--r--Lib/test/_test_multiprocessing.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 67bb17c..4a588d9 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -5,6 +5,7 @@
import unittest
import unittest.mock
import queue as pyqueue
+import textwrap
import time
import io
import itertools
@@ -5760,6 +5761,35 @@ class TestSyncManagerTypes(unittest.TestCase):
self.run_worker(self._test_namespace, o)
+class TestNamedResource(unittest.TestCase):
+ def test_global_named_resource_spawn(self):
+ #
+ # gh-90549: Check that global named resources in main module
+ # will not leak by a subprocess, in spawn context.
+ #
+ testfn = os_helper.TESTFN
+ self.addCleanup(os_helper.unlink, testfn)
+ with open(testfn, 'w', encoding='utf-8') as f:
+ f.write(textwrap.dedent('''\
+ import multiprocessing as mp
+
+ ctx = mp.get_context('spawn')
+
+ global_resource = ctx.Semaphore()
+
+ def submain(): pass
+
+ if __name__ == '__main__':
+ p = ctx.Process(target=submain)
+ p.start()
+ p.join()
+ '''))
+ rc, out, err = test.support.script_helper.assert_python_ok(testfn)
+ # on error, err = 'UserWarning: resource_tracker: There appear to
+ # be 1 leaked semaphore objects to clean up at shutdown'
+ self.assertEqual(err, b'')
+
+
class MiscTestCase(unittest.TestCase):
def test__all__(self):
# Just make sure names in not_exported are excluded