| 1 |
import os |
| 2 |
import signal |
| 3 |
import socket |
| 4 |
import sys |
| 5 |
import time |
| 6 |
import threading |
| 7 |
import unittest |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
if sys.platform != 'win32': |
| 11 |
raise unittest.SkipTest('Windows only') |
| 12 |
|
| 13 |
import _overlapped |
| 14 |
import _winapi |
| 15 |
|
| 16 |
import asyncio |
| 17 |
from asyncio import windows_events |
| 18 |
from test.test_asyncio import utils as test_utils |
| 19 |
|
| 20 |
|
| 21 |
def tearDownModule(): |
| 22 |
asyncio.events._set_event_loop_policy(None) |
| 23 |
|
| 24 |
|
| 25 |
class UpperProto(asyncio.Protocol): |
| 26 |
def __init__(self): |
| 27 |
self.buf = [] |
| 28 |
|
| 29 |
def connection_made(self, trans): |
| 30 |
self.trans = trans |
| 31 |
|
| 32 |
def data_received(self, data): |
| 33 |
self.buf.append(data) |
| 34 |
if b'\n' in data: |
| 35 |
self.trans.write(b''.join(self.buf).upper()) |
| 36 |
self.trans.close() |
| 37 |
|
| 38 |
|
| 39 |
class WindowsEventsTestCase(test_utils.TestCase): |
| 40 |
def _unraisablehook(self, unraisable): |
| 41 |
# Storing unraisable.object can resurrect an object which is being |
| 42 |
# finalized. Storing unraisable.exc_value creates a reference cycle. |
| 43 |
self._unraisable = unraisable |
| 44 |
print(unraisable) |
| 45 |
|
| 46 |
def setUp(self): |
| 47 |
self._prev_unraisablehook = sys.unraisablehook |
| 48 |
self._unraisable = None |
| 49 |
sys.unraisablehook = self._unraisablehook |
| 50 |
|
| 51 |
def tearDown(self): |
| 52 |
sys.unraisablehook = self._prev_unraisablehook |
| 53 |
self.assertIsNone(self._unraisable) |
| 54 |
|
| 55 |
class ProactorLoopCtrlC(WindowsEventsTestCase): |
| 56 |
|
| 57 |
def test_ctrl_c(self): |
| 58 |
|
| 59 |
def SIGINT_after_delay(): |
| 60 |
time.sleep(0.1) |
| 61 |
signal.raise_signal(signal.SIGINT) |
| 62 |
|
| 63 |
thread = threading.Thread(target=SIGINT_after_delay) |
| 64 |
loop = asyncio.new_event_loop() |
| 65 |
try: |
| 66 |
# only start the loop once the event loop is running |
| 67 |
loop.call_soon(thread.start) |
| 68 |
loop.run_forever() |
| 69 |
self.fail("should not fall through 'run_forever'") |
| 70 |
except KeyboardInterrupt: |
| 71 |
pass |
| 72 |
finally: |
| 73 |
self.close_loop(loop) |
| 74 |
thread.join() |
| 75 |
|
| 76 |
|
| 77 |
class ProactorMultithreading(WindowsEventsTestCase): |
| 78 |
def test_run_from_nonmain_thread(self): |
| 79 |
finished = False |
| 80 |
|
| 81 |
async def coro(): |
| 82 |
await asyncio.sleep(0) |
| 83 |
|
| 84 |
def func(): |
| 85 |
nonlocal finished |
| 86 |
loop = asyncio.new_event_loop() |
| 87 |
loop.run_until_complete(coro()) |
| 88 |
# close() must not call signal.set_wakeup_fd() |
| 89 |
loop.close() |
| 90 |
finished = True |
| 91 |
|
| 92 |
thread = threading.Thread(target=func) |
| 93 |
thread.start() |
| 94 |
thread.join() |
| 95 |
self.assertTrue(finished) |
| 96 |
|
| 97 |
|
| 98 |
class ProactorTests(WindowsEventsTestCase): |
| 99 |
|
| 100 |
def setUp(self): |
| 101 |
super().setUp() |
| 102 |
self.loop = asyncio.ProactorEventLoop() |
| 103 |
self.set_event_loop(self.loop) |
| 104 |
|
| 105 |
def test_close(self): |
| 106 |
a, b = socket.socketpair() |
| 107 |
trans = self.loop._make_socket_transport(a, asyncio.Protocol()) |
| 108 |
f = asyncio.ensure_future(self.loop.sock_recv(b, 100), loop=self.loop) |
| 109 |
trans.close() |
| 110 |
self.loop.run_until_complete(f) |
| 111 |
self.assertEqual(f.result(), b'') |
| 112 |
b.close() |
| 113 |
|
| 114 |
def test_double_bind(self): |
| 115 |
ADDRESS = r'\\.\pipe\test_double_bind-%s' % os.getpid() |
| 116 |
server1 = windows_events.PipeServer(ADDRESS) |
| 117 |
with self.assertRaises(PermissionError): |
| 118 |
windows_events.PipeServer(ADDRESS) |
| 119 |
server1.close() |
| 120 |
|
| 121 |
def test_pipe(self): |
| 122 |
res = self.loop.run_until_complete(self._test_pipe()) |
| 123 |
self.assertEqual(res, 'done') |
| 124 |
|
| 125 |
async def _test_pipe(self): |
| 126 |
ADDRESS = r'\\.\pipe\_test_pipe-%s' % os.getpid() |
| 127 |
|
| 128 |
with self.assertRaises(FileNotFoundError): |
| 129 |
await self.loop.create_pipe_connection( |
| 130 |
asyncio.Protocol, ADDRESS) |
| 131 |
|
| 132 |
[server] = await self.loop.start_serving_pipe( |
| 133 |
UpperProto, ADDRESS) |
| 134 |
self.assertIsInstance(server, windows_events.PipeServer) |
| 135 |
|
| 136 |
clients = [] |
| 137 |
for i in range(5): |
| 138 |
stream_reader = asyncio.StreamReader(loop=self.loop) |
| 139 |
protocol = asyncio.StreamReaderProtocol(stream_reader, |
| 140 |
loop=self.loop) |
| 141 |
trans, proto = await self.loop.create_pipe_connection( |
| 142 |
lambda: protocol, ADDRESS) |
| 143 |
self.assertIsInstance(trans, asyncio.Transport) |
| 144 |
self.assertEqual(protocol, proto) |
| 145 |
clients.append((stream_reader, trans)) |
| 146 |
|
| 147 |
for i, (r, w) in enumerate(clients): |
| 148 |
w.write('lower-{}\n'.format(i).encode()) |
| 149 |
|
| 150 |
for i, (r, w) in enumerate(clients): |
| 151 |
response = await r.readline() |
| 152 |
self.assertEqual(response, 'LOWER-{}\n'.format(i).encode()) |
| 153 |
w.close() |
| 154 |
|
| 155 |
server.close() |
| 156 |
|
| 157 |
with self.assertRaises(FileNotFoundError): |
| 158 |
await self.loop.create_pipe_connection( |
| 159 |
asyncio.Protocol, ADDRESS) |
| 160 |
|
| 161 |
return 'done' |
| 162 |
|
| 163 |
def test_connect_pipe_cancel(self): |
| 164 |
exc = OSError() |
| 165 |
exc.winerror = _overlapped.ERROR_PIPE_BUSY |
| 166 |
with mock.patch.object(_overlapped, 'ConnectPipe', |
| 167 |
side_effect=exc) as connect: |
| 168 |
coro = self.loop._proactor.connect_pipe('pipe_address') |
| 169 |
task = self.loop.create_task(coro) |
| 170 |
|
| 171 |
# check that it's possible to cancel connect_pipe() |
| 172 |
task.cancel() |
| 173 |
with self.assertRaises(asyncio.CancelledError): |
| 174 |
self.loop.run_until_complete(task) |
| 175 |
|
| 176 |
def test_wait_for_handle(self): |
| 177 |
event = _overlapped.CreateEvent(None, True, False, None) |
| 178 |
self.addCleanup(_winapi.CloseHandle, event) |
| 179 |
|
| 180 |
# Wait for unset event with 0.5s timeout; |
| 181 |
# result should be False at timeout |
| 182 |
timeout = 0.5 |
| 183 |
fut = self.loop._proactor.wait_for_handle(event, timeout) |
| 184 |
start = self.loop.time() |
| 185 |
done = self.loop.run_until_complete(fut) |
| 186 |
elapsed = self.loop.time() - start |
| 187 |
|
| 188 |
self.assertEqual(done, False) |
| 189 |
self.assertFalse(fut.result()) |
| 190 |
self.assertGreaterEqual(elapsed, timeout - test_utils.CLOCK_RES) |
| 191 |
|
| 192 |
_overlapped.SetEvent(event) |
| 193 |
|
| 194 |
# Wait for set event; |
| 195 |
# result should be True immediately |
| 196 |
fut = self.loop._proactor.wait_for_handle(event, 10) |
| 197 |
done = self.loop.run_until_complete(fut) |
| 198 |
|
| 199 |
self.assertEqual(done, True) |
| 200 |
self.assertTrue(fut.result()) |
| 201 |
|
| 202 |
# asyncio issue #195: cancelling a done _WaitHandleFuture |
| 203 |
# must not crash |
| 204 |
fut.cancel() |
| 205 |
|
| 206 |
def test_wait_for_handle_cancel(self): |
| 207 |
event = _overlapped.CreateEvent(None, True, False, None) |
| 208 |
self.addCleanup(_winapi.CloseHandle, event) |
| 209 |
|
| 210 |
# Wait for unset event with a cancelled future; |
| 211 |
# CancelledError should be raised immediately |
| 212 |
fut = self.loop._proactor.wait_for_handle(event, 10) |
| 213 |
fut.cancel() |
| 214 |
with self.assertRaises(asyncio.CancelledError): |
| 215 |
self.loop.run_until_complete(fut) |
| 216 |
|
| 217 |
# asyncio issue #195: cancelling a _WaitHandleFuture twice |
| 218 |
# must not crash |
| 219 |
fut = self.loop._proactor.wait_for_handle(event) |
| 220 |
fut.cancel() |
| 221 |
fut.cancel() |
| 222 |
|
| 223 |
def test_read_self_pipe_restart(self): |
| 224 |
# Regression test for https://bugs.python.org/issue39010 |
| 225 |
# Previously, restarting a proactor event loop in certain states |
| 226 |
# would lead to spurious ConnectionResetErrors being logged. |
| 227 |
self.loop.call_exception_handler = mock.Mock() |
| 228 |
# Start an operation in another thread so that the self-pipe is used. |
| 229 |
# This is theoretically timing-dependent (the task in the executor |
| 230 |
# must complete before our start/stop cycles), but in practice it |
| 231 |
# seems to work every time. |
| 232 |
f = self.loop.run_in_executor(None, lambda: None) |
| 233 |
self.loop.stop() |
| 234 |
self.loop.run_forever() |
| 235 |
self.loop.stop() |
| 236 |
self.loop.run_forever() |
| 237 |
|
| 238 |
# Shut everything down cleanly. This is an important part of the |
| 239 |
# test - in issue 39010, the error occurred during loop.close(), |
| 240 |
# so we want to close the loop during the test instead of leaving |
| 241 |
# it for tearDown. |
| 242 |
# |
| 243 |
# First wait for f to complete to avoid a "future's result was never |
| 244 |
# retrieved" error. |
| 245 |
self.loop.run_until_complete(f) |
| 246 |
# Now shut down the loop itself (self.close_loop also shuts down the |
| 247 |
# loop's default executor). |
| 248 |
self.close_loop(self.loop) |
| 249 |
self.assertFalse(self.loop.call_exception_handler.called) |
| 250 |
|
| 251 |
def test_address_argument_type_error(self): |
| 252 |
# Regression test for https://github.com/python/cpython/issues/98793 |
| 253 |
proactor = self.loop._proactor |
| 254 |
sock = socket.socket(type=socket.SOCK_DGRAM) |
| 255 |
bad_address = None |
| 256 |
with self.assertRaises(TypeError): |
| 257 |
proactor.connect(sock, bad_address) |
| 258 |
with self.assertRaises(TypeError): |
| 259 |
proactor.sendto(sock, b'abc', addr=bad_address) |
| 260 |
sock.close() |
| 261 |
|
| 262 |
def test_client_pipe_stat(self): |
| 263 |
res = self.loop.run_until_complete(self._test_client_pipe_stat()) |
| 264 |
self.assertEqual(res, 'done') |
| 265 |
|
| 266 |
async def _test_client_pipe_stat(self): |
| 267 |
# Regression test for https://github.com/python/cpython/issues/100573 |
| 268 |
ADDRESS = r'\\.\pipe\test_client_pipe_stat-%s' % os.getpid() |
| 269 |
|
| 270 |
async def probe(): |
| 271 |
# See https://github.com/python/cpython/pull/100959#discussion_r1068533658 |
| 272 |
h = _overlapped.ConnectPipe(ADDRESS) |
| 273 |
try: |
| 274 |
_winapi.CloseHandle(_overlapped.ConnectPipe(ADDRESS)) |
| 275 |
except OSError as e: |
| 276 |
if e.winerror != _overlapped.ERROR_PIPE_BUSY: |
| 277 |
raise |
| 278 |
finally: |
| 279 |
_winapi.CloseHandle(h) |
| 280 |
|
| 281 |
with self.assertRaises(FileNotFoundError): |
| 282 |
await probe() |
| 283 |
|
| 284 |
[server] = await self.loop.start_serving_pipe(asyncio.Protocol, ADDRESS) |
| 285 |
self.assertIsInstance(server, windows_events.PipeServer) |
| 286 |
|
| 287 |
errors = [] |
| 288 |
self.loop.set_exception_handler(lambda _, data: errors.append(data)) |
| 289 |
|
| 290 |
for i in range(5): |
| 291 |
await self.loop.create_task(probe()) |
| 292 |
|
| 293 |
self.assertEqual(len(errors), 0, errors) |
| 294 |
|
| 295 |
server.close() |
| 296 |
|
| 297 |
with self.assertRaises(FileNotFoundError): |
| 298 |
await probe() |
| 299 |
|
| 300 |
return "done" |
| 301 |
|
| 302 |
def test_loop_restart(self): |
| 303 |
# We're fishing for the "RuntimeError: <_overlapped.Overlapped object at XXX> |
| 304 |
# still has pending operation at deallocation, the process may crash" error |
| 305 |
stop = threading.Event() |
| 306 |
def threadMain(): |
| 307 |
while not stop.is_set(): |
| 308 |
self.loop.call_soon_threadsafe(lambda: None) |
| 309 |
time.sleep(0.01) |
| 310 |
thr = threading.Thread(target=threadMain) |
| 311 |
|
| 312 |
# In 10 60-second runs of this test prior to the fix: |
| 313 |
# time in seconds until failure: (none), 15.0, 6.4, (none), 7.6, 8.3, 1.7, 22.2, 23.5, 8.3 |
| 314 |
# 10 seconds had a 50% failure rate but longer would be more costly |
| 315 |
end_time = time.time() + 10 # Run for 10 seconds |
| 316 |
self.loop.call_soon(thr.start) |
| 317 |
while not self._unraisable: # Stop if we got an unraisable exc |
| 318 |
self.loop.stop() |
| 319 |
self.loop.run_forever() |
| 320 |
if time.time() >= end_time: |
| 321 |
break |
| 322 |
|
| 323 |
stop.set() |
| 324 |
thr.join() |
| 325 |
|
| 326 |
|
| 327 |
class WinPolicyTests(WindowsEventsTestCase): |
| 328 |
|
| 329 |
def test_selector_win_policy(self): |
| 330 |
async def main(): |
| 331 |
self.assertIsInstance(asyncio.get_running_loop(), asyncio.SelectorEventLoop) |
| 332 |
|
| 333 |
old_policy = asyncio.events._get_event_loop_policy() |
| 334 |
try: |
| 335 |
with self.assertWarnsRegex( |
| 336 |
DeprecationWarning, |
| 337 |
"'asyncio.WindowsSelectorEventLoopPolicy' is deprecated", |
| 338 |
): |
| 339 |
asyncio.events._set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 340 |
asyncio.run(main()) |
| 341 |
finally: |
| 342 |
asyncio.events._set_event_loop_policy(old_policy) |
| 343 |
|
| 344 |
def test_proactor_win_policy(self): |
| 345 |
async def main(): |
| 346 |
self.assertIsInstance( |
| 347 |
asyncio.get_running_loop(), |
| 348 |
asyncio.ProactorEventLoop) |
| 349 |
|
| 350 |
old_policy = asyncio.events._get_event_loop_policy() |
| 351 |
try: |
| 352 |
with self.assertWarnsRegex( |
| 353 |
DeprecationWarning, |
| 354 |
"'asyncio.WindowsProactorEventLoopPolicy' is deprecated", |
| 355 |
): |
| 356 |
asyncio.events._set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) |
| 357 |
asyncio.run(main()) |
| 358 |
finally: |
| 359 |
asyncio.events._set_event_loop_policy(old_policy) |
| 360 |
|
| 361 |
|
| 362 |
if __name__ == '__main__': |
| 363 |
unittest.main() |