bpo-31250, test_asyncio: fix dangling threads (#3252) · python/cpython@16432be

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)