gh-90549: Fix leak of global named resources using multiprocessing spawn (GH-30617)

Co-authored-by: XD Trol <milestonejxd@gmail.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
(cherry picked from commit 30610d2837)

Co-authored-by: Leo Trol <milestone.jxd@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-06-10 05:17:11 -07:00 committed by GitHub
parent 8d8251a9b1
commit 2ad51c636a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 2 deletions

View file

@ -5,6 +5,7 @@
import unittest
import unittest.mock
import queue as pyqueue
import textwrap
import time
import io
import itertools
@ -5699,6 +5700,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