Skip to content

Commit a4bfbc9

Browse files
vstinnerlisroach
authored andcommitted
bpo-37278: Fix test_asyncio ProactorLoopCtrlC (pythonGH-14074)
Join the thread to prevent leaking a running thread and leaking a reference. Cleanup also the test: * asyncioWindowsProactorEventLoopPolicy became the default policy, there is no need to set it manually. * Only start the thread once the loop is running. * Use a shorter sleep in the thread (100 ms rather than 1 sec). * Use close_loop(loop) rather than loop.close(). * Use longer variable names.
1 parent ea4abb0 commit a4bfbc9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Lib/test/test_asyncio/test_windows_events.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ class ProactorLoopCtrlC(test_utils.TestCase):
4545
def test_ctrl_c(self):
4646

4747
def SIGINT_after_delay():
48-
time.sleep(1)
48+
time.sleep(0.1)
4949
signal.raise_signal(signal.SIGINT)
5050

51-
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
52-
l = asyncio.get_event_loop()
51+
thread = threading.Thread(target=SIGINT_after_delay)
52+
loop = asyncio.get_event_loop()
5353
try:
54-
t = threading.Thread(target=SIGINT_after_delay)
55-
t.start()
56-
l.run_forever()
54+
# only start the loop once the event loop is running
55+
loop.call_soon(thread.start)
56+
loop.run_forever()
5757
self.fail("should not fall through 'run_forever'")
5858
except KeyboardInterrupt:
5959
pass
6060
finally:
61-
l.close()
61+
self.close_loop(loop)
62+
thread.join()
6263

6364

6465
class ProactorTests(test_utils.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix test_asyncio ProactorLoopCtrlC: join the thread to prevent leaking a
2+
running thread and leaking a reference.

0 commit comments

Comments
 (0)