[Python-checkins] bpo-34679: ProactorEventLoop only uses set_wakeup_fd() in main thread (GH-16901)

Miss Skeleton (bot) webhook-mailer at python.org
Wed Oct 23 11:44:04 EDT 2019


https://github.com/python/cpython/commit/cbf474c98e702d12c97cd16a1e44ede10ea52b5b
commit: cbf474c98e702d12c97cd16a1e44ede10ea52b5b
branch: 3.8
author: Miss Skeleton (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-10-23T08:43:57-07:00
summary:

bpo-34679: ProactorEventLoop only uses set_wakeup_fd() in main thread (GH-16901)


bpo-34679, bpo-38563: asyncio.ProactorEventLoop.close() now only calls
signal.set_wakeup_fd() in the main thread.
(cherry picked from commit 1b53a24fb4417c764dd5933bce505f5c94249ca6)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Library/2019-10-23-16-25-12.bpo-34679.Bnw8o3.rst
M Lib/asyncio/proactor_events.py
M Lib/test/test_asyncio/test_windows_events.py

diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
index 229f56e6bb9e2..830d8edc32f90 100644
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -627,10 +627,9 @@ def __init__(self, proactor):
         self._accept_futures = {}   # socket file descriptor => Future
         proactor.set_loop(self)
         self._make_self_pipe()
-        self_no = self._csock.fileno()
         if threading.current_thread() is threading.main_thread():
             # wakeup fd can only be installed to a file descriptor from the main thread
-            signal.set_wakeup_fd(self_no)
+            signal.set_wakeup_fd(self._csock.fileno())
 
     def _make_socket_transport(self, sock, protocol, waiter=None,
                                extra=None, server=None):
@@ -676,7 +675,8 @@ def close(self):
         if self.is_closed():
             return
 
-        signal.set_wakeup_fd(-1)
+        if threading.current_thread() is threading.main_thread():
+            signal.set_wakeup_fd(-1)
         # Call these methods before closing the event loop (before calling
         # BaseEventLoop.close), because they can schedule callbacks with
         # call_soon(), which is forbidden when the event loop is closed.
diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py
index 9ed10fc20f817..6b005702c9be7 100644
--- a/Lib/test/test_asyncio/test_windows_events.py
+++ b/Lib/test/test_asyncio/test_windows_events.py
@@ -69,6 +69,8 @@ def func():
             nonlocal finished
             loop = asyncio.new_event_loop()
             loop.run_until_complete(coro())
+            # close() must not call signal.set_wakeup_fd()
+            loop.close()
             finished = True
 
         thread = threading.Thread(target=func)
diff --git a/Misc/NEWS.d/next/Library/2019-10-23-16-25-12.bpo-34679.Bnw8o3.rst b/Misc/NEWS.d/next/Library/2019-10-23-16-25-12.bpo-34679.Bnw8o3.rst
new file mode 100644
index 0000000000000..34334db6032b5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-10-23-16-25-12.bpo-34679.Bnw8o3.rst
@@ -0,0 +1,2 @@
+asynci.ProactorEventLoop.close() now only calls signal.set_wakeup_fd() in the
+main thread.



More information about the Python-checkins mailing list