Skip to content

Commit 69d22b8

Browse files
bpo-34679: Restore instantiation Windows IOCP event loop from non-main thread (GH-15492)
* Restore running proactor event loop from non-main thread Co-Authored-By: Kyle Stanley <[email protected]> (cherry picked from commit 1c06009) Co-authored-by: Andrew Svetlov <[email protected]>
1 parent 522a394 commit 69d22b8

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/asyncio/proactor_events.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import socket
1212
import warnings
1313
import signal
14+
import threading
1415
import collections
1516

1617
from . import base_events
@@ -627,7 +628,9 @@ def __init__(self, proactor):
627628
proactor.set_loop(self)
628629
self._make_self_pipe()
629630
self_no = self._csock.fileno()
630-
signal.set_wakeup_fd(self_no)
631+
if threading.current_thread() is threading.main_thread():
632+
# wakeup fd can only be installed to a file descriptor from the main thread
633+
signal.set_wakeup_fd(self_no)
631634

632635
def _make_socket_transport(self, sock, protocol, waiter=None,
633636
extra=None, server=None):

Lib/test/test_asyncio/test_windows_events.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ def SIGINT_after_delay():
5959
thread.join()
6060

6161

62+
class ProactorMultithreading(test_utils.TestCase):
63+
def test_run_from_nonmain_thread(self):
64+
finished = False
65+
66+
async def coro():
67+
await asyncio.sleep(0)
68+
69+
def func():
70+
nonlocal finished
71+
loop = asyncio.new_event_loop()
72+
loop.run_until_complete(coro())
73+
finished = True
74+
75+
thread = threading.Thread(target=func)
76+
thread.start()
77+
thread.join()
78+
self.assertTrue(finished)
79+
80+
6281
class ProactorTests(test_utils.TestCase):
6382

6483
def setUp(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restores instantiation of Windows IOCP event loops from the non-main thread.

0 commit comments

Comments
 (0)