[Python-checkins] bpo-37278: Fix test_asyncio ProactorLoopCtrlC (GH-14074)

Victor Stinner webhook-mailer at python.org
Fri Jun 14 07:03:07 EDT 2019

commit: 07559450b2d9179e4c99e0af088ce7550e549f94
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019-06-14T13:02:51+02:00

bpo-37278: Fix test_asyncio ProactorLoopCtrlC (GH-14074)

Join the thread to prevent leaking a running thread and leaking a

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.

A Misc/NEWS.d/next/Tests/2019-06-14-12-21-47.bpo-37278.z0HUOr.rst
M Lib/test/test_asyncio/test_windows_events.py

diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py
index 13aef7cf1f77..1e1c01d713b5 100644
--- a/Lib/test/test_asyncio/test_windows_events.py
+++ b/Lib/test/test_asyncio/test_windows_events.py
@@ -45,20 +45,21 @@ class ProactorLoopCtrlC(test_utils.TestCase):
     def test_ctrl_c(self):
         def SIGINT_after_delay():
-            time.sleep(1)
+            time.sleep(0.1)
-        asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
-        l = asyncio.get_event_loop()
+        thread = threading.Thread(target=SIGINT_after_delay)
+        loop = asyncio.get_event_loop()
-            t = threading.Thread(target=SIGINT_after_delay)
-            t.start()
-            l.run_forever()
+            # only start the loop once the event loop is running
+            loop.call_soon(thread.start)
+            loop.run_forever()
             self.fail("should not fall through 'run_forever'")
         except KeyboardInterrupt:
-            l.close()
+            self.close_loop(loop)
+        thread.join()
 class ProactorTests(test_utils.TestCase):
diff --git a/Misc/NEWS.d/next/Tests/2019-06-14-12-21-47.bpo-37278.z0HUOr.rst b/Misc/NEWS.d/next/Tests/2019-06-14-12-21-47.bpo-37278.z0HUOr.rst
new file mode 100644
index 000000000000..3d3011b51c5b
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-06-14-12-21-47.bpo-37278.z0HUOr.rst
@@ -0,0 +1,2 @@
+Fix test_asyncio ProactorLoopCtrlC: join the thread to prevent leaking a
+running thread and leaking a reference.

More information about the Python-checkins mailing list