Skip to content

Commit abf3a68

Browse files
[3.11] gh-109295: Clean up multiprocessing in test_asyncio and test_compileall (GH-109298) (#109302)
gh-109295: Clean up multiprocessing in test_asyncio and test_compileall (GH-109298) test_asyncio and test_compileall now clean up multiprocessing by calling multiprocessing _cleanup_tests(): explicitly clean up resources and stop background processes like the resource tracker. (cherry picked from commit 09ea4b8) Co-authored-by: Victor Stinner <[email protected]>
1 parent 82a1806 commit abf3a68

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Lib/test/test_asyncio/test_events.py

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from asyncio import events
3232
from asyncio import proactor_events
3333
from asyncio import selector_events
34+
from multiprocessing.util import _cleanup_tests as multiprocessing_cleanup_tests
3435
from test.test_asyncio import utils as test_utils
3536
from test import support
3637
from test.support import socket_helper
@@ -2730,6 +2731,8 @@ def test_get_event_loop_new_process(self):
27302731
# multiprocessing.synchronize module cannot be imported.
27312732
support.skip_if_broken_multiprocessing_synchronize()
27322733

2734+
self.addCleanup(multiprocessing_cleanup_tests)
2735+
27332736
async def main():
27342737
pool = concurrent.futures.ProcessPoolExecutor()
27352738
result = await self.loop.run_in_executor(

Lib/test/test_compileall.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
try:
1919
# compileall relies on ProcessPoolExecutor if ProcessPoolExecutor exists
2020
# and it can function.
21+
from multiprocessing.util import _cleanup_tests as multiprocessing_cleanup_tests
2122
from concurrent.futures import ProcessPoolExecutor
2223
from concurrent.futures.process import _check_system_limits
2324
_check_system_limits()
@@ -54,6 +55,8 @@ class CompileallTestsBase:
5455

5556
def setUp(self):
5657
self.directory = tempfile.mkdtemp()
58+
self.addCleanup(shutil.rmtree, self.directory)
59+
5760
self.source_path = os.path.join(self.directory, '_test.py')
5861
self.bc_path = importlib.util.cache_from_source(self.source_path)
5962
with open(self.source_path, 'w', encoding="utf-8") as file:
@@ -66,9 +69,6 @@ def setUp(self):
6669
self.source_path3 = os.path.join(self.subdirectory, '_test3.py')
6770
shutil.copyfile(self.source_path, self.source_path3)
6871

69-
def tearDown(self):
70-
shutil.rmtree(self.directory)
71-
7272
def add_bad_source_file(self):
7373
self.bad_source_path = os.path.join(self.directory, '_test_bad.py')
7474
with open(self.bad_source_path, 'w', encoding="utf-8") as file:
@@ -307,9 +307,13 @@ def _test_ddir_only(self, *, ddir, parallel=True):
307307
script_helper.make_script(path, "__init__", "")
308308
mods.append(script_helper.make_script(path, "mod",
309309
"def fn(): 1/0\nfn()\n"))
310+
311+
if parallel:
312+
self.addCleanup(multiprocessing_cleanup_tests)
310313
compileall.compile_dir(
311314
self.directory, quiet=True, ddir=ddir,
312315
workers=2 if parallel else 1)
316+
313317
self.assertTrue(mods)
314318
for mod in mods:
315319
self.assertTrue(mod.startswith(self.directory), mod)

0 commit comments

Comments
 (0)