2013-10-17 17:40:50 -03:00
|
|
|
"""Event loop using a proactor and related classes.
|
|
|
|
|
|
|
|
A proactor is a "notify-on-completion" multiplexer. Currently a
|
|
|
|
proactor is only implemented on Windows with IOCP.
|
|
|
|
"""
|
|
|
|
|
2017-12-10 19:36:12 -04:00
|
|
|
__all__ = 'BaseProactorEventLoop',
|
2014-01-25 10:32:06 -04:00
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
import io
|
|
|
|
import os
|
2013-10-17 17:40:50 -03:00
|
|
|
import socket
|
2015-01-29 12:50:58 -04:00
|
|
|
import warnings
|
2018-12-18 17:56:17 -04:00
|
|
|
import signal
|
2019-08-26 06:51:08 -03:00
|
|
|
import threading
|
2019-05-28 06:52:15 -03:00
|
|
|
import collections
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
from . import base_events
|
|
|
|
from . import constants
|
|
|
|
from . import futures
|
2018-09-11 14:13:04 -03:00
|
|
|
from . import exceptions
|
2018-01-28 17:30:26 -04:00
|
|
|
from . import protocols
|
2015-01-13 19:19:09 -04:00
|
|
|
from . import sslproto
|
2013-10-17 17:40:50 -03:00
|
|
|
from . import transports
|
2019-05-27 10:57:20 -03:00
|
|
|
from . import trsock
|
2013-10-17 19:39:45 -03:00
|
|
|
from .log import logger
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2019-05-28 06:52:15 -03:00
|
|
|
def _set_socket_extra(transport, sock):
|
|
|
|
transport._extra['socket'] = trsock.TransportSocket(sock)
|
|
|
|
|
|
|
|
try:
|
|
|
|
transport._extra['sockname'] = sock.getsockname()
|
|
|
|
except socket.error:
|
|
|
|
if transport._loop.get_debug():
|
|
|
|
logger.warning(
|
|
|
|
"getsockname() failed on %r", sock, exc_info=True)
|
|
|
|
|
|
|
|
if 'peername' not in transport._extra:
|
|
|
|
try:
|
|
|
|
transport._extra['peername'] = sock.getpeername()
|
|
|
|
except socket.error:
|
|
|
|
# UDP sockets may not have a peer name
|
|
|
|
transport._extra['peername'] = None
|
|
|
|
|
|
|
|
|
2014-02-18 19:41:13 -04:00
|
|
|
class _ProactorBasePipeTransport(transports._FlowControlMixin,
|
|
|
|
transports.BaseTransport):
|
2013-10-17 17:40:50 -03:00
|
|
|
"""Base class for pipe and socket transports."""
|
|
|
|
|
|
|
|
def __init__(self, loop, sock, protocol, waiter=None,
|
|
|
|
extra=None, server=None):
|
2014-11-05 10:27:41 -04:00
|
|
|
super().__init__(extra, loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self._set_extra(sock)
|
|
|
|
self._sock = sock
|
2018-05-28 15:31:28 -03:00
|
|
|
self.set_protocol(protocol)
|
2013-10-17 17:40:50 -03:00
|
|
|
self._server = server
|
2013-12-04 16:12:07 -04:00
|
|
|
self._buffer = None # None or bytearray.
|
2013-10-17 17:40:50 -03:00
|
|
|
self._read_fut = None
|
|
|
|
self._write_fut = None
|
2014-02-01 17:49:59 -04:00
|
|
|
self._pending_write = 0
|
2013-10-17 17:40:50 -03:00
|
|
|
self._conn_lost = 0
|
|
|
|
self._closing = False # Set when close() called.
|
2022-10-24 17:21:42 -03:00
|
|
|
self._called_connection_lost = False
|
2013-10-17 17:40:50 -03:00
|
|
|
self._eof_written = False
|
|
|
|
if self._server is not None:
|
2024-03-11 21:31:49 -03:00
|
|
|
self._server._attach()
|
2013-10-17 17:40:50 -03:00
|
|
|
self._loop.call_soon(self._protocol.connection_made, self)
|
|
|
|
if waiter is not None:
|
2015-01-28 19:36:35 -04:00
|
|
|
# only wake up the waiter when connection_made() has been called
|
2015-11-17 13:19:41 -04:00
|
|
|
self._loop.call_soon(futures._set_result_unless_cancelled,
|
|
|
|
waiter, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-07-11 22:11:53 -03:00
|
|
|
def __repr__(self):
|
2014-10-12 04:52:11 -03:00
|
|
|
info = [self.__class__.__name__]
|
2015-01-15 08:32:28 -04:00
|
|
|
if self._sock is None:
|
2014-10-12 04:52:11 -03:00
|
|
|
info.append('closed')
|
|
|
|
elif self._closing:
|
|
|
|
info.append('closing')
|
2015-01-15 08:32:28 -04:00
|
|
|
if self._sock is not None:
|
2017-12-10 19:36:12 -04:00
|
|
|
info.append(f'fd={self._sock.fileno()}')
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._read_fut is not None:
|
2017-12-10 19:36:12 -04:00
|
|
|
info.append(f'read={self._read_fut!r}')
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._write_fut is not None:
|
2017-12-10 19:36:12 -04:00
|
|
|
info.append(f'write={self._write_fut!r}')
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._buffer:
|
2017-12-10 19:36:12 -04:00
|
|
|
info.append(f'write_bufsize={len(self._buffer)}')
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._eof_written:
|
|
|
|
info.append('EOF written')
|
2017-12-10 19:36:12 -04:00
|
|
|
return '<{}>'.format(' '.join(info))
|
2014-07-11 22:11:53 -03:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def _set_extra(self, sock):
|
|
|
|
self._extra['pipe'] = sock
|
|
|
|
|
2016-09-11 22:11:02 -03:00
|
|
|
def set_protocol(self, protocol):
|
|
|
|
self._protocol = protocol
|
|
|
|
|
|
|
|
def get_protocol(self):
|
|
|
|
return self._protocol
|
|
|
|
|
2015-11-16 13:43:21 -04:00
|
|
|
def is_closing(self):
|
|
|
|
return self._closing
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def close(self):
|
|
|
|
if self._closing:
|
|
|
|
return
|
|
|
|
self._closing = True
|
|
|
|
self._conn_lost += 1
|
|
|
|
if not self._buffer and self._write_fut is None:
|
|
|
|
self._loop.call_soon(self._call_connection_lost, None)
|
|
|
|
if self._read_fut is not None:
|
|
|
|
self._read_fut.cancel()
|
2015-01-15 08:40:27 -04:00
|
|
|
self._read_fut = None
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-01-10 06:24:40 -04:00
|
|
|
def __del__(self, _warn=warnings.warn):
|
2017-04-24 22:57:18 -03:00
|
|
|
if self._sock is not None:
|
2019-01-10 06:24:40 -04:00
|
|
|
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
|
2022-05-17 19:27:32 -03:00
|
|
|
self._sock.close()
|
2015-01-29 12:50:58 -04:00
|
|
|
|
2014-02-18 20:40:41 -04:00
|
|
|
def _fatal_error(self, exc, message='Fatal error on pipe transport'):
|
2018-01-28 17:30:26 -04:00
|
|
|
try:
|
2019-05-27 10:28:34 -03:00
|
|
|
if isinstance(exc, OSError):
|
2018-01-28 17:30:26 -04:00
|
|
|
if self._loop.get_debug():
|
|
|
|
logger.debug("%r: %s", self, message, exc_info=True)
|
|
|
|
else:
|
|
|
|
self._loop.call_exception_handler({
|
|
|
|
'message': message,
|
|
|
|
'exception': exc,
|
|
|
|
'transport': self,
|
|
|
|
'protocol': self._protocol,
|
|
|
|
})
|
|
|
|
finally:
|
|
|
|
self._force_close(exc)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def _force_close(self, exc):
|
2019-01-07 21:46:59 -04:00
|
|
|
if self._empty_waiter is not None and not self._empty_waiter.done():
|
2018-02-25 12:32:14 -04:00
|
|
|
if exc is None:
|
|
|
|
self._empty_waiter.set_result(None)
|
|
|
|
else:
|
|
|
|
self._empty_waiter.set_exception(exc)
|
2022-10-24 17:21:42 -03:00
|
|
|
if self._closing and self._called_connection_lost:
|
2013-10-17 17:40:50 -03:00
|
|
|
return
|
|
|
|
self._closing = True
|
|
|
|
self._conn_lost += 1
|
|
|
|
if self._write_fut:
|
|
|
|
self._write_fut.cancel()
|
2015-01-15 08:40:27 -04:00
|
|
|
self._write_fut = None
|
2013-10-17 17:40:50 -03:00
|
|
|
if self._read_fut:
|
|
|
|
self._read_fut.cancel()
|
2015-01-15 08:40:27 -04:00
|
|
|
self._read_fut = None
|
2014-02-01 17:49:59 -04:00
|
|
|
self._pending_write = 0
|
2013-12-04 16:12:07 -04:00
|
|
|
self._buffer = None
|
2013-10-17 17:40:50 -03:00
|
|
|
self._loop.call_soon(self._call_connection_lost, exc)
|
|
|
|
|
|
|
|
def _call_connection_lost(self, exc):
|
2022-10-26 23:37:12 -03:00
|
|
|
if self._called_connection_lost:
|
|
|
|
return
|
2013-10-17 17:40:50 -03:00
|
|
|
try:
|
|
|
|
self._protocol.connection_lost(exc)
|
|
|
|
finally:
|
|
|
|
# XXX If there is a pending overlapped read on the other
|
|
|
|
# end then it may fail with ERROR_NETNAME_DELETED if we
|
|
|
|
# just close our end. First calling shutdown() seems to
|
|
|
|
# cure it, but maybe using DisconnectEx() would be better.
|
2022-03-15 11:59:02 -03:00
|
|
|
if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
|
2013-10-17 17:40:50 -03:00
|
|
|
self._sock.shutdown(socket.SHUT_RDWR)
|
|
|
|
self._sock.close()
|
2015-01-14 19:04:21 -04:00
|
|
|
self._sock = None
|
2013-10-17 17:40:50 -03:00
|
|
|
server = self._server
|
|
|
|
if server is not None:
|
2024-03-11 21:31:49 -03:00
|
|
|
server._detach()
|
2013-10-17 17:40:50 -03:00
|
|
|
self._server = None
|
2022-10-24 17:21:42 -03:00
|
|
|
self._called_connection_lost = True
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-12-04 16:12:07 -04:00
|
|
|
def get_write_buffer_size(self):
|
2014-02-01 17:49:59 -04:00
|
|
|
size = self._pending_write
|
|
|
|
if self._buffer is not None:
|
|
|
|
size += len(self._buffer)
|
|
|
|
return size
|
2013-12-04 16:12:07 -04:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
|
|
|
|
transports.ReadTransport):
|
|
|
|
"""Transport for read pipes."""
|
|
|
|
|
|
|
|
def __init__(self, loop, sock, protocol, waiter=None,
|
2020-07-14 16:41:24 -03:00
|
|
|
extra=None, server=None, buffer_size=65536):
|
|
|
|
self._pending_data_length = -1
|
2018-05-28 15:31:28 -03:00
|
|
|
self._paused = True
|
2013-10-17 17:40:50 -03:00
|
|
|
super().__init__(loop, sock, protocol, waiter, extra, server)
|
2018-05-28 15:31:28 -03:00
|
|
|
|
2020-07-14 16:41:24 -03:00
|
|
|
self._data = bytearray(buffer_size)
|
2018-05-28 15:31:28 -03:00
|
|
|
self._loop.call_soon(self._loop_reading)
|
|
|
|
self._paused = False
|
2018-01-28 17:30:26 -04:00
|
|
|
|
2017-12-18 18:03:23 -04:00
|
|
|
def is_reading(self):
|
|
|
|
return not self._paused and not self._closing
|
|
|
|
|
2013-10-18 11:58:20 -03:00
|
|
|
def pause_reading(self):
|
2017-12-18 18:03:23 -04:00
|
|
|
if self._closing or self._paused:
|
|
|
|
return
|
2013-10-17 17:40:50 -03:00
|
|
|
self._paused = True
|
2018-01-28 17:30:26 -04:00
|
|
|
|
2018-06-07 19:25:52 -03:00
|
|
|
# bpo-33694: Don't cancel self._read_fut because cancelling an
|
|
|
|
# overlapped WSASend() loss silently data with the current proactor
|
|
|
|
# implementation.
|
|
|
|
#
|
|
|
|
# If CancelIoEx() fails with ERROR_NOT_FOUND, it means that WSASend()
|
|
|
|
# completed (even if HasOverlappedIoCompleted() returns 0), but
|
|
|
|
# Overlapped.cancel() currently silently ignores the ERROR_NOT_FOUND
|
|
|
|
# error. Once the overlapped is ignored, the IOCP loop will ignores the
|
|
|
|
# completion I/O event and so not read the result of the overlapped
|
|
|
|
# WSARecv().
|
2018-01-28 17:30:26 -04:00
|
|
|
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._loop.get_debug():
|
|
|
|
logger.debug("%r pauses reading", self)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-10-18 11:58:20 -03:00
|
|
|
def resume_reading(self):
|
2017-12-18 18:03:23 -04:00
|
|
|
if self._closing or not self._paused:
|
2013-10-17 17:40:50 -03:00
|
|
|
return
|
2018-06-07 19:25:52 -03:00
|
|
|
|
2017-12-18 18:03:23 -04:00
|
|
|
self._paused = False
|
2018-06-07 19:25:52 -03:00
|
|
|
if self._read_fut is None:
|
|
|
|
self._loop.call_soon(self._loop_reading, None)
|
|
|
|
|
2020-07-14 16:41:24 -03:00
|
|
|
length = self._pending_data_length
|
|
|
|
self._pending_data_length = -1
|
|
|
|
if length > -1:
|
2021-06-12 23:47:44 -03:00
|
|
|
# Call the protocol method after calling _loop_reading(),
|
2018-06-07 19:25:52 -03:00
|
|
|
# since the protocol can decide to pause reading again.
|
2020-07-14 16:41:24 -03:00
|
|
|
self._loop.call_soon(self._data_received, self._data[:length], length)
|
2018-06-07 19:25:52 -03:00
|
|
|
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._loop.get_debug():
|
|
|
|
logger.debug("%r resumes reading", self)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2018-06-07 19:25:52 -03:00
|
|
|
def _eof_received(self):
|
2018-01-28 17:30:26 -04:00
|
|
|
if self._loop.get_debug():
|
|
|
|
logger.debug("%r received EOF", self)
|
|
|
|
|
|
|
|
try:
|
|
|
|
keep_open = self._protocol.eof_received()
|
2019-05-27 09:45:12 -03:00
|
|
|
except (SystemExit, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except BaseException as exc:
|
2018-01-28 17:30:26 -04:00
|
|
|
self._fatal_error(
|
|
|
|
exc, 'Fatal error: protocol.eof_received() call failed.')
|
|
|
|
return
|
|
|
|
|
|
|
|
if not keep_open:
|
|
|
|
self.close()
|
|
|
|
|
2020-07-14 16:41:24 -03:00
|
|
|
def _data_received(self, data, length):
|
2018-06-07 19:25:52 -03:00
|
|
|
if self._paused:
|
|
|
|
# Don't call any protocol method while reading is paused.
|
|
|
|
# The protocol will be called on resume_reading().
|
2020-07-14 16:41:24 -03:00
|
|
|
assert self._pending_data_length == -1
|
|
|
|
self._pending_data_length = length
|
2018-05-28 15:31:28 -03:00
|
|
|
return
|
|
|
|
|
2020-07-14 16:41:24 -03:00
|
|
|
if length == 0:
|
2018-06-07 19:25:52 -03:00
|
|
|
self._eof_received()
|
2013-10-17 17:40:50 -03:00
|
|
|
return
|
|
|
|
|
2018-06-07 19:25:52 -03:00
|
|
|
if isinstance(self._protocol, protocols.BufferedProtocol):
|
|
|
|
try:
|
2018-06-08 05:32:06 -03:00
|
|
|
protocols._feed_data_to_buffered_proto(self._protocol, data)
|
2019-05-27 09:45:12 -03:00
|
|
|
except (SystemExit, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except BaseException as exc:
|
2018-06-07 19:25:52 -03:00
|
|
|
self._fatal_error(exc,
|
|
|
|
'Fatal error: protocol.buffer_updated() '
|
|
|
|
'call failed.')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self._protocol.data_received(data)
|
|
|
|
|
|
|
|
def _loop_reading(self, fut=None):
|
2020-07-14 16:41:24 -03:00
|
|
|
length = -1
|
2018-01-28 17:30:26 -04:00
|
|
|
data = None
|
2013-10-17 17:40:50 -03:00
|
|
|
try:
|
|
|
|
if fut is not None:
|
|
|
|
assert self._read_fut is fut or (self._read_fut is None and
|
|
|
|
self._closing)
|
|
|
|
self._read_fut = None
|
2018-01-27 15:22:47 -04:00
|
|
|
if fut.done():
|
|
|
|
# deliver data later in "finally" clause
|
2020-07-14 16:41:24 -03:00
|
|
|
length = fut.result()
|
|
|
|
if length == 0:
|
|
|
|
# we got end-of-file so no need to reschedule a new read
|
|
|
|
return
|
|
|
|
|
2022-12-10 19:07:02 -04:00
|
|
|
# It's a new slice so make it immutable so protocols upstream don't have problems
|
|
|
|
data = bytes(memoryview(self._data)[:length])
|
2018-01-27 15:22:47 -04:00
|
|
|
else:
|
|
|
|
# the future will be replaced by next proactor.recv call
|
|
|
|
fut.cancel()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
if self._closing:
|
|
|
|
# since close() has been called we ignore any read data
|
|
|
|
return
|
|
|
|
|
2018-06-07 19:25:52 -03:00
|
|
|
# bpo-33694: buffer_updated() has currently no fast path because of
|
|
|
|
# a data loss issue caused by overlapped WSASend() cancellation.
|
|
|
|
|
|
|
|
if not self._paused:
|
|
|
|
# reschedule a new read
|
2020-07-14 16:41:24 -03:00
|
|
|
self._read_fut = self._loop._proactor.recv_into(self._sock, self._data)
|
2013-10-17 17:40:50 -03:00
|
|
|
except ConnectionAbortedError as exc:
|
|
|
|
if not self._closing:
|
2014-02-18 20:40:41 -04:00
|
|
|
self._fatal_error(exc, 'Fatal read error on pipe transport')
|
2014-08-25 18:20:52 -03:00
|
|
|
elif self._loop.get_debug():
|
|
|
|
logger.debug("Read error on pipe transport while closing",
|
|
|
|
exc_info=True)
|
2013-10-17 17:40:50 -03:00
|
|
|
except ConnectionResetError as exc:
|
|
|
|
self._force_close(exc)
|
|
|
|
except OSError as exc:
|
2014-02-18 20:40:41 -04:00
|
|
|
self._fatal_error(exc, 'Fatal read error on pipe transport')
|
2018-09-11 14:13:04 -03:00
|
|
|
except exceptions.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
if not self._closing:
|
|
|
|
raise
|
|
|
|
else:
|
2018-06-07 19:25:52 -03:00
|
|
|
if not self._paused:
|
|
|
|
self._read_fut.add_done_callback(self._loop_reading)
|
2013-10-17 17:40:50 -03:00
|
|
|
finally:
|
2020-07-14 16:41:24 -03:00
|
|
|
if length > -1:
|
|
|
|
self._data_received(data, length)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2014-01-31 09:18:18 -04:00
|
|
|
class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
|
2014-02-01 17:49:59 -04:00
|
|
|
transports.WriteTransport):
|
2013-10-17 17:40:50 -03:00
|
|
|
"""Transport for write pipes."""
|
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
_start_tls_compatible = True
|
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super().__init__(*args, **kw)
|
|
|
|
self._empty_waiter = None
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def write(self, data):
|
2013-12-04 16:12:07 -04:00
|
|
|
if not isinstance(data, (bytes, bytearray, memoryview)):
|
2017-12-10 19:36:12 -04:00
|
|
|
raise TypeError(
|
|
|
|
f"data argument must be a bytes-like object, "
|
|
|
|
f"not {type(data).__name__}")
|
2013-10-17 17:40:50 -03:00
|
|
|
if self._eof_written:
|
2013-12-04 16:12:07 -04:00
|
|
|
raise RuntimeError('write_eof() already called')
|
2018-02-25 12:32:14 -04:00
|
|
|
if self._empty_waiter is not None:
|
|
|
|
raise RuntimeError('unable to write; sendfile is in progress')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._conn_lost:
|
|
|
|
if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
|
2013-10-17 19:39:45 -03:00
|
|
|
logger.warning('socket.send() raised exception.')
|
2013-10-17 17:40:50 -03:00
|
|
|
self._conn_lost += 1
|
|
|
|
return
|
|
|
|
|
2013-12-04 16:12:07 -04:00
|
|
|
# Observable states:
|
|
|
|
# 1. IDLE: _write_fut and _buffer both None
|
|
|
|
# 2. WRITING: _write_fut set; _buffer None
|
|
|
|
# 3. BACKED UP: _write_fut set; _buffer a bytearray
|
|
|
|
# We always copy the data, so the caller can't modify it
|
|
|
|
# while we're still waiting for the I/O to happen.
|
|
|
|
if self._write_fut is None: # IDLE -> WRITING
|
|
|
|
assert self._buffer is None
|
|
|
|
# Pass a copy, except if it's already immutable.
|
|
|
|
self._loop_writing(data=bytes(data))
|
|
|
|
elif not self._buffer: # WRITING -> BACKED UP
|
|
|
|
# Make a mutable copy which we can extend.
|
|
|
|
self._buffer = bytearray(data)
|
|
|
|
self._maybe_pause_protocol()
|
|
|
|
else: # BACKED UP
|
|
|
|
# Append to buffer (also copies).
|
|
|
|
self._buffer.extend(data)
|
|
|
|
self._maybe_pause_protocol()
|
|
|
|
|
|
|
|
def _loop_writing(self, f=None, data=None):
|
2013-10-17 17:40:50 -03:00
|
|
|
try:
|
2018-07-30 15:58:50 -03:00
|
|
|
if f is not None and self._write_fut is None and self._closing:
|
|
|
|
# XXX most likely self._force_close() has been called, and
|
|
|
|
# it has set self._write_fut to None.
|
|
|
|
return
|
2013-10-17 17:40:50 -03:00
|
|
|
assert f is self._write_fut
|
|
|
|
self._write_fut = None
|
2014-02-01 17:49:59 -04:00
|
|
|
self._pending_write = 0
|
2013-10-17 17:40:50 -03:00
|
|
|
if f:
|
|
|
|
f.result()
|
2013-12-04 16:12:07 -04:00
|
|
|
if data is None:
|
|
|
|
data = self._buffer
|
|
|
|
self._buffer = None
|
2013-10-17 17:40:50 -03:00
|
|
|
if not data:
|
|
|
|
if self._closing:
|
|
|
|
self._loop.call_soon(self._call_connection_lost, None)
|
|
|
|
if self._eof_written:
|
|
|
|
self._sock.shutdown(socket.SHUT_WR)
|
2014-02-01 17:49:59 -04:00
|
|
|
# Now that we've reduced the buffer size, tell the
|
|
|
|
# protocol to resume writing if it was paused. Note that
|
|
|
|
# we do this last since the callback is called immediately
|
|
|
|
# and it may add more data to the buffer (even causing the
|
|
|
|
# protocol to be paused again).
|
|
|
|
self._maybe_resume_protocol()
|
2013-12-04 16:12:07 -04:00
|
|
|
else:
|
|
|
|
self._write_fut = self._loop._proactor.send(self._sock, data)
|
2014-02-01 17:49:59 -04:00
|
|
|
if not self._write_fut.done():
|
|
|
|
assert self._pending_write == 0
|
|
|
|
self._pending_write = len(data)
|
|
|
|
self._write_fut.add_done_callback(self._loop_writing)
|
|
|
|
self._maybe_pause_protocol()
|
|
|
|
else:
|
|
|
|
self._write_fut.add_done_callback(self._loop_writing)
|
2018-02-25 12:32:14 -04:00
|
|
|
if self._empty_waiter is not None and self._write_fut is None:
|
|
|
|
self._empty_waiter.set_result(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
except ConnectionResetError as exc:
|
|
|
|
self._force_close(exc)
|
|
|
|
except OSError as exc:
|
2014-02-18 20:40:41 -04:00
|
|
|
self._fatal_error(exc, 'Fatal write error on pipe transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def can_write_eof(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def write_eof(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def abort(self):
|
|
|
|
self._force_close(None)
|
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
def _make_empty_waiter(self):
|
|
|
|
if self._empty_waiter is not None:
|
|
|
|
raise RuntimeError("Empty waiter is already set")
|
|
|
|
self._empty_waiter = self._loop.create_future()
|
|
|
|
if self._write_fut is None:
|
|
|
|
self._empty_waiter.set_result(None)
|
|
|
|
return self._empty_waiter
|
|
|
|
|
|
|
|
def _reset_empty_waiter(self):
|
|
|
|
self._empty_waiter = None
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-31 09:18:18 -04:00
|
|
|
class _ProactorWritePipeTransport(_ProactorBaseWritePipeTransport):
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super().__init__(*args, **kw)
|
|
|
|
self._read_fut = self._loop._proactor.recv(self._sock, 16)
|
|
|
|
self._read_fut.add_done_callback(self._pipe_closed)
|
|
|
|
|
|
|
|
def _pipe_closed(self, fut):
|
|
|
|
if fut.cancelled():
|
|
|
|
# the transport has been closed
|
|
|
|
return
|
2014-02-04 03:57:48 -04:00
|
|
|
assert fut.result() == b''
|
|
|
|
if self._closing:
|
|
|
|
assert self._read_fut is None
|
|
|
|
return
|
2014-01-31 09:18:18 -04:00
|
|
|
assert fut is self._read_fut, (fut, self._read_fut)
|
|
|
|
self._read_fut = None
|
|
|
|
if self._write_fut is not None:
|
2014-02-20 05:33:01 -04:00
|
|
|
self._force_close(BrokenPipeError())
|
2014-01-31 09:18:18 -04:00
|
|
|
else:
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
2022-02-22 19:39:24 -04:00
|
|
|
class _ProactorDatagramTransport(_ProactorBasePipeTransport,
|
|
|
|
transports.DatagramTransport):
|
2019-05-28 06:52:15 -03:00
|
|
|
max_size = 256 * 1024
|
|
|
|
def __init__(self, loop, sock, protocol, address=None,
|
|
|
|
waiter=None, extra=None):
|
|
|
|
self._address = address
|
|
|
|
self._empty_waiter = None
|
2022-04-15 16:59:01 -03:00
|
|
|
self._buffer_size = 0
|
2019-05-28 06:52:15 -03:00
|
|
|
# We don't need to call _protocol.connection_made() since our base
|
|
|
|
# constructor does it for us.
|
|
|
|
super().__init__(loop, sock, protocol, waiter=waiter, extra=extra)
|
|
|
|
|
|
|
|
# The base constructor sets _buffer = None, so we set it here
|
|
|
|
self._buffer = collections.deque()
|
|
|
|
self._loop.call_soon(self._loop_reading)
|
|
|
|
|
|
|
|
def _set_extra(self, sock):
|
|
|
|
_set_socket_extra(self, sock)
|
|
|
|
|
|
|
|
def get_write_buffer_size(self):
|
2022-04-15 16:59:01 -03:00
|
|
|
return self._buffer_size
|
2019-05-28 06:52:15 -03:00
|
|
|
|
|
|
|
def abort(self):
|
|
|
|
self._force_close(None)
|
|
|
|
|
|
|
|
def sendto(self, data, addr=None):
|
|
|
|
if not isinstance(data, (bytes, bytearray, memoryview)):
|
|
|
|
raise TypeError('data argument must be bytes-like object (%r)',
|
|
|
|
type(data))
|
|
|
|
|
|
|
|
if self._address is not None and addr not in (None, self._address):
|
|
|
|
raise ValueError(
|
|
|
|
f'Invalid address: must be None or {self._address}')
|
|
|
|
|
|
|
|
if self._conn_lost and self._address:
|
|
|
|
if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
|
|
|
|
logger.warning('socket.sendto() raised exception.')
|
|
|
|
self._conn_lost += 1
|
|
|
|
return
|
|
|
|
|
|
|
|
# Ensure that what we buffer is immutable.
|
|
|
|
self._buffer.append((bytes(data), addr))
|
2024-02-16 22:38:07 -04:00
|
|
|
self._buffer_size += len(data) + 8 # include header bytes
|
2019-05-28 06:52:15 -03:00
|
|
|
|
|
|
|
if self._write_fut is None:
|
|
|
|
# No current write operations are active, kick one off
|
|
|
|
self._loop_writing()
|
|
|
|
# else: A write operation is already kicked off
|
|
|
|
|
|
|
|
self._maybe_pause_protocol()
|
|
|
|
|
|
|
|
def _loop_writing(self, fut=None):
|
|
|
|
try:
|
|
|
|
if self._conn_lost:
|
|
|
|
return
|
|
|
|
|
|
|
|
assert fut is self._write_fut
|
|
|
|
self._write_fut = None
|
|
|
|
if fut:
|
|
|
|
# We are in a _loop_writing() done callback, get the result
|
|
|
|
fut.result()
|
|
|
|
|
|
|
|
if not self._buffer or (self._conn_lost and self._address):
|
|
|
|
# The connection has been closed
|
|
|
|
if self._closing:
|
|
|
|
self._loop.call_soon(self._call_connection_lost, None)
|
|
|
|
return
|
|
|
|
|
|
|
|
data, addr = self._buffer.popleft()
|
2022-04-15 16:59:01 -03:00
|
|
|
self._buffer_size -= len(data)
|
2019-05-28 06:52:15 -03:00
|
|
|
if self._address is not None:
|
|
|
|
self._write_fut = self._loop._proactor.send(self._sock,
|
|
|
|
data)
|
|
|
|
else:
|
|
|
|
self._write_fut = self._loop._proactor.sendto(self._sock,
|
|
|
|
data,
|
|
|
|
addr=addr)
|
|
|
|
except OSError as exc:
|
|
|
|
self._protocol.error_received(exc)
|
|
|
|
except Exception as exc:
|
|
|
|
self._fatal_error(exc, 'Fatal write error on datagram transport')
|
|
|
|
else:
|
|
|
|
self._write_fut.add_done_callback(self._loop_writing)
|
|
|
|
self._maybe_resume_protocol()
|
|
|
|
|
|
|
|
def _loop_reading(self, fut=None):
|
|
|
|
data = None
|
|
|
|
try:
|
|
|
|
if self._conn_lost:
|
|
|
|
return
|
|
|
|
|
|
|
|
assert self._read_fut is fut or (self._read_fut is None and
|
|
|
|
self._closing)
|
|
|
|
|
|
|
|
self._read_fut = None
|
|
|
|
if fut is not None:
|
|
|
|
res = fut.result()
|
|
|
|
|
|
|
|
if self._closing:
|
|
|
|
# since close() has been called we ignore any read data
|
|
|
|
data = None
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._address is not None:
|
|
|
|
data, addr = res, self._address
|
|
|
|
else:
|
|
|
|
data, addr = res
|
|
|
|
|
|
|
|
if self._conn_lost:
|
|
|
|
return
|
|
|
|
if self._address is not None:
|
|
|
|
self._read_fut = self._loop._proactor.recv(self._sock,
|
|
|
|
self.max_size)
|
|
|
|
else:
|
|
|
|
self._read_fut = self._loop._proactor.recvfrom(self._sock,
|
|
|
|
self.max_size)
|
|
|
|
except OSError as exc:
|
|
|
|
self._protocol.error_received(exc)
|
|
|
|
except exceptions.CancelledError:
|
|
|
|
if not self._closing:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if self._read_fut is not None:
|
|
|
|
self._read_fut.add_done_callback(self._loop_reading)
|
|
|
|
finally:
|
|
|
|
if data:
|
|
|
|
self._protocol.datagram_received(data, addr)
|
|
|
|
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
class _ProactorDuplexPipeTransport(_ProactorReadPipeTransport,
|
2014-01-31 09:18:18 -04:00
|
|
|
_ProactorBaseWritePipeTransport,
|
2013-10-17 17:40:50 -03:00
|
|
|
transports.Transport):
|
|
|
|
"""Transport for duplex pipes."""
|
|
|
|
|
|
|
|
def can_write_eof(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def write_eof(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class _ProactorSocketTransport(_ProactorReadPipeTransport,
|
2014-01-31 09:18:18 -04:00
|
|
|
_ProactorBaseWritePipeTransport,
|
2013-10-17 17:40:50 -03:00
|
|
|
transports.Transport):
|
|
|
|
"""Transport for connected sockets."""
|
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
_sendfile_compatible = constants._SendfileMode.TRY_NATIVE
|
2018-01-27 15:22:47 -04:00
|
|
|
|
2018-12-03 15:08:13 -04:00
|
|
|
def __init__(self, loop, sock, protocol, waiter=None,
|
|
|
|
extra=None, server=None):
|
|
|
|
super().__init__(loop, sock, protocol, waiter, extra, server)
|
|
|
|
base_events._set_nodelay(sock)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def _set_extra(self, sock):
|
2019-05-28 06:52:15 -03:00
|
|
|
_set_socket_extra(self, sock)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def can_write_eof(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def write_eof(self):
|
|
|
|
if self._closing or self._eof_written:
|
|
|
|
return
|
|
|
|
self._eof_written = True
|
|
|
|
if self._write_fut is None:
|
|
|
|
self._sock.shutdown(socket.SHUT_WR)
|
|
|
|
|
|
|
|
|
|
|
|
class BaseProactorEventLoop(base_events.BaseEventLoop):
|
|
|
|
|
|
|
|
def __init__(self, proactor):
|
|
|
|
super().__init__()
|
2013-10-17 19:39:45 -03:00
|
|
|
logger.debug('Using proactor: %s', proactor.__class__.__name__)
|
2013-10-17 17:40:50 -03:00
|
|
|
self._proactor = proactor
|
|
|
|
self._selector = proactor # convenient alias
|
2014-01-10 19:03:21 -04:00
|
|
|
self._self_reading_future = None
|
|
|
|
self._accept_futures = {} # socket file descriptor => Future
|
2013-10-17 17:40:50 -03:00
|
|
|
proactor.set_loop(self)
|
|
|
|
self._make_self_pipe()
|
2019-08-26 06:51:08 -03:00
|
|
|
if threading.current_thread() is threading.main_thread():
|
|
|
|
# wakeup fd can only be installed to a file descriptor from the main thread
|
2022-09-17 18:12:45 -03:00
|
|
|
signal.set_wakeup_fd(self._csock.fileno())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def _make_socket_transport(self, sock, protocol, waiter=None,
|
|
|
|
extra=None, server=None):
|
|
|
|
return _ProactorSocketTransport(self, sock, protocol, waiter,
|
|
|
|
extra, server)
|
|
|
|
|
2017-12-19 15:45:42 -04:00
|
|
|
def _make_ssl_transport(
|
|
|
|
self, rawsock, protocol, sslcontext, waiter=None,
|
|
|
|
*, server_side=False, server_hostname=None,
|
|
|
|
extra=None, server=None,
|
2022-02-15 09:04:00 -04:00
|
|
|
ssl_handshake_timeout=None,
|
|
|
|
ssl_shutdown_timeout=None):
|
2017-12-19 15:45:42 -04:00
|
|
|
ssl_protocol = sslproto.SSLProtocol(
|
|
|
|
self, protocol, sslcontext, waiter,
|
|
|
|
server_side, server_hostname,
|
2022-02-15 09:04:00 -04:00
|
|
|
ssl_handshake_timeout=ssl_handshake_timeout,
|
|
|
|
ssl_shutdown_timeout=ssl_shutdown_timeout)
|
2015-01-13 19:19:09 -04:00
|
|
|
_ProactorSocketTransport(self, rawsock, ssl_protocol,
|
|
|
|
extra=extra, server=server)
|
|
|
|
return ssl_protocol._app_transport
|
|
|
|
|
2019-05-28 06:52:15 -03:00
|
|
|
def _make_datagram_transport(self, sock, protocol,
|
|
|
|
address=None, waiter=None, extra=None):
|
|
|
|
return _ProactorDatagramTransport(self, sock, protocol, address,
|
|
|
|
waiter, extra)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def _make_duplex_pipe_transport(self, sock, protocol, waiter=None,
|
|
|
|
extra=None):
|
|
|
|
return _ProactorDuplexPipeTransport(self,
|
|
|
|
sock, protocol, waiter, extra)
|
|
|
|
|
|
|
|
def _make_read_pipe_transport(self, sock, protocol, waiter=None,
|
|
|
|
extra=None):
|
|
|
|
return _ProactorReadPipeTransport(self, sock, protocol, waiter, extra)
|
|
|
|
|
|
|
|
def _make_write_pipe_transport(self, sock, protocol, waiter=None,
|
2014-01-31 09:18:18 -04:00
|
|
|
extra=None):
|
|
|
|
# We want connection_lost() to be called when other end closes
|
|
|
|
return _ProactorWritePipeTransport(self,
|
|
|
|
sock, protocol, waiter, extra)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def close(self):
|
2014-12-26 16:07:52 -04:00
|
|
|
if self.is_running():
|
2014-12-04 20:43:42 -04:00
|
|
|
raise RuntimeError("Cannot close a running event loop")
|
2014-06-10 05:23:10 -03:00
|
|
|
if self.is_closed():
|
|
|
|
return
|
2015-01-13 11:11:19 -04:00
|
|
|
|
2019-10-23 12:25:29 -03:00
|
|
|
if threading.current_thread() is threading.main_thread():
|
2022-09-17 18:12:45 -03:00
|
|
|
signal.set_wakeup_fd(-1)
|
2015-01-13 11:11:19 -04:00
|
|
|
# Call these methods before closing the event loop (before calling
|
|
|
|
# BaseEventLoop.close), because they can schedule callbacks with
|
|
|
|
# call_soon(), which is forbidden when the event loop is closed.
|
2014-06-10 05:23:10 -03:00
|
|
|
self._stop_accept_futures()
|
|
|
|
self._close_self_pipe()
|
|
|
|
self._proactor.close()
|
|
|
|
self._proactor = None
|
|
|
|
self._selector = None
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-13 11:11:19 -04:00
|
|
|
# Close the event loop
|
|
|
|
super().close()
|
|
|
|
|
2017-12-14 21:53:26 -04:00
|
|
|
async def sock_recv(self, sock, n):
|
|
|
|
return await self._proactor.recv(sock, n)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2017-12-14 21:53:26 -04:00
|
|
|
async def sock_recv_into(self, sock, buf):
|
|
|
|
return await self._proactor.recv_into(sock, buf)
|
2017-10-19 16:46:40 -03:00
|
|
|
|
2022-03-13 13:42:29 -03:00
|
|
|
async def sock_recvfrom(self, sock, bufsize):
|
|
|
|
return await self._proactor.recvfrom(sock, bufsize)
|
|
|
|
|
|
|
|
async def sock_recvfrom_into(self, sock, buf, nbytes=0):
|
|
|
|
if not nbytes:
|
|
|
|
nbytes = len(buf)
|
|
|
|
|
|
|
|
return await self._proactor.recvfrom_into(sock, buf, nbytes)
|
|
|
|
|
2017-12-14 21:53:26 -04:00
|
|
|
async def sock_sendall(self, sock, data):
|
|
|
|
return await self._proactor.send(sock, data)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-13 13:42:29 -03:00
|
|
|
async def sock_sendto(self, sock, data, address):
|
|
|
|
return await self._proactor.sendto(sock, data, 0, address)
|
|
|
|
|
2017-12-14 21:53:26 -04:00
|
|
|
async def sock_connect(self, sock, address):
|
|
|
|
return await self._proactor.connect(sock, address)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2017-12-14 21:53:26 -04:00
|
|
|
async def sock_accept(self, sock):
|
|
|
|
return await self._proactor.accept(sock)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
async def _sock_sendfile_native(self, sock, file, offset, count):
|
|
|
|
try:
|
|
|
|
fileno = file.fileno()
|
|
|
|
except (AttributeError, io.UnsupportedOperation) as err:
|
2018-09-11 14:13:04 -03:00
|
|
|
raise exceptions.SendfileNotAvailableError("not a regular file")
|
2018-02-25 12:32:14 -04:00
|
|
|
try:
|
|
|
|
fsize = os.fstat(fileno).st_size
|
2019-11-19 17:34:03 -04:00
|
|
|
except OSError:
|
2018-09-11 14:13:04 -03:00
|
|
|
raise exceptions.SendfileNotAvailableError("not a regular file")
|
2018-02-25 12:32:14 -04:00
|
|
|
blocksize = count if count else fsize
|
|
|
|
if not blocksize:
|
|
|
|
return 0 # empty file
|
|
|
|
|
|
|
|
blocksize = min(blocksize, 0xffff_ffff)
|
|
|
|
end_pos = min(offset + count, fsize) if count else fsize
|
|
|
|
offset = min(offset, fsize)
|
|
|
|
total_sent = 0
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
blocksize = min(end_pos - offset, blocksize)
|
|
|
|
if blocksize <= 0:
|
|
|
|
return total_sent
|
|
|
|
await self._proactor.sendfile(sock, file, offset, blocksize)
|
|
|
|
offset += blocksize
|
|
|
|
total_sent += blocksize
|
|
|
|
finally:
|
|
|
|
if total_sent > 0:
|
|
|
|
file.seek(offset)
|
|
|
|
|
|
|
|
async def _sendfile_native(self, transp, file, offset, count):
|
|
|
|
resume_reading = transp.is_reading()
|
|
|
|
transp.pause_reading()
|
|
|
|
await transp._make_empty_waiter()
|
|
|
|
try:
|
|
|
|
return await self.sock_sendfile(transp._sock, file, offset, count,
|
|
|
|
fallback=False)
|
|
|
|
finally:
|
|
|
|
transp._reset_empty_waiter()
|
|
|
|
if resume_reading:
|
|
|
|
transp.resume_reading()
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def _close_self_pipe(self):
|
2014-01-10 19:03:21 -04:00
|
|
|
if self._self_reading_future is not None:
|
|
|
|
self._self_reading_future.cancel()
|
|
|
|
self._self_reading_future = None
|
2013-10-17 17:40:50 -03:00
|
|
|
self._ssock.close()
|
|
|
|
self._ssock = None
|
|
|
|
self._csock.close()
|
|
|
|
self._csock = None
|
|
|
|
self._internal_fds -= 1
|
|
|
|
|
|
|
|
def _make_self_pipe(self):
|
|
|
|
# A self-socket, really. :-)
|
2017-11-28 06:15:26 -04:00
|
|
|
self._ssock, self._csock = socket.socketpair()
|
2013-10-17 17:40:50 -03:00
|
|
|
self._ssock.setblocking(False)
|
|
|
|
self._csock.setblocking(False)
|
|
|
|
self._internal_fds += 1
|
|
|
|
|
|
|
|
def _loop_self_reading(self, f=None):
|
|
|
|
try:
|
|
|
|
if f is not None:
|
|
|
|
f.result() # may raise
|
2020-08-31 16:57:52 -03:00
|
|
|
if self._self_reading_future is not f:
|
|
|
|
# When we scheduled this Future, we assigned it to
|
|
|
|
# _self_reading_future. If it's not there now, something has
|
|
|
|
# tried to cancel the loop while this callback was still in the
|
|
|
|
# queue (see windows_events.ProactorEventLoop.run_forever). In
|
|
|
|
# that case stop here instead of continuing to schedule a new
|
|
|
|
# iteration.
|
|
|
|
return
|
2013-10-17 17:40:50 -03:00
|
|
|
f = self._proactor.recv(self._ssock, 4096)
|
2018-09-11 14:13:04 -03:00
|
|
|
except exceptions.CancelledError:
|
2015-01-21 18:38:37 -04:00
|
|
|
# _close_self_pipe() has been called, stop waiting for data
|
|
|
|
return
|
2019-05-27 09:45:12 -03:00
|
|
|
except (SystemExit, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except BaseException as exc:
|
2015-01-21 18:38:37 -04:00
|
|
|
self.call_exception_handler({
|
|
|
|
'message': 'Error on reading from the event loop self pipe',
|
|
|
|
'exception': exc,
|
|
|
|
'loop': self,
|
|
|
|
})
|
2013-10-17 17:40:50 -03:00
|
|
|
else:
|
2014-01-10 19:03:21 -04:00
|
|
|
self._self_reading_future = f
|
2013-10-17 17:40:50 -03:00
|
|
|
f.add_done_callback(self._loop_self_reading)
|
|
|
|
|
|
|
|
def _write_to_self(self):
|
2020-09-12 03:50:18 -03:00
|
|
|
# This may be called from a different thread, possibly after
|
|
|
|
# _close_self_pipe() has been called or even while it is
|
|
|
|
# running. Guard for self._csock being None or closed. When
|
|
|
|
# a socket is closed, send() raises OSError (with errno set to
|
|
|
|
# EBADF, but let's not rely on the exact error code).
|
|
|
|
csock = self._csock
|
|
|
|
if csock is None:
|
|
|
|
return
|
|
|
|
|
2019-01-15 08:58:38 -04:00
|
|
|
try:
|
2020-09-12 03:50:18 -03:00
|
|
|
csock.send(b'\0')
|
2019-01-15 08:58:38 -04:00
|
|
|
except OSError:
|
|
|
|
if self._debug:
|
|
|
|
logger.debug("Fail to write a null byte into the "
|
|
|
|
"self-pipe socket",
|
|
|
|
exc_info=True)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-13 19:19:09 -04:00
|
|
|
def _start_serving(self, protocol_factory, sock,
|
2017-12-19 15:45:42 -04:00
|
|
|
sslcontext=None, server=None, backlog=100,
|
2022-02-15 09:04:00 -04:00
|
|
|
ssl_handshake_timeout=None,
|
|
|
|
ssl_shutdown_timeout=None):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def loop(f=None):
|
|
|
|
try:
|
|
|
|
if f is not None:
|
|
|
|
conn, addr = f.result()
|
2014-07-11 22:11:53 -03:00
|
|
|
if self._debug:
|
|
|
|
logger.debug("%r got a new connection from %r: %r",
|
|
|
|
server, addr, conn)
|
2013-10-17 17:40:50 -03:00
|
|
|
protocol = protocol_factory()
|
2015-01-13 19:19:09 -04:00
|
|
|
if sslcontext is not None:
|
|
|
|
self._make_ssl_transport(
|
|
|
|
conn, protocol, sslcontext, server_side=True,
|
2017-12-19 15:45:42 -04:00
|
|
|
extra={'peername': addr}, server=server,
|
2022-02-15 09:04:00 -04:00
|
|
|
ssl_handshake_timeout=ssl_handshake_timeout,
|
|
|
|
ssl_shutdown_timeout=ssl_shutdown_timeout)
|
2015-01-13 19:19:09 -04:00
|
|
|
else:
|
|
|
|
self._make_socket_transport(
|
|
|
|
conn, protocol,
|
|
|
|
extra={'peername': addr}, server=server)
|
2014-06-10 05:23:10 -03:00
|
|
|
if self.is_closed():
|
|
|
|
return
|
2013-10-17 17:40:50 -03:00
|
|
|
f = self._proactor.accept(sock)
|
2014-02-18 19:02:19 -04:00
|
|
|
except OSError as exc:
|
2013-10-17 17:40:50 -03:00
|
|
|
if sock.fileno() != -1:
|
2014-02-18 19:02:19 -04:00
|
|
|
self.call_exception_handler({
|
2014-08-25 18:20:52 -03:00
|
|
|
'message': 'Accept failed on a socket',
|
2014-02-18 19:02:19 -04:00
|
|
|
'exception': exc,
|
2019-05-27 10:57:20 -03:00
|
|
|
'socket': trsock.TransportSocket(sock),
|
2014-02-18 19:02:19 -04:00
|
|
|
})
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.close()
|
2014-08-25 18:20:52 -03:00
|
|
|
elif self._debug:
|
|
|
|
logger.debug("Accept failed on socket %r",
|
|
|
|
sock, exc_info=True)
|
2018-09-11 14:13:04 -03:00
|
|
|
except exceptions.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.close()
|
|
|
|
else:
|
2014-01-10 19:03:21 -04:00
|
|
|
self._accept_futures[sock.fileno()] = f
|
2013-10-17 17:40:50 -03:00
|
|
|
f.add_done_callback(loop)
|
|
|
|
|
|
|
|
self.call_soon(loop)
|
|
|
|
|
|
|
|
def _process_events(self, event_list):
|
2015-01-09 16:32:05 -04:00
|
|
|
# Events are processed in the IocpProactor._poll() method
|
|
|
|
pass
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-10 05:23:10 -03:00
|
|
|
def _stop_accept_futures(self):
|
2014-01-10 19:03:21 -04:00
|
|
|
for future in self._accept_futures.values():
|
|
|
|
future.cancel()
|
2014-06-10 05:23:10 -03:00
|
|
|
self._accept_futures.clear()
|
|
|
|
|
|
|
|
def _stop_serving(self, sock):
|
2017-12-19 08:23:17 -04:00
|
|
|
future = self._accept_futures.pop(sock.fileno(), None)
|
|
|
|
if future:
|
|
|
|
future.cancel()
|
2013-10-17 17:40:50 -03:00
|
|
|
self._proactor._stop_serving(sock)
|
|
|
|
sock.close()
|