Skip to content

Commit 16432be

Browse files
authored
bpo-31250, test_asyncio: fix dangling threads (#3252)
* Explicitly call shutdown(wait=True) on executors to wait until all threads complete to prevent side effects between tests. * Fix test_loop_self_reading_exception(): don't mock loop.close(). Previously, the original close() method was called rather than the mock, because how set_event_loop() registered loop.close().
1 parent 6c2feab commit 16432be

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎Lib/asyncio/test_utils.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,19 @@ def get_function_source(func):
437437

438438

439439
class TestCase(unittest.TestCase):
440+
@staticmethod
441+
def close_loop(loop):
442+
executor = loop._default_executor
443+
if executor is not None:
444+
executor.shutdown(wait=True)
445+
loop.close()
446+
440447
def set_event_loop(self, loop, *, cleanup=True):
441448
assert loop is not None
442449
# ensure that the event loop is passed explicitly in asyncio
443450
events.set_event_loop(None)
444451
if cleanup:
445-
self.addCleanup(loop.close)
452+
self.addCleanup(self.close_loop, loop)
446453

447454
def new_test_loop(self, gen=None):
448455
loop = TestLoop(gen)

‎Lib/test/test_asyncio/test_futures.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def run(arg):
380380
self.assertTrue(asyncio.isfuture(f2))
381381
self.assertEqual(res, 'oi')
382382
self.assertNotEqual(ident, threading.get_ident())
383+
ex.shutdown(wait=True)
383384

384385
def test_wrap_future_future(self):
385386
f1 = self._new_future(loop=self.loop)
@@ -395,6 +396,7 @@ def run(arg):
395396
f1 = ex.submit(run, 'oi')
396397
f2 = asyncio.wrap_future(f1)
397398
self.assertIs(self.loop, f2._loop)
399+
ex.shutdown(wait=True)
398400

399401
def test_wrap_future_cancel(self):
400402
f1 = concurrent.futures.Future()

‎Lib/test/test_asyncio/test_proactor_events.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ def test_loop_self_reading_fut(self):
529529
self.loop._loop_self_reading)
530530

531531
def test_loop_self_reading_exception(self):
532-
self.loop.close = mock.Mock()
533532
self.loop.call_exception_handler = mock.Mock()
534533
self.proactor.recv.side_effect = OSError()
535534
self.loop._loop_self_reading()

0 commit comments

Comments
 (0)