Skip to content

Commit 5013a5e

Browse files
authored
[3.6] bpo-31250: test_asyncio: fix dangling threads (#3517)
* 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(). (cherry picked from commit 16432be) * bpo-31250, test_asyncio: fix EventLoopTestsMixin.tearDown() (#3264) Call doCleanups() to close the loop after calling executor.shutdown(wait=True): see TestCase.set_event_loop() of asyncio.test_utils. Replace also gc.collect() with support.gc_collect(). (cherry picked from commit e8a533f)
1 parent 4d7807a commit 5013a5e

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

Lib/asyncio/test_utils.py

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

439439

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

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

Lib/test/test_asyncio/test_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ def tearDown(self):
258258
if not self.loop.is_closed():
259259
test_utils.run_briefly(self.loop)
260260

261-
self.loop.close()
262-
gc.collect()
261+
self.doCleanups()
262+
support.gc_collect()
263263
super().tearDown()
264264

265265
def test_run_until_complete_nesting(self):

Lib/test/test_asyncio/test_futures.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ def run(arg):
413413
self.assertTrue(asyncio.isfuture(f2))
414414
self.assertEqual(res, 'oi')
415415
self.assertNotEqual(ident, threading.get_ident())
416+
ex.shutdown(wait=True)
416417

417418
def test_wrap_future_future(self):
418419
f1 = self._new_future(loop=self.loop)
@@ -428,6 +429,7 @@ def run(arg):
428429
f1 = ex.submit(run, 'oi')
429430
f2 = asyncio.wrap_future(f1)
430431
self.assertIs(self.loop, f2._loop)
432+
ex.shutdown(wait=True)
431433

432434
def test_wrap_future_cancel(self):
433435
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)