Debian Codegraph @

src/trio/_core/_tests/test_io.py

raw
1 from __future__ import annotations
2
3 import random
4 import select
5 import socket as stdlib_socket
6 import sys
7 from collections.abc import Awaitable, Callable
8 from contextlib import suppress
9 from typing import TYPE_CHECKING, TypeVar
10
11 import pytest
12
13 import trio
14
15 from ... import _core
16 from ...testing import assert_checkpoints, wait_all_tasks_blocked
17
18 # Cross-platform tests for IO handling
19
20 if TYPE_CHECKING:
21 from collections.abc import Generator
22
23 from typing_extensions import ParamSpec
24
25 ArgsT = ParamSpec("ArgsT")
26
27
28 def fill_socket(sock: stdlib_socket.socket) -> None:
29 try:
30 while True:
31 sock.send(b"x" * 65536)
32 except BlockingIOError:
33 pass
34
35
36 def drain_socket(sock: stdlib_socket.socket) -> None:
37 try:
38 while True:
39 sock.recv(65536)
40 except BlockingIOError:
41 pass
42
43
44 WaitSocket = Callable[[stdlib_socket.socket], Awaitable[object]]
45 SocketPair = tuple[stdlib_socket.socket, stdlib_socket.socket]
46 RetT = TypeVar("RetT")
47
48
49 @pytest.fixture
50 def socketpair() -> Generator[SocketPair, None, None]:
51 pair = stdlib_socket.socketpair()
52 for sock in pair:
53 sock.setblocking(False)
54 yield pair
55 for sock in pair:
56 sock.close()
57
58
59 def also_using_fileno(
60 fn: Callable[[stdlib_socket.socket | int], RetT],
61 ) -> list[Callable[[stdlib_socket.socket], RetT]]:
62 def fileno_wrapper(fileobj: stdlib_socket.socket) -> RetT:
63 return fn(fileobj.fileno())
64
65 name = f"<{fn.__name__} on fileno>"
66 fileno_wrapper.__name__ = fileno_wrapper.__qualname__ = name
67 return [fn, fileno_wrapper]
68
69
70 # Decorators that feed in different settings for wait_readable / wait_writable
71 # / notify_closing.
72 # Note that if you use all three decorators on the same test, it will run all
73 # N**3 *combinations*
74 read_socket_test = pytest.mark.parametrize(
75 "wait_readable",
76 also_using_fileno(trio.lowlevel.wait_readable),
77 ids=lambda fn: fn.__name__,
78 )
79 write_socket_test = pytest.mark.parametrize(
80 "wait_writable",
81 also_using_fileno(trio.lowlevel.wait_writable),
82 ids=lambda fn: fn.__name__,
83 )
84 notify_closing_test = pytest.mark.parametrize(
85 "notify_closing",
86 also_using_fileno(trio.lowlevel.notify_closing),
87 ids=lambda fn: fn.__name__,
88 )
89
90
91 # XX These tests are all a bit dicey because they can't distinguish between
92 # wait_on_{read,writ}able blocking the way it should, versus blocking
93 # momentarily and then immediately resuming.
94 @read_socket_test
95 @write_socket_test
96 async def test_wait_basic(
97 socketpair: SocketPair,
98 wait_readable: WaitSocket,
99 wait_writable: WaitSocket,
100 ) -> None:
101 a, b = socketpair
102
103 # They start out writable()
104 with assert_checkpoints():
105 await wait_writable(a)
106
107 # But readable() blocks until data arrives
108 record = []
109
110 async def block_on_read() -> None:
111 try:
112 with assert_checkpoints():
113 await wait_readable(a)
114 except _core.Cancelled:
115 record.append("cancelled")
116 else:
117 record.append("readable")
118 assert a.recv(10) == b"x"
119
120 async with _core.open_nursery() as nursery:
121 nursery.start_soon(block_on_read)
122 await wait_all_tasks_blocked()
123 assert record == []
124 b.send(b"x")
125
126 fill_socket(a)
127
128 # Now writable will block, but readable won't
129 with assert_checkpoints():
130 await wait_readable(b)
131 record = []
132
133 async def block_on_write() -> None:
134 try:
135 with assert_checkpoints():
136 await wait_writable(a)
137 except _core.Cancelled:
138 record.append("cancelled")
139 else:
140 record.append("writable")
141
142 async with _core.open_nursery() as nursery:
143 nursery.start_soon(block_on_write)
144 await wait_all_tasks_blocked()
145 assert record == []
146 drain_socket(b)
147
148 # check cancellation
149 record = []
150 async with _core.open_nursery() as nursery:
151 nursery.start_soon(block_on_read)
152 await wait_all_tasks_blocked()
153 nursery.cancel_scope.cancel()
154 assert record == ["cancelled"]
155
156 fill_socket(a)
157 record = []
158 async with _core.open_nursery() as nursery:
159 nursery.start_soon(block_on_write)
160 await wait_all_tasks_blocked()
161 nursery.cancel_scope.cancel()
162 assert record == ["cancelled"]
163
164
165 @read_socket_test
166 async def test_double_read(socketpair: SocketPair, wait_readable: WaitSocket) -> None:
167 a, _b = socketpair
168
169 # You can't have two tasks trying to read from a socket at the same time
170 async with _core.open_nursery() as nursery:
171 nursery.start_soon(wait_readable, a)
172 await wait_all_tasks_blocked()
173 with pytest.raises(_core.BusyResourceError):
174 await wait_readable(a)
175 nursery.cancel_scope.cancel()
176
177
178 @write_socket_test
179 async def test_double_write(socketpair: SocketPair, wait_writable: WaitSocket) -> None:
180 a, _b = socketpair
181
182 # You can't have two tasks trying to write to a socket at the same time
183 fill_socket(a)
184 async with _core.open_nursery() as nursery:
185 nursery.start_soon(wait_writable, a)
186 await wait_all_tasks_blocked()
187 with pytest.raises(_core.BusyResourceError):
188 await wait_writable(a)
189 nursery.cancel_scope.cancel()
190
191
192 @read_socket_test
193 @write_socket_test
194 @notify_closing_test
195 async def test_interrupted_by_close(
196 socketpair: SocketPair,
197 wait_readable: WaitSocket,
198 wait_writable: WaitSocket,
199 notify_closing: Callable[[stdlib_socket.socket], object],
200 ) -> None:
201 a, _b = socketpair
202
203 async def reader() -> None:
204 with pytest.raises(_core.ClosedResourceError):
205 await wait_readable(a)
206
207 async def writer() -> None:
208 with pytest.raises(_core.ClosedResourceError):
209 await wait_writable(a)
210
211 fill_socket(a)
212
213 async with _core.open_nursery() as nursery:
214 nursery.start_soon(reader)
215 nursery.start_soon(writer)
216 await wait_all_tasks_blocked()
217 notify_closing(a)
218
219
220 @read_socket_test
221 @write_socket_test
222 async def test_socket_simultaneous_read_write(
223 socketpair: SocketPair,
224 wait_readable: WaitSocket,
225 wait_writable: WaitSocket,
226 ) -> None:
227 record: list[str] = []
228
229 async def r_task(sock: stdlib_socket.socket) -> None:
230 await wait_readable(sock)
231 record.append("r_task")
232
233 async def w_task(sock: stdlib_socket.socket) -> None:
234 await wait_writable(sock)
235 record.append("w_task")
236
237 a, b = socketpair
238 fill_socket(a)
239 async with _core.open_nursery() as nursery:
240 nursery.start_soon(r_task, a)
241 nursery.start_soon(w_task, a)
242 await wait_all_tasks_blocked()
243 assert record == []
244 b.send(b"x")
245 await wait_all_tasks_blocked()
246 assert record == ["r_task"]
247 drain_socket(b)
248 await wait_all_tasks_blocked()
249 assert record == ["r_task", "w_task"]
250
251
252 @read_socket_test
253 @write_socket_test
254 async def test_socket_actual_streaming(
255 socketpair: SocketPair,
256 wait_readable: WaitSocket,
257 wait_writable: WaitSocket,
258 ) -> None:
259 a, b = socketpair
260
261 # Use a small send buffer on one of the sockets to increase the chance of
262 # getting partial writes
263 a.setsockopt(stdlib_socket.SOL_SOCKET, stdlib_socket.SO_SNDBUF, 10000)
264
265 N = 1000000 # 1 megabyte
266 MAX_CHUNK = 65536
267
268 results: dict[str, int] = {}
269
270 async def sender(sock: stdlib_socket.socket, seed: int, key: str) -> None:
271 r = random.Random(seed)
272 sent = 0
273 while sent < N:
274 print("sent", sent)
275 chunk = bytearray(r.randrange(MAX_CHUNK))
276 while chunk:
277 with assert_checkpoints():
278 await wait_writable(sock)
279 this_chunk_size = sock.send(chunk)
280 sent += this_chunk_size
281 del chunk[:this_chunk_size]
282 sock.shutdown(stdlib_socket.SHUT_WR)
283 results[key] = sent
284
285 async def receiver(sock: stdlib_socket.socket, key: str) -> None:
286 received = 0
287 while True:
288 print("received", received)
289 with assert_checkpoints():
290 await wait_readable(sock)
291 this_chunk_size = len(sock.recv(MAX_CHUNK))
292 if not this_chunk_size:
293 break
294 received += this_chunk_size
295 results[key] = received
296
297 async with _core.open_nursery() as nursery:
298 nursery.start_soon(sender, a, 0, "send_a")
299 nursery.start_soon(sender, b, 1, "send_b")
300 nursery.start_soon(receiver, a, "recv_a")
301 nursery.start_soon(receiver, b, "recv_b")
302
303 assert results["send_a"] == results["recv_b"]
304 assert results["send_b"] == results["recv_a"]
305
306
307 async def test_notify_closing_on_invalid_object() -> None:
308 # It should either be a no-op (generally on Unix, where we don't know
309 # which fds are valid), or an OSError (on Windows, where we currently only
310 # support sockets, so we have to do some validation to figure out whether
311 # it's a socket or a regular handle).
312 got_oserror = False
313 got_no_error = False
314 try:
315 trio.lowlevel.notify_closing(-1)
316 except OSError:
317 got_oserror = True
318 else:
319 got_no_error = True
320 assert got_oserror or got_no_error
321
322
323 async def test_wait_on_invalid_object() -> None:
324 # We definitely want to raise an error everywhere if you pass in an
325 # invalid fd to wait_*
326 for wait in [trio.lowlevel.wait_readable, trio.lowlevel.wait_writable]:
327 with stdlib_socket.socket() as s:
328 fileno = s.fileno()
329 # We just closed the socket and don't do anything else in between, so
330 # we can be confident that the fileno hasn't be reassigned.
331 with pytest.raises(
332 OSError,
333 match=r"^\[\w+ \d+] (Bad file descriptor|An operation was attempted on something that is not a socket)$",
334 ):
335 await wait(fileno)
336
337
338 async def test_io_manager_statistics() -> None:
339 def check(*, expected_readers: int, expected_writers: int) -> None:
340 statistics = _core.current_statistics()
341 print(statistics)
342 iostats = statistics.io_statistics
343 if iostats.backend == "epoll" or iostats.backend == "windows":
344 assert iostats.tasks_waiting_read == expected_readers
345 assert iostats.tasks_waiting_write == expected_writers
346 else:
347 assert iostats.backend == "kqueue"
348 assert iostats.monitors == 0
349 assert iostats.tasks_waiting == expected_readers + expected_writers
350
351 a1, b1 = stdlib_socket.socketpair()
352 a2, b2 = stdlib_socket.socketpair()
353 a3, b3 = stdlib_socket.socketpair()
354 for sock in [a1, b1, a2, b2, a3, b3]:
355 sock.setblocking(False)
356 with a1, b1, a2, b2, a3, b3:
357 # let the call_soon_task settle down
358 await wait_all_tasks_blocked()
359
360 # 1 for call_soon_task
361 check(expected_readers=1, expected_writers=0)
362
363 # We want:
364 # - one socket with a writer blocked
365 # - two sockets with a reader blocked
366 # - a socket with both blocked
367 fill_socket(a1)
368 fill_socket(a3)
369 async with _core.open_nursery() as nursery:
370 nursery.start_soon(_core.wait_writable, a1)
371 nursery.start_soon(_core.wait_readable, a2)
372 nursery.start_soon(_core.wait_readable, b2)
373 nursery.start_soon(_core.wait_writable, a3)
374 nursery.start_soon(_core.wait_readable, a3)
375
376 await wait_all_tasks_blocked()
377
378 # +1 for call_soon_task
379 check(expected_readers=3 + 1, expected_writers=2)
380
381 nursery.cancel_scope.cancel()
382
383 # 1 for call_soon_task
384 check(expected_readers=1, expected_writers=0)
385
386
387 @pytest.mark.filterwarnings("ignore:.*UnboundedQueue:trio.TrioDeprecationWarning")
388 async def test_io_manager_kqueue_monitors_statistics() -> None:
389 def check(
390 *,
391 expected_monitors: int,
392 expected_readers: int,
393 expected_writers: int,
394 ) -> None:
395 statistics = _core.current_statistics()
396 print(statistics)
397 iostats = statistics.io_statistics
398 assert iostats.backend == "kqueue"
399 assert iostats.monitors == expected_monitors
400 assert iostats.tasks_waiting == expected_readers + expected_writers
401
402 a1, b1 = stdlib_socket.socketpair()
403 for sock in [a1, b1]:
404 sock.setblocking(False)
405
406 with a1, b1:
407 # let the call_soon_task settle down
408 await wait_all_tasks_blocked()
409
410 if sys.platform != "win32" and sys.platform != "linux":
411 # 1 for call_soon_task
412 check(expected_monitors=0, expected_readers=1, expected_writers=0)
413
414 with _core.monitor_kevent(a1.fileno(), select.KQ_FILTER_READ):
415 with (
416 pytest.raises(_core.BusyResourceError),
417 _core.monitor_kevent(a1.fileno(), select.KQ_FILTER_READ),
418 ):
419 pass # pragma: no cover
420 check(expected_monitors=1, expected_readers=1, expected_writers=0)
421
422 check(expected_monitors=0, expected_readers=1, expected_writers=0)
423
424
425 async def test_can_survive_unnotified_close() -> None:
426 # An "unnotified" close is when the user closes an fd/socket/handle
427 # directly, without calling notify_closing first. This should never happen
428 # -- users should call notify_closing before closing things. But, just in
429 # case they don't, we would still like to avoid exploding.
430 #
431 # Acceptable behaviors:
432 # - wait_* never return, but can be cancelled cleanly
433 # - wait_* exit cleanly
434 # - wait_* raise an OSError
435 #
436 # Not acceptable:
437 # - getting stuck in an uncancellable state
438 # - TrioInternalError blowing up the whole run
439 #
440 # This test exercises some tricky "unnotified close" scenarios, to make
441 # sure we get the "acceptable" behaviors.
442
443 async def allow_OSError(
444 async_func: Callable[ArgsT, Awaitable[object]],
445 *args: ArgsT.args,
446 **kwargs: ArgsT.kwargs,
447 ) -> None:
448 with suppress(OSError):
449 await async_func(*args, **kwargs)
450
451 with stdlib_socket.socket() as s:
452 async with trio.open_nursery() as nursery:
453 nursery.start_soon(allow_OSError, trio.lowlevel.wait_readable, s)
454 await wait_all_tasks_blocked()
455 s.close()
456 await wait_all_tasks_blocked()
457 nursery.cancel_scope.cancel()
458
459 # We hit different paths on Windows depending on whether we close the last
460 # handle to the object (which produces a LOCAL_CLOSE notification and
461 # wakes up wait_readable), or only close one of the handles (which leaves
462 # wait_readable pending until cancelled).
463 with stdlib_socket.socket() as s, s.dup() as s2: # noqa: F841
464 async with trio.open_nursery() as nursery:
465 nursery.start_soon(allow_OSError, trio.lowlevel.wait_readable, s)
466 await wait_all_tasks_blocked()
467 s.close()
468 await wait_all_tasks_blocked()
469 nursery.cancel_scope.cancel()
470
471 # A more elaborate case, with two tasks waiting. On windows and epoll,
472 # the two tasks get muxed together onto a single underlying wait
473 # operation. So when they're cancelled, there's a brief moment where one
474 # of the tasks is cancelled but the other isn't, so we try to re-issue the
475 # underlying wait operation. But here, the handle we were going to use to
476 # do that has been pulled out from under our feet... so test that we can
477 # survive this.
478 a, b = stdlib_socket.socketpair()
479 with a, b, a.dup() as a2:
480 a.setblocking(False)
481 b.setblocking(False)
482 fill_socket(a)
483 async with trio.open_nursery() as nursery:
484 nursery.start_soon(allow_OSError, trio.lowlevel.wait_readable, a)
485 nursery.start_soon(allow_OSError, trio.lowlevel.wait_writable, a)
486 await wait_all_tasks_blocked()
487 a.close()
488 nursery.cancel_scope.cancel()
489
490 # A similar case, but now the single-task-wakeup happens due to I/O
491 # arriving, not a cancellation, so the operation gets re-issued from
492 # handle_io context rather than abort context.
493 a, b = stdlib_socket.socketpair()
494 with a, b, a.dup() as a2:
495 print(f"a={a.fileno()}, b={b.fileno()}, a2={a2.fileno()}")
496 a.setblocking(False)
497 b.setblocking(False)
498 fill_socket(a)
499 e = trio.Event()
500
501 # We want to wait for the kernel to process the wakeup on 'a', if any.
502 # But depending on the platform, we might not get a wakeup on 'a'. So
503 # we put one task to sleep waiting on 'a', and we put a second task to
504 # sleep waiting on 'a2', with the idea that the 'a2' notification will
505 # definitely arrive, and when it does then we can assume that whatever
506 # notification was going to arrive for 'a' has also arrived.
507 async def wait_readable_a2_then_set() -> None:
508 await trio.lowlevel.wait_readable(a2)
509 e.set()
510
511 async with trio.open_nursery() as nursery:
512 nursery.start_soon(allow_OSError, trio.lowlevel.wait_readable, a)
513 nursery.start_soon(allow_OSError, trio.lowlevel.wait_writable, a)
514 nursery.start_soon(wait_readable_a2_then_set)
515 await wait_all_tasks_blocked()
516 a.close()
517 b.send(b"x")
518 # Make sure that the wakeup has been received and everything has
519 # settled before cancelling the wait_writable.
520 await e.wait()
521 await wait_all_tasks_blocked()
522 nursery.cancel_scope.cancel()