Skip to content

Commit cbf474c

Browse files
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 1b53a24) Co-authored-by: Victor Stinner <[email protected]>
1 parent c54b54b commit cbf474c

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

Lib/asyncio/proactor_events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,9 @@ def __init__(self, proactor):
627627
self._accept_futures = {} # socket file descriptor => Future
628628
proactor.set_loop(self)
629629
self._make_self_pipe()
630-
self_no = self._csock.fileno()
631630
if threading.current_thread() is threading.main_thread():
632631
# wakeup fd can only be installed to a file descriptor from the main thread
633-
signal.set_wakeup_fd(self_no)
632+
signal.set_wakeup_fd(self._csock.fileno())
634633

635634
def _make_socket_transport(self, sock, protocol, waiter=None,
636635
extra=None, server=None):
@@ -676,7 +675,8 @@ def close(self):
676675
if self.is_closed():
677676
return
678677

679-
signal.set_wakeup_fd(-1)
678+
if threading.current_thread() is threading.main_thread():
679+
signal.set_wakeup_fd(-1)
680680
# Call these methods before closing the event loop (before calling
681681
# BaseEventLoop.close), because they can schedule callbacks with
682682
# call_soon(), which is forbidden when the event loop is closed.

Lib/test/test_asyncio/test_windows_events.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def func():
6969
nonlocal finished
7070
loop = asyncio.new_event_loop()
7171
loop.run_until_complete(coro())
72+
# close() must not call signal.set_wakeup_fd()
73+
loop.close()
7274
finished = True
7375

7476
thread = threading.Thread(target=func)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
asynci.ProactorEventLoop.close() now only calls signal.set_wakeup_fd() in the
2+
main thread.

0 commit comments

Comments
 (0)