Coverage for Lib/asyncio/proactor_events.py: 74%
600 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 05:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 05:46 +0000
1"""Event loop using a proactor and related classes.
3A proactor is a "notify-on-completion" multiplexer. Currently a
4proactor is only implemented on Windows with IOCP.
5"""
7__all__ = 'BaseProactorEventLoop',
9import io
10import os
11import socket
12import warnings
13import signal
14import threading
15import collections
17from . import base_events
18from . import constants
19from . import futures
20from . import exceptions
21from . import protocols
22from . import sslproto
23from . import transports
24from . import trsock
25from .log import logger
28def _set_socket_extra(transport, sock):
29 transport._extra['socket'] = trsock.TransportSocket(sock)
31 try:
32 transport._extra['sockname'] = sock.getsockname()
33 except socket.error:
34 if transport._loop.get_debug():
35 logger.warning(
36 "getsockname() failed on %r", sock, exc_info=True)
38 if 'peername' not in transport._extra: 38 ↛ exitline 38 didn't return from function '_set_socket_extra' because the condition on line 38 was always true
39 try:
40 transport._extra['peername'] = sock.getpeername()
41 except socket.error:
42 # UDP sockets may not have a peer name
43 transport._extra['peername'] = None
46class _ProactorBasePipeTransport(transports._FlowControlMixin,
47 transports.BaseTransport):
48 """Base class for pipe and socket transports."""
50 def __init__(self, loop, sock, protocol, waiter=None,
51 extra=None, server=None):
52 super().__init__(extra, loop)
53 self._set_extra(sock)
54 self._sock = sock
55 self.set_protocol(protocol)
56 self._server = server
57 self._buffer = None # None or bytearray.
58 self._read_fut = None
59 self._write_fut = None
60 self._pending_write = 0
61 self._conn_lost = 0
62 self._closing = False # Set when close() called.
63 self._called_connection_lost = False
64 self._eof_written = False
65 self._empty_waiter = None
66 if self._server is not None:
67 self._server._attach(self)
68 self._loop.call_soon(self._protocol.connection_made, self)
69 if waiter is not None:
70 # only wake up the waiter when connection_made() has been called
71 self._loop.call_soon(futures._set_result_unless_cancelled,
72 waiter, None)
74 def __repr__(self):
75 info = [self.__class__.__name__]
76 if self._sock is None: 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true
77 info.append('closed')
78 elif self._closing: 78 ↛ 79line 78 didn't jump to line 79 because the condition on line 78 was never true
79 info.append('closing')
80 if self._sock is not None: 80 ↛ 82line 80 didn't jump to line 82 because the condition on line 80 was always true
81 info.append(f'fd={self._sock.fileno()}')
82 if self._read_fut is not None: 82 ↛ 83line 82 didn't jump to line 83 because the condition on line 82 was never true
83 info.append(f'read={self._read_fut!r}')
84 if self._write_fut is not None: 84 ↛ 85line 84 didn't jump to line 85 because the condition on line 84 was never true
85 info.append(f'write={self._write_fut!r}')
86 if self._buffer: 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true
87 info.append(f'write_bufsize={len(self._buffer)}')
88 if self._eof_written: 88 ↛ 89line 88 didn't jump to line 89 because the condition on line 88 was never true
89 info.append('EOF written')
90 return '<{}>'.format(' '.join(info))
92 def _set_extra(self, sock):
93 self._extra['pipe'] = sock
95 def set_protocol(self, protocol):
96 self._protocol = protocol
98 def get_protocol(self):
99 return self._protocol
101 def is_closing(self):
102 return self._closing
104 def close(self):
105 if self._closing:
106 return
107 self._closing = True
108 if not self._buffer and self._write_fut is None:
109 # Nothing left to flush: no more data will be sent.
110 self._conn_lost += 1
111 self._loop.call_soon(self._call_connection_lost, None)
112 if self._read_fut is not None:
113 self._read_fut.cancel()
114 self._read_fut = None
116 def __del__(self, _warn=warnings.warn):
117 if self._sock is not None: 117 ↛ 118line 117 didn't jump to line 118 because the condition on line 117 was never true
118 _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
119 self._sock.close()
121 def _fatal_error(self, exc, message='Fatal error on pipe transport'):
122 try:
123 if isinstance(exc, OSError):
124 if self._loop.get_debug(): 124 ↛ 125line 124 didn't jump to line 125 because the condition on line 124 was never true
125 logger.debug("%r: %s", self, message, exc_info=True)
126 else:
127 self._loop.call_exception_handler({
128 'message': message,
129 'exception': exc,
130 'transport': self,
131 'protocol': self._protocol,
132 })
133 finally:
134 self._force_close(exc)
136 def _force_close(self, exc):
137 if self._empty_waiter is not None and not self._empty_waiter.done(): 137 ↛ 138line 137 didn't jump to line 138 because the condition on line 137 was never true
138 if exc is None:
139 self._empty_waiter.set_result(None)
140 else:
141 self._empty_waiter.set_exception(exc)
142 if self._closing and self._called_connection_lost: 142 ↛ 143line 142 didn't jump to line 143 because the condition on line 142 was never true
143 return
144 self._closing = True
145 self._conn_lost += 1
146 if self._write_fut:
147 self._write_fut.cancel()
148 self._write_fut = None
149 if self._read_fut:
150 self._read_fut.cancel()
151 self._read_fut = None
152 self._pending_write = 0
153 self._buffer = None
154 self._loop.call_soon(self._call_connection_lost, exc)
156 def _call_connection_lost(self, exc):
157 if self._called_connection_lost:
158 return
159 try:
160 self._protocol.connection_lost(exc)
161 finally:
162 # XXX If there is a pending overlapped read on the other
163 # end then it may fail with ERROR_NETNAME_DELETED if we
164 # just close our end. First calling shutdown() seems to
165 # cure it, but maybe using DisconnectEx() would be better.
166 if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
167 try:
168 self._sock.shutdown(socket.SHUT_RDWR)
169 except ConnectionError:
170 # The connection is already gone.
171 # The peer has already reset the connection.
172 pass
173 self._sock.close()
174 self._sock = None
175 server = self._server
176 if server is not None:
177 server._detach(self)
178 self._server = None
179 self._called_connection_lost = True
181 def get_write_buffer_size(self):
182 size = self._pending_write
183 if self._buffer is not None:
184 size += len(self._buffer)
185 return size
188class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
189 transports.ReadTransport):
190 """Transport for read pipes."""
192 def __init__(self, loop, sock, protocol, waiter=None,
193 extra=None, server=None, buffer_size=65536):
194 self._pending_data_length = -1
195 self._paused = True
196 super().__init__(loop, sock, protocol, waiter, extra, server)
198 self._data = bytearray(buffer_size)
199 self._loop.call_soon(self._loop_reading)
200 self._paused = False
202 def is_reading(self):
203 return not self._paused and not self._closing
205 def pause_reading(self):
206 if self._closing or self._paused:
207 return
208 self._paused = True
210 # bpo-33694: Don't cancel self._read_fut because cancelling an
211 # overlapped WSASend() loss silently data with the current proactor
212 # implementation.
213 #
214 # If CancelIoEx() fails with ERROR_NOT_FOUND, it means that WSASend()
215 # completed (even if HasOverlappedIoCompleted() returns 0), but
216 # Overlapped.cancel() currently silently ignores the ERROR_NOT_FOUND
217 # error. Once the overlapped is ignored, the IOCP loop will ignores the
218 # completion I/O event and so not read the result of the overlapped
219 # WSARecv().
221 if self._loop.get_debug(): 221 ↛ 222line 221 didn't jump to line 222 because the condition on line 221 was never true
222 logger.debug("%r pauses reading", self)
224 def resume_reading(self):
225 if self._closing or not self._paused:
226 return
228 self._paused = False
229 if self._read_fut is None:
230 self._loop.call_soon(self._loop_reading, None)
232 length = self._pending_data_length
233 self._pending_data_length = -1
234 if length > -1:
235 # Call the protocol method after calling _loop_reading(),
236 # since the protocol can decide to pause reading again.
237 self._loop.call_soon(self._data_received, self._data[:length], length)
239 if self._loop.get_debug(): 239 ↛ 240line 239 didn't jump to line 240 because the condition on line 239 was never true
240 logger.debug("%r resumes reading", self)
242 def _eof_received(self):
243 if self._loop.get_debug(): 243 ↛ 244line 243 didn't jump to line 244 because the condition on line 243 was never true
244 logger.debug("%r received EOF", self)
246 try:
247 keep_open = self._protocol.eof_received()
248 except (SystemExit, KeyboardInterrupt):
249 raise
250 except BaseException as exc:
251 self._fatal_error(
252 exc, 'Fatal error: protocol.eof_received() call failed.')
253 return
255 if not keep_open: 255 ↛ exitline 255 didn't return from function '_eof_received' because the condition on line 255 was always true
256 self.close()
258 def _data_received(self, data, length):
259 if self._paused:
260 # Don't call any protocol method while reading is paused.
261 # The protocol will be called on resume_reading().
262 assert self._pending_data_length == -1
263 self._pending_data_length = length
264 return
266 if length == 0:
267 self._eof_received()
268 return
270 if isinstance(self._protocol, protocols.BufferedProtocol): 270 ↛ 271line 270 didn't jump to line 271 because the condition on line 270 was never true
271 try:
272 protocols._feed_data_to_buffered_proto(self._protocol, data)
273 except (SystemExit, KeyboardInterrupt):
274 raise
275 except BaseException as exc:
276 self._fatal_error(exc,
277 'Fatal error: protocol.buffer_updated() '
278 'call failed.')
279 return
280 else:
281 self._protocol.data_received(data)
283 def _loop_reading(self, fut=None):
284 length = -1
285 data = None
286 try:
287 if fut is not None:
288 assert self._read_fut is fut or (self._read_fut is None and
289 self._closing)
290 self._read_fut = None
291 if fut.done(): 291 ↛ 302line 291 didn't jump to line 302 because the condition on line 291 was always true
292 # deliver data later in "finally" clause
293 length = fut.result()
294 if length == 0:
295 # we got end-of-file so no need to reschedule a new read
296 return
298 # It's a new slice so make it immutable so protocols upstream don't have problems
299 data = bytes(memoryview(self._data)[:length])
300 else:
301 # the future will be replaced by next proactor.recv call
302 fut.cancel()
304 if self._closing:
305 # since close() has been called we ignore any read data
306 return
308 # bpo-33694: buffer_updated() has currently no fast path because of
309 # a data loss issue caused by overlapped WSASend() cancellation.
311 if not self._paused:
312 # reschedule a new read
313 self._read_fut = self._loop._proactor.recv_into(self._sock, self._data)
314 except ConnectionAbortedError as exc:
315 if not self._closing: 315 ↛ 317line 315 didn't jump to line 317 because the condition on line 315 was always true
316 self._fatal_error(exc, 'Fatal read error on pipe transport')
317 elif self._loop.get_debug():
318 logger.debug("Read error on pipe transport while closing",
319 exc_info=True)
320 except ConnectionResetError as exc:
321 self._force_close(exc)
322 except OSError as exc:
323 self._fatal_error(exc, 'Fatal read error on pipe transport')
324 except exceptions.CancelledError:
325 if not self._closing:
326 raise
327 else:
328 if not self._paused:
329 self._read_fut.add_done_callback(self._loop_reading)
330 finally:
331 if length > -1:
332 self._data_received(data, length)
335class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
336 transports.WriteTransport):
337 """Transport for write pipes."""
339 _start_tls_compatible = True
341 def write(self, data):
342 if not isinstance(data, (bytes, bytearray, memoryview)): 342 ↛ 343line 342 didn't jump to line 343 because the condition on line 342 was never true
343 raise TypeError(
344 f"data argument must be a bytes-like object, "
345 f"not {type(data).__name__}")
346 if self._eof_written: 346 ↛ 347line 346 didn't jump to line 347 because the condition on line 346 was never true
347 raise RuntimeError('write_eof() already called')
348 if self._empty_waiter is not None: 348 ↛ 349line 348 didn't jump to line 349 because the condition on line 348 was never true
349 raise RuntimeError('unable to write; sendfile is in progress')
351 if not data:
352 return
354 if self._conn_lost:
355 if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
356 logger.warning('socket.send() raised exception.')
357 self._conn_lost += 1
358 return
360 # Observable states:
361 # 1. IDLE: _write_fut and _buffer both None
362 # 2. WRITING: _write_fut set; _buffer None
363 # 3. BACKED UP: _write_fut set; _buffer a bytearray
364 # We always copy the data, so the caller can't modify it
365 # while we're still waiting for the I/O to happen.
366 if self._write_fut is None: # IDLE -> WRITING
367 assert self._buffer is None
368 # Pass a copy, except if it's already immutable.
369 self._loop_writing(data=bytes(data))
370 elif not self._buffer: # WRITING -> BACKED UP
371 # Make a mutable copy which we can extend.
372 self._buffer = bytearray(data)
373 self._maybe_pause_protocol()
374 else: # BACKED UP
375 # Append to buffer (also copies).
376 self._buffer.extend(data)
377 self._maybe_pause_protocol()
379 def _loop_writing(self, f=None, data=None):
380 try:
381 if f is not None and self._write_fut is None and self._closing:
382 # XXX most likely self._force_close() has been called, and
383 # it has set self._write_fut to None.
384 return
385 assert f is self._write_fut
386 self._write_fut = None
387 self._pending_write = 0
388 if f:
389 f.result()
390 if data is None:
391 data = self._buffer
392 self._buffer = None
393 if not data:
394 if self._closing:
395 self._conn_lost += 1
396 self._loop.call_soon(self._call_connection_lost, None)
397 if self._eof_written:
398 self._sock.shutdown(socket.SHUT_WR)
399 # Now that we've reduced the buffer size, tell the
400 # protocol to resume writing if it was paused. Note that
401 # we do this last since the callback is called immediately
402 # and it may add more data to the buffer (even causing the
403 # protocol to be paused again).
404 self._maybe_resume_protocol()
405 else:
406 self._write_fut = self._loop._proactor.send(self._sock, data)
407 if not self._write_fut.done():
408 assert self._pending_write == 0
409 self._pending_write = len(data)
410 self._write_fut.add_done_callback(self._loop_writing)
411 self._maybe_pause_protocol()
412 else:
413 self._write_fut.add_done_callback(self._loop_writing)
414 if self._empty_waiter is not None and self._write_fut is None: 414 ↛ 415line 414 didn't jump to line 415 because the condition on line 414 was never true
415 self._empty_waiter.set_result(None)
416 except ConnectionResetError as exc:
417 self._force_close(exc)
418 except OSError as exc:
419 self._fatal_error(exc, 'Fatal write error on pipe transport')
421 def can_write_eof(self):
422 return True
424 def write_eof(self):
425 self.close()
427 def abort(self):
428 self._force_close(None)
430 def _make_empty_waiter(self):
431 if self._empty_waiter is not None:
432 raise RuntimeError("Empty waiter is already set")
433 self._empty_waiter = self._loop.create_future()
434 if self._write_fut is None:
435 self._empty_waiter.set_result(None)
436 return self._empty_waiter
438 def _reset_empty_waiter(self):
439 self._empty_waiter = None
442class _ProactorWritePipeTransport(_ProactorBaseWritePipeTransport):
443 def __init__(self, *args, **kw):
444 super().__init__(*args, **kw)
445 self._read_fut = self._loop._proactor.recv(self._sock, 16)
446 self._read_fut.add_done_callback(self._pipe_closed)
448 def _pipe_closed(self, fut):
449 if fut.cancelled():
450 # the transport has been closed
451 return
452 assert fut.result() == b''
453 if self._closing:
454 assert self._read_fut is None
455 return
456 assert fut is self._read_fut, (fut, self._read_fut)
457 self._read_fut = None
458 if self._write_fut is not None:
459 self._force_close(BrokenPipeError())
460 else:
461 self.close()
464class _ProactorDatagramTransport(_ProactorBasePipeTransport,
465 transports.DatagramTransport):
466 max_size = 256 * 1024
467 _header_size = 8
469 def __init__(self, loop, sock, protocol, address=None,
470 waiter=None, extra=None):
471 self._address = address
472 self._buffer_size = 0
473 # We don't need to call _protocol.connection_made() since our base
474 # constructor does it for us.
475 super().__init__(loop, sock, protocol, waiter=waiter, extra=extra)
477 # The base constructor sets _buffer = None, so we set it here
478 self._buffer = collections.deque()
479 self._loop.call_soon(self._loop_reading)
481 def _set_extra(self, sock):
482 _set_socket_extra(self, sock)
484 def get_write_buffer_size(self):
485 return self._buffer_size
487 def abort(self):
488 self._force_close(None)
490 def _force_close(self, exc):
491 # The base class drops the buffer; the size is tracked separately.
492 self._buffer_size = 0
493 super()._force_close(exc)
495 def sendto(self, data, addr=None):
496 if not isinstance(data, (bytes, bytearray, memoryview)):
497 raise TypeError(f'data argument must be a bytes-like object, '
498 f'not {type(data).__name__!r}')
500 if self._address is not None and addr not in (None, self._address):
501 raise ValueError(
502 f'Invalid address: must be None or {self._address}')
504 if self._conn_lost and self._address:
505 if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
506 logger.warning('socket.sendto() raised exception.')
507 self._conn_lost += 1
508 return
510 # Ensure that what we buffer is immutable.
511 self._buffer.append((bytes(data), addr))
512 self._buffer_size += len(data) + self._header_size
514 if self._write_fut is None:
515 # No current write operations are active, kick one off
516 self._loop_writing()
517 # else: A write operation is already kicked off
519 self._maybe_pause_protocol()
521 def _loop_writing(self, fut=None):
522 try:
523 if self._conn_lost: 523 ↛ 526line 523 didn't jump to line 526 because the condition on line 523 was never true
524 # No more data will be sent: either everything buffered has
525 # already been flushed, or _force_close() dropped it.
526 return
528 assert fut is self._write_fut
529 self._write_fut = None
530 if fut: 530 ↛ 532line 530 didn't jump to line 532 because the condition on line 530 was never true
531 # We are in a _loop_writing() done callback, get the result
532 fut.result()
534 if not self._buffer:
535 # Everything buffered has been sent
536 if self._closing: 536 ↛ 539line 536 didn't jump to line 539 because the condition on line 536 was always true
537 self._conn_lost += 1
538 self._loop.call_soon(self._call_connection_lost, None)
539 return
541 data, addr = self._buffer.popleft()
542 self._buffer_size -= len(data) + self._header_size
543 if self._address is not None:
544 self._write_fut = self._loop._proactor.send(self._sock,
545 data)
546 else:
547 self._write_fut = self._loop._proactor.sendto(self._sock,
548 data,
549 addr=addr)
550 except OSError as exc:
551 self._protocol.error_received(exc)
552 # error_received() is arbitrary protocol code: it may have sent
553 # (scheduling a write of its own, directly or via call_soon()),
554 # closed, or aborted the transport.
555 if self._buffer or self._closing: 555 ↛ 560line 555 didn't jump to line 560 because the condition on line 555 was never true
556 # Either data is still queued, or a close() is waiting on
557 # the write loop to drain it and call connection_lost().
558 # This write failed, so there is no completion callback
559 # pending to re-enter the loop -- schedule one (gh-156698).
560 def write_next():
561 # error_received() may have scheduled a write of its own,
562 # directly or with call_soon(); its completion callback
563 # will drain the rest of the buffer.
564 if self._write_fut is None:
565 self._loop_writing()
567 self._loop.call_soon(write_next)
568 else:
569 # Nothing left to write, so a paused protocol has to be
570 # resumed here: the next entry into _loop_writing() returns
571 # early on an empty buffer without doing it.
572 self._maybe_resume_protocol()
573 except Exception as exc:
574 self._fatal_error(exc, 'Fatal write error on datagram transport')
575 else:
576 self._write_fut.add_done_callback(self._loop_writing)
577 self._maybe_resume_protocol()
579 def _loop_reading(self, fut=None):
580 data = None
581 try:
582 if self._closing:
583 return
585 assert self._read_fut is fut
587 self._read_fut = None
588 if fut is not None:
589 res = fut.result()
591 if self._address is not None: 591 ↛ 592line 591 didn't jump to line 592 because the condition on line 591 was never true
592 data, addr = res, self._address
593 else:
594 data, addr = res
596 if self._address is not None: 596 ↛ 597line 596 didn't jump to line 597 because the condition on line 596 was never true
597 self._read_fut = self._loop._proactor.recv(self._sock,
598 self.max_size)
599 else:
600 self._read_fut = self._loop._proactor.recvfrom(self._sock,
601 self.max_size)
602 except ConnectionResetError as exc:
603 # WSARecvFrom() reports a stale ICMP port unreachable
604 # notification as a synchronous ConnectionResetError when the
605 # same socket was used to send to an address that is not
606 # listening. This is transient, so reschedule the read loop
607 # instead of leaving it dead.
608 self._protocol.error_received(exc)
609 if not self._closing:
610 self._loop.call_soon(self._loop_reading)
611 except OSError as exc:
612 self._protocol.error_received(exc)
613 except exceptions.CancelledError:
614 if not self._closing:
615 raise
616 else:
617 if self._read_fut is not None: 617 ↛ 620line 617 didn't jump to line 620 because the condition on line 617 was always true
618 self._read_fut.add_done_callback(self._loop_reading)
619 finally:
620 if data:
621 self._protocol.datagram_received(data, addr)
624class _ProactorDuplexPipeTransport(_ProactorReadPipeTransport,
625 _ProactorBaseWritePipeTransport,
626 transports.Transport):
627 """Transport for duplex pipes."""
629 def can_write_eof(self):
630 return False
632 def write_eof(self):
633 raise NotImplementedError
636class _ProactorSocketTransport(_ProactorReadPipeTransport,
637 _ProactorBaseWritePipeTransport,
638 transports.Transport):
639 """Transport for connected sockets."""
641 _sendfile_compatible = constants._SendfileMode.TRY_NATIVE
643 def __init__(self, loop, sock, protocol, waiter=None,
644 extra=None, server=None):
645 super().__init__(loop, sock, protocol, waiter, extra, server)
646 base_events._set_nodelay(sock)
648 def _set_extra(self, sock):
649 _set_socket_extra(self, sock)
651 def can_write_eof(self):
652 return True
654 def write_eof(self):
655 if self._closing or self._eof_written:
656 return
657 self._eof_written = True
658 if self._write_fut is None:
659 self._sock.shutdown(socket.SHUT_WR)
662class BaseProactorEventLoop(base_events.BaseEventLoop):
664 def __init__(self, proactor):
665 super().__init__()
666 logger.debug('Using proactor: %s', proactor.__class__.__name__)
667 self._proactor = proactor
668 self._selector = proactor # convenient alias
669 self._self_reading_future = None
670 self._accept_futures = {} # socket file descriptor => Future
671 proactor.set_loop(self)
672 self._make_self_pipe()
673 if threading.current_thread() is threading.main_thread(): 673 ↛ exitline 673 didn't return from function '__init__' because the condition on line 673 was always true
674 # wakeup fd can only be installed to a file descriptor from the main thread
675 signal.set_wakeup_fd(self._csock.fileno())
677 def _make_socket_transport(self, sock, protocol, waiter=None,
678 extra=None, server=None, context=None):
679 return _ProactorSocketTransport(self, sock, protocol, waiter,
680 extra, server)
682 def _make_ssl_transport(
683 self, rawsock, protocol, sslcontext, waiter=None,
684 *, server_side=False, server_hostname=None,
685 extra=None, server=None,
686 ssl_handshake_timeout=None,
687 ssl_shutdown_timeout=None, context=None):
688 ssl_protocol = sslproto.SSLProtocol(
689 self, protocol, sslcontext, waiter,
690 server_side, server_hostname,
691 ssl_handshake_timeout=ssl_handshake_timeout,
692 ssl_shutdown_timeout=ssl_shutdown_timeout)
693 _ProactorSocketTransport(self, rawsock, ssl_protocol,
694 extra=extra, server=server)
695 return ssl_protocol._app_transport
697 def _make_datagram_transport(self, sock, protocol,
698 address=None, waiter=None, extra=None):
699 return _ProactorDatagramTransport(self, sock, protocol, address,
700 waiter, extra)
702 def _make_duplex_pipe_transport(self, sock, protocol, waiter=None,
703 extra=None):
704 return _ProactorDuplexPipeTransport(self,
705 sock, protocol, waiter, extra)
707 def _make_read_pipe_transport(self, sock, protocol, waiter=None,
708 extra=None):
709 return _ProactorReadPipeTransport(self, sock, protocol, waiter, extra)
711 def _make_write_pipe_transport(self, sock, protocol, waiter=None,
712 extra=None):
713 # We want connection_lost() to be called when other end closes
714 return _ProactorWritePipeTransport(self,
715 sock, protocol, waiter, extra)
717 def close(self):
718 if self.is_running(): 718 ↛ 719line 718 didn't jump to line 719 because the condition on line 718 was never true
719 raise RuntimeError("Cannot close a running event loop")
720 if self.is_closed():
721 return
723 if threading.current_thread() is threading.main_thread(): 723 ↛ 728line 723 didn't jump to line 728 because the condition on line 723 was always true
724 signal.set_wakeup_fd(-1)
725 # Call these methods before closing the event loop (before calling
726 # BaseEventLoop.close), because they can schedule callbacks with
727 # call_soon(), which is forbidden when the event loop is closed.
728 self._stop_accept_futures()
729 self._close_self_pipe()
730 self._proactor.close()
731 self._proactor = None
732 self._selector = None
734 # Close the event loop
735 super().close()
737 async def sock_recv(self, sock, n):
738 return await self._proactor.recv(sock, n)
740 async def sock_recv_into(self, sock, buf):
741 return await self._proactor.recv_into(sock, buf)
743 async def sock_recvfrom(self, sock, bufsize):
744 return await self._proactor.recvfrom(sock, bufsize)
746 async def sock_recvfrom_into(self, sock, buf, nbytes=0):
747 if not nbytes:
748 nbytes = len(buf)
750 return await self._proactor.recvfrom_into(sock, buf, nbytes)
752 async def sock_sendall(self, sock, data):
753 return await self._proactor.send(sock, data)
755 async def sock_sendto(self, sock, data, address):
756 return await self._proactor.sendto(sock, data, 0, address)
758 async def sock_connect(self, sock, address):
759 if self._debug and sock.gettimeout() != 0:
760 raise ValueError("the socket must be non-blocking")
761 return await self._proactor.connect(sock, address)
763 async def sock_accept(self, sock):
764 return await self._proactor.accept(sock)
766 async def _sock_sendfile_native(self, sock, file, offset, count):
767 try:
768 fileno = file.fileno()
769 except (AttributeError, io.UnsupportedOperation):
770 raise exceptions.SendfileNotAvailableError("not a regular file")
771 try:
772 fsize = os.fstat(fileno).st_size
773 except OSError:
774 raise exceptions.SendfileNotAvailableError("not a regular file")
775 blocksize = count if count else fsize
776 if not blocksize:
777 return 0 # empty file
779 blocksize = min(blocksize, 0xffff_ffff)
780 end_pos = min(offset + count, fsize) if count else fsize
781 offset = min(offset, fsize)
782 total_sent = 0
783 try:
784 while True:
785 blocksize = min(end_pos - offset, blocksize)
786 if blocksize <= 0:
787 return total_sent
788 await self._proactor.sendfile(sock, file, offset, blocksize)
789 offset += blocksize
790 total_sent += blocksize
791 finally:
792 file.seek(offset)
794 async def _sendfile_native(self, transp, file, offset, count):
795 resume_reading = transp.is_reading()
796 transp.pause_reading()
797 try:
798 await transp._make_empty_waiter()
799 return await self.sock_sendfile(transp._sock, file, offset, count,
800 fallback=False)
801 finally:
802 transp._reset_empty_waiter()
803 if resume_reading:
804 transp.resume_reading()
806 def _close_self_pipe(self):
807 if self._self_reading_future is not None:
808 self._self_reading_future.cancel()
809 self._self_reading_future = None
810 self._ssock.close()
811 self._ssock = None
812 self._csock.close()
813 self._csock = None
814 self._internal_fds -= 1
816 def _make_self_pipe(self):
817 # A self-socket, really. :-)
818 self._ssock, self._csock = socket.socketpair()
819 self._ssock.setblocking(False)
820 self._csock.setblocking(False)
821 self._internal_fds += 1
823 def _loop_self_reading(self, f=None):
824 try:
825 if f is not None:
826 f.result() # may raise
827 if self._self_reading_future is not f: 827 ↛ 834line 827 didn't jump to line 834 because the condition on line 827 was never true
828 # When we scheduled this Future, we assigned it to
829 # _self_reading_future. If it's not there now, something has
830 # tried to cancel the loop while this callback was still in the
831 # queue (see windows_events.ProactorEventLoop.run_forever). In
832 # that case stop here instead of continuing to schedule a new
833 # iteration.
834 return
835 f = self._proactor.recv(self._ssock, 4096)
836 except exceptions.CancelledError:
837 # _close_self_pipe() has been called, stop waiting for data
838 return
839 except (SystemExit, KeyboardInterrupt):
840 raise
841 except BaseException as exc:
842 self.call_exception_handler({
843 'message': 'Error on reading from the event loop self pipe',
844 'exception': exc,
845 'loop': self,
846 })
847 else:
848 self._self_reading_future = f
849 f.add_done_callback(self._loop_self_reading)
851 def _write_to_self(self):
852 # This may be called from a different thread, possibly after
853 # _close_self_pipe() has been called or even while it is
854 # running. Guard for self._csock being None or closed. When
855 # a socket is closed, send() raises OSError (with errno set to
856 # EBADF, but let's not rely on the exact error code).
857 csock = self._csock
858 if csock is None: 858 ↛ 859line 858 didn't jump to line 859 because the condition on line 858 was never true
859 return
861 try:
862 csock.send(b'\0')
863 except OSError:
864 if self._debug:
865 logger.debug("Fail to write a null byte into the "
866 "self-pipe socket",
867 exc_info=True)
869 def _start_serving(self, protocol_factory, sock,
870 sslcontext=None, server=None, backlog=100,
871 ssl_handshake_timeout=None,
872 ssl_shutdown_timeout=None, context=None):
874 def loop(f=None):
875 try:
876 if f is not None:
877 conn, addr = f.result()
878 if self._debug: 878 ↛ 879line 878 didn't jump to line 879 because the condition on line 878 was never true
879 logger.debug("%r got a new connection from %r: %r",
880 server, addr, conn)
881 protocol = protocol_factory()
882 if server is not None and not server.is_serving(): 882 ↛ 885line 882 didn't jump to line 885 because the condition on line 882 was never true
883 # The server was closed after the connection was
884 # accepted.
885 conn.close()
886 return
887 if sslcontext is not None: 887 ↛ 888line 887 didn't jump to line 888 because the condition on line 887 was never true
888 self._make_ssl_transport(
889 conn, protocol, sslcontext, server_side=True,
890 extra={'peername': addr}, server=server,
891 ssl_handshake_timeout=ssl_handshake_timeout,
892 ssl_shutdown_timeout=ssl_shutdown_timeout)
893 else:
894 self._make_socket_transport(
895 conn, protocol,
896 extra={'peername': addr}, server=server)
897 if self.is_closed(): 897 ↛ 898line 897 didn't jump to line 898 because the condition on line 897 was never true
898 return
899 f = self._proactor.accept(sock)
900 except (ConnectionResetError, ConnectionAbortedError):
901 # The peer closed the connection before it was accepted.
902 # The listening socket is still usable: keep accepting.
903 if self._debug: 903 ↛ 904line 903 didn't jump to line 904 because the condition on line 903 was never true
904 logger.debug("Accept failed on socket %r",
905 sock, exc_info=True)
906 self.call_soon(loop)
907 except OSError as exc:
908 if sock.fileno() != -1: 908 ↛ 915line 908 didn't jump to line 915 because the condition on line 908 was always true
909 self.call_exception_handler({
910 'message': 'Accept failed on a socket',
911 'exception': exc,
912 'socket': trsock.TransportSocket(sock),
913 })
914 sock.close()
915 elif self._debug:
916 logger.debug("Accept failed on socket %r",
917 sock, exc_info=True)
918 except exceptions.CancelledError:
919 sock.close()
920 else:
921 self._accept_futures[sock.fileno()] = f
922 f.add_done_callback(loop)
924 self.call_soon(loop)
926 def _process_events(self, event_list):
927 # Events are processed in the IocpProactor._poll() method
928 pass
930 def _stop_accept_futures(self):
931 for future in self._accept_futures.values():
932 future.cancel()
933 self._accept_futures.clear()
935 def _stop_serving(self, sock):
936 future = self._accept_futures.pop(sock.fileno(), None)
937 if future: 937 ↛ 939line 937 didn't jump to line 939 because the condition on line 937 was always true
938 future.cancel()
939 self._proactor._stop_serving(sock)
940 sock.close()