proactor_events.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. """Event loop using a proactor and related classes.
  2. A proactor is a "notify-on-completion" multiplexer. Currently a
  3. proactor is only implemented on Windows with IOCP.
  4. """
  5. __all__ = 'BaseProactorEventLoop',
  6. import io
  7. import os
  8. import socket
  9. import warnings
  10. import signal
  11. import threading
  12. import collections
  13. from . import base_events
  14. from . import constants
  15. from . import futures
  16. from . import exceptions
  17. from . import protocols
  18. from . import sslproto
  19. from . import transports
  20. from . import trsock
  21. from .log import logger
  22. def _set_socket_extra(transport, sock):
  23. transport._extra['socket'] = trsock.TransportSocket(sock)
  24. try:
  25. transport._extra['sockname'] = sock.getsockname()
  26. except socket.error:
  27. if transport._loop.get_debug():
  28. logger.warning(
  29. "getsockname() failed on %r", sock, exc_info=True)
  30. if 'peername' not in transport._extra:
  31. try:
  32. transport._extra['peername'] = sock.getpeername()
  33. except socket.error:
  34. # UDP sockets may not have a peer name
  35. transport._extra['peername'] = None
  36. class _ProactorBasePipeTransport(transports._FlowControlMixin,
  37. transports.BaseTransport):
  38. """Base class for pipe and socket transports."""
  39. def __init__(self, loop, sock, protocol, waiter=None,
  40. extra=None, server=None):
  41. super().__init__(extra, loop)
  42. self._set_extra(sock)
  43. self._sock = sock
  44. self.set_protocol(protocol)
  45. self._server = server
  46. self._buffer = None # None or bytearray.
  47. self._read_fut = None
  48. self._write_fut = None
  49. self._pending_write = 0
  50. self._conn_lost = 0
  51. self._closing = False # Set when close() called.
  52. self._called_connection_lost = False
  53. self._eof_written = False
  54. if self._server is not None:
  55. self._server._attach()
  56. self._loop.call_soon(self._protocol.connection_made, self)
  57. if waiter is not None:
  58. # only wake up the waiter when connection_made() has been called
  59. self._loop.call_soon(futures._set_result_unless_cancelled,
  60. waiter, None)
  61. def __repr__(self):
  62. info = [self.__class__.__name__]
  63. if self._sock is None:
  64. info.append('closed')
  65. elif self._closing:
  66. info.append('closing')
  67. if self._sock is not None:
  68. info.append(f'fd={self._sock.fileno()}')
  69. if self._read_fut is not None:
  70. info.append(f'read={self._read_fut!r}')
  71. if self._write_fut is not None:
  72. info.append(f'write={self._write_fut!r}')
  73. if self._buffer:
  74. info.append(f'write_bufsize={len(self._buffer)}')
  75. if self._eof_written:
  76. info.append('EOF written')
  77. return '<{}>'.format(' '.join(info))
  78. def _set_extra(self, sock):
  79. self._extra['pipe'] = sock
  80. def set_protocol(self, protocol):
  81. self._protocol = protocol
  82. def get_protocol(self):
  83. return self._protocol
  84. def is_closing(self):
  85. return self._closing
  86. def close(self):
  87. if self._closing:
  88. return
  89. self._closing = True
  90. self._conn_lost += 1
  91. if not self._buffer and self._write_fut is None:
  92. self._loop.call_soon(self._call_connection_lost, None)
  93. if self._read_fut is not None:
  94. self._read_fut.cancel()
  95. self._read_fut = None
  96. def __del__(self, _warn=warnings.warn):
  97. if self._sock is not None:
  98. _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
  99. self._sock.close()
  100. def _fatal_error(self, exc, message='Fatal error on pipe transport'):
  101. try:
  102. if isinstance(exc, OSError):
  103. if self._loop.get_debug():
  104. logger.debug("%r: %s", self, message, exc_info=True)
  105. else:
  106. self._loop.call_exception_handler({
  107. 'message': message,
  108. 'exception': exc,
  109. 'transport': self,
  110. 'protocol': self._protocol,
  111. })
  112. finally:
  113. self._force_close(exc)
  114. def _force_close(self, exc):
  115. if self._empty_waiter is not None and not self._empty_waiter.done():
  116. if exc is None:
  117. self._empty_waiter.set_result(None)
  118. else:
  119. self._empty_waiter.set_exception(exc)
  120. if self._closing and self._called_connection_lost:
  121. return
  122. self._closing = True
  123. self._conn_lost += 1
  124. if self._write_fut:
  125. self._write_fut.cancel()
  126. self._write_fut = None
  127. if self._read_fut:
  128. self._read_fut.cancel()
  129. self._read_fut = None
  130. self._pending_write = 0
  131. self._buffer = None
  132. self._loop.call_soon(self._call_connection_lost, exc)
  133. def _call_connection_lost(self, exc):
  134. if self._called_connection_lost:
  135. return
  136. try:
  137. self._protocol.connection_lost(exc)
  138. finally:
  139. # XXX If there is a pending overlapped read on the other
  140. # end then it may fail with ERROR_NETNAME_DELETED if we
  141. # just close our end. First calling shutdown() seems to
  142. # cure it, but maybe using DisconnectEx() would be better.
  143. if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
  144. self._sock.shutdown(socket.SHUT_RDWR)
  145. self._sock.close()
  146. self._sock = None
  147. server = self._server
  148. if server is not None:
  149. server._detach()
  150. self._server = None
  151. self._called_connection_lost = True
  152. def get_write_buffer_size(self):
  153. size = self._pending_write
  154. if self._buffer is not None:
  155. size += len(self._buffer)
  156. return size
  157. class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
  158. transports.ReadTransport):
  159. """Transport for read pipes."""
  160. def __init__(self, loop, sock, protocol, waiter=None,
  161. extra=None, server=None, buffer_size=65536):
  162. self._pending_data_length = -1
  163. self._paused = True
  164. super().__init__(loop, sock, protocol, waiter, extra, server)
  165. self._data = bytearray(buffer_size)
  166. self._loop.call_soon(self._loop_reading)
  167. self._paused = False
  168. def is_reading(self):
  169. return not self._paused and not self._closing
  170. def pause_reading(self):
  171. if self._closing or self._paused:
  172. return
  173. self._paused = True
  174. # bpo-33694: Don't cancel self._read_fut because cancelling an
  175. # overlapped WSASend() loss silently data with the current proactor
  176. # implementation.
  177. #
  178. # If CancelIoEx() fails with ERROR_NOT_FOUND, it means that WSASend()
  179. # completed (even if HasOverlappedIoCompleted() returns 0), but
  180. # Overlapped.cancel() currently silently ignores the ERROR_NOT_FOUND
  181. # error. Once the overlapped is ignored, the IOCP loop will ignores the
  182. # completion I/O event and so not read the result of the overlapped
  183. # WSARecv().
  184. if self._loop.get_debug():
  185. logger.debug("%r pauses reading", self)
  186. def resume_reading(self):
  187. if self._closing or not self._paused:
  188. return
  189. self._paused = False
  190. if self._read_fut is None:
  191. self._loop.call_soon(self._loop_reading, None)
  192. length = self._pending_data_length
  193. self._pending_data_length = -1
  194. if length > -1:
  195. # Call the protocol method after calling _loop_reading(),
  196. # since the protocol can decide to pause reading again.
  197. self._loop.call_soon(self._data_received, self._data[:length], length)
  198. if self._loop.get_debug():
  199. logger.debug("%r resumes reading", self)
  200. def _eof_received(self):
  201. if self._loop.get_debug():
  202. logger.debug("%r received EOF", self)
  203. try:
  204. keep_open = self._protocol.eof_received()
  205. except (SystemExit, KeyboardInterrupt):
  206. raise
  207. except BaseException as exc:
  208. self._fatal_error(
  209. exc, 'Fatal error: protocol.eof_received() call failed.')
  210. return
  211. if not keep_open:
  212. self.close()
  213. def _data_received(self, data, length):
  214. if self._paused:
  215. # Don't call any protocol method while reading is paused.
  216. # The protocol will be called on resume_reading().
  217. assert self._pending_data_length == -1
  218. self._pending_data_length = length
  219. return
  220. if length == 0:
  221. self._eof_received()
  222. return
  223. if isinstance(self._protocol, protocols.BufferedProtocol):
  224. try:
  225. protocols._feed_data_to_buffered_proto(self._protocol, data)
  226. except (SystemExit, KeyboardInterrupt):
  227. raise
  228. except BaseException as exc:
  229. self._fatal_error(exc,
  230. 'Fatal error: protocol.buffer_updated() '
  231. 'call failed.')
  232. return
  233. else:
  234. self._protocol.data_received(data)
  235. def _loop_reading(self, fut=None):
  236. length = -1
  237. data = None
  238. try:
  239. if fut is not None:
  240. assert self._read_fut is fut or (self._read_fut is None and
  241. self._closing)
  242. self._read_fut = None
  243. if fut.done():
  244. # deliver data later in "finally" clause
  245. length = fut.result()
  246. if length == 0:
  247. # we got end-of-file so no need to reschedule a new read
  248. return
  249. # It's a new slice so make it immutable so protocols upstream don't have problems
  250. data = bytes(memoryview(self._data)[:length])
  251. else:
  252. # the future will be replaced by next proactor.recv call
  253. fut.cancel()
  254. if self._closing:
  255. # since close() has been called we ignore any read data
  256. return
  257. # bpo-33694: buffer_updated() has currently no fast path because of
  258. # a data loss issue caused by overlapped WSASend() cancellation.
  259. if not self._paused:
  260. # reschedule a new read
  261. self._read_fut = self._loop._proactor.recv_into(self._sock, self._data)
  262. except ConnectionAbortedError as exc:
  263. if not self._closing:
  264. self._fatal_error(exc, 'Fatal read error on pipe transport')
  265. elif self._loop.get_debug():
  266. logger.debug("Read error on pipe transport while closing",
  267. exc_info=True)
  268. except ConnectionResetError as exc:
  269. self._force_close(exc)
  270. except OSError as exc:
  271. self._fatal_error(exc, 'Fatal read error on pipe transport')
  272. except exceptions.CancelledError:
  273. if not self._closing:
  274. raise
  275. else:
  276. if not self._paused:
  277. self._read_fut.add_done_callback(self._loop_reading)
  278. finally:
  279. if length > -1:
  280. self._data_received(data, length)
  281. class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
  282. transports.WriteTransport):
  283. """Transport for write pipes."""
  284. _start_tls_compatible = True
  285. def __init__(self, *args, **kw):
  286. super().__init__(*args, **kw)
  287. self._empty_waiter = None
  288. def write(self, data):
  289. if not isinstance(data, (bytes, bytearray, memoryview)):
  290. raise TypeError(
  291. f"data argument must be a bytes-like object, "
  292. f"not {type(data).__name__}")
  293. if self._eof_written:
  294. raise RuntimeError('write_eof() already called')
  295. if self._empty_waiter is not None:
  296. raise RuntimeError('unable to write; sendfile is in progress')
  297. if not data:
  298. return
  299. if self._conn_lost:
  300. if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
  301. logger.warning('socket.send() raised exception.')
  302. self._conn_lost += 1
  303. return
  304. # Observable states:
  305. # 1. IDLE: _write_fut and _buffer both None
  306. # 2. WRITING: _write_fut set; _buffer None
  307. # 3. BACKED UP: _write_fut set; _buffer a bytearray
  308. # We always copy the data, so the caller can't modify it
  309. # while we're still waiting for the I/O to happen.
  310. if self._write_fut is None: # IDLE -> WRITING
  311. assert self._buffer is None
  312. # Pass a copy, except if it's already immutable.
  313. self._loop_writing(data=bytes(data))
  314. elif not self._buffer: # WRITING -> BACKED UP
  315. # Make a mutable copy which we can extend.
  316. self._buffer = bytearray(data)
  317. self._maybe_pause_protocol()
  318. else: # BACKED UP
  319. # Append to buffer (also copies).
  320. self._buffer.extend(data)
  321. self._maybe_pause_protocol()
  322. def _loop_writing(self, f=None, data=None):
  323. try:
  324. if f is not None and self._write_fut is None and self._closing:
  325. # XXX most likely self._force_close() has been called, and
  326. # it has set self._write_fut to None.
  327. return
  328. assert f is self._write_fut
  329. self._write_fut = None
  330. self._pending_write = 0
  331. if f:
  332. f.result()
  333. if data is None:
  334. data = self._buffer
  335. self._buffer = None
  336. if not data:
  337. if self._closing:
  338. self._loop.call_soon(self._call_connection_lost, None)
  339. if self._eof_written:
  340. self._sock.shutdown(socket.SHUT_WR)
  341. # Now that we've reduced the buffer size, tell the
  342. # protocol to resume writing if it was paused. Note that
  343. # we do this last since the callback is called immediately
  344. # and it may add more data to the buffer (even causing the
  345. # protocol to be paused again).
  346. self._maybe_resume_protocol()
  347. else:
  348. self._write_fut = self._loop._proactor.send(self._sock, data)
  349. if not self._write_fut.done():
  350. assert self._pending_write == 0
  351. self._pending_write = len(data)
  352. self._write_fut.add_done_callback(self._loop_writing)
  353. self._maybe_pause_protocol()
  354. else:
  355. self._write_fut.add_done_callback(self._loop_writing)
  356. if self._empty_waiter is not None and self._write_fut is None:
  357. self._empty_waiter.set_result(None)
  358. except ConnectionResetError as exc:
  359. self._force_close(exc)
  360. except OSError as exc:
  361. self._fatal_error(exc, 'Fatal write error on pipe transport')
  362. def can_write_eof(self):
  363. return True
  364. def write_eof(self):
  365. self.close()
  366. def abort(self):
  367. self._force_close(None)
  368. def _make_empty_waiter(self):
  369. if self._empty_waiter is not None:
  370. raise RuntimeError("Empty waiter is already set")
  371. self._empty_waiter = self._loop.create_future()
  372. if self._write_fut is None:
  373. self._empty_waiter.set_result(None)
  374. return self._empty_waiter
  375. def _reset_empty_waiter(self):
  376. self._empty_waiter = None
  377. class _ProactorWritePipeTransport(_ProactorBaseWritePipeTransport):
  378. def __init__(self, *args, **kw):
  379. super().__init__(*args, **kw)
  380. self._read_fut = self._loop._proactor.recv(self._sock, 16)
  381. self._read_fut.add_done_callback(self._pipe_closed)
  382. def _pipe_closed(self, fut):
  383. if fut.cancelled():
  384. # the transport has been closed
  385. return
  386. assert fut.result() == b''
  387. if self._closing:
  388. assert self._read_fut is None
  389. return
  390. assert fut is self._read_fut, (fut, self._read_fut)
  391. self._read_fut = None
  392. if self._write_fut is not None:
  393. self._force_close(BrokenPipeError())
  394. else:
  395. self.close()
  396. class _ProactorDatagramTransport(_ProactorBasePipeTransport,
  397. transports.DatagramTransport):
  398. max_size = 256 * 1024
  399. def __init__(self, loop, sock, protocol, address=None,
  400. waiter=None, extra=None):
  401. self._address = address
  402. self._empty_waiter = None
  403. self._buffer_size = 0
  404. # We don't need to call _protocol.connection_made() since our base
  405. # constructor does it for us.
  406. super().__init__(loop, sock, protocol, waiter=waiter, extra=extra)
  407. # The base constructor sets _buffer = None, so we set it here
  408. self._buffer = collections.deque()
  409. self._loop.call_soon(self._loop_reading)
  410. def _set_extra(self, sock):
  411. _set_socket_extra(self, sock)
  412. def get_write_buffer_size(self):
  413. return self._buffer_size
  414. def abort(self):
  415. self._force_close(None)
  416. def sendto(self, data, addr=None):
  417. if not isinstance(data, (bytes, bytearray, memoryview)):
  418. raise TypeError('data argument must be bytes-like object (%r)',
  419. type(data))
  420. if not data:
  421. return
  422. if self._address is not None and addr not in (None, self._address):
  423. raise ValueError(
  424. f'Invalid address: must be None or {self._address}')
  425. if self._conn_lost and self._address:
  426. if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
  427. logger.warning('socket.sendto() raised exception.')
  428. self._conn_lost += 1
  429. return
  430. # Ensure that what we buffer is immutable.
  431. self._buffer.append((bytes(data), addr))
  432. self._buffer_size += len(data)
  433. if self._write_fut is None:
  434. # No current write operations are active, kick one off
  435. self._loop_writing()
  436. # else: A write operation is already kicked off
  437. self._maybe_pause_protocol()
  438. def _loop_writing(self, fut=None):
  439. try:
  440. if self._conn_lost:
  441. return
  442. assert fut is self._write_fut
  443. self._write_fut = None
  444. if fut:
  445. # We are in a _loop_writing() done callback, get the result
  446. fut.result()
  447. if not self._buffer or (self._conn_lost and self._address):
  448. # The connection has been closed
  449. if self._closing:
  450. self._loop.call_soon(self._call_connection_lost, None)
  451. return
  452. data, addr = self._buffer.popleft()
  453. self._buffer_size -= len(data)
  454. if self._address is not None:
  455. self._write_fut = self._loop._proactor.send(self._sock,
  456. data)
  457. else:
  458. self._write_fut = self._loop._proactor.sendto(self._sock,
  459. data,
  460. addr=addr)
  461. except OSError as exc:
  462. self._protocol.error_received(exc)
  463. except Exception as exc:
  464. self._fatal_error(exc, 'Fatal write error on datagram transport')
  465. else:
  466. self._write_fut.add_done_callback(self._loop_writing)
  467. self._maybe_resume_protocol()
  468. def _loop_reading(self, fut=None):
  469. data = None
  470. try:
  471. if self._conn_lost:
  472. return
  473. assert self._read_fut is fut or (self._read_fut is None and
  474. self._closing)
  475. self._read_fut = None
  476. if fut is not None:
  477. res = fut.result()
  478. if self._closing:
  479. # since close() has been called we ignore any read data
  480. data = None
  481. return
  482. if self._address is not None:
  483. data, addr = res, self._address
  484. else:
  485. data, addr = res
  486. if self._conn_lost:
  487. return
  488. if self._address is not None:
  489. self._read_fut = self._loop._proactor.recv(self._sock,
  490. self.max_size)
  491. else:
  492. self._read_fut = self._loop._proactor.recvfrom(self._sock,
  493. self.max_size)
  494. except OSError as exc:
  495. self._protocol.error_received(exc)
  496. except exceptions.CancelledError:
  497. if not self._closing:
  498. raise
  499. else:
  500. if self._read_fut is not None:
  501. self._read_fut.add_done_callback(self._loop_reading)
  502. finally:
  503. if data:
  504. self._protocol.datagram_received(data, addr)
  505. class _ProactorDuplexPipeTransport(_ProactorReadPipeTransport,
  506. _ProactorBaseWritePipeTransport,
  507. transports.Transport):
  508. """Transport for duplex pipes."""
  509. def can_write_eof(self):
  510. return False
  511. def write_eof(self):
  512. raise NotImplementedError
  513. class _ProactorSocketTransport(_ProactorReadPipeTransport,
  514. _ProactorBaseWritePipeTransport,
  515. transports.Transport):
  516. """Transport for connected sockets."""
  517. _sendfile_compatible = constants._SendfileMode.TRY_NATIVE
  518. def __init__(self, loop, sock, protocol, waiter=None,
  519. extra=None, server=None):
  520. super().__init__(loop, sock, protocol, waiter, extra, server)
  521. base_events._set_nodelay(sock)
  522. def _set_extra(self, sock):
  523. _set_socket_extra(self, sock)
  524. def can_write_eof(self):
  525. return True
  526. def write_eof(self):
  527. if self._closing or self._eof_written:
  528. return
  529. self._eof_written = True
  530. if self._write_fut is None:
  531. self._sock.shutdown(socket.SHUT_WR)
  532. class BaseProactorEventLoop(base_events.BaseEventLoop):
  533. def __init__(self, proactor):
  534. super().__init__()
  535. logger.debug('Using proactor: %s', proactor.__class__.__name__)
  536. self._proactor = proactor
  537. self._selector = proactor # convenient alias
  538. self._self_reading_future = None
  539. self._accept_futures = {} # socket file descriptor => Future
  540. proactor.set_loop(self)
  541. self._make_self_pipe()
  542. if threading.current_thread() is threading.main_thread():
  543. # wakeup fd can only be installed to a file descriptor from the main thread
  544. signal.set_wakeup_fd(self._csock.fileno())
  545. def _make_socket_transport(self, sock, protocol, waiter=None,
  546. extra=None, server=None):
  547. return _ProactorSocketTransport(self, sock, protocol, waiter,
  548. extra, server)
  549. def _make_ssl_transport(
  550. self, rawsock, protocol, sslcontext, waiter=None,
  551. *, server_side=False, server_hostname=None,
  552. extra=None, server=None,
  553. ssl_handshake_timeout=None,
  554. ssl_shutdown_timeout=None):
  555. ssl_protocol = sslproto.SSLProtocol(
  556. self, protocol, sslcontext, waiter,
  557. server_side, server_hostname,
  558. ssl_handshake_timeout=ssl_handshake_timeout,
  559. ssl_shutdown_timeout=ssl_shutdown_timeout)
  560. _ProactorSocketTransport(self, rawsock, ssl_protocol,
  561. extra=extra, server=server)
  562. return ssl_protocol._app_transport
  563. def _make_datagram_transport(self, sock, protocol,
  564. address=None, waiter=None, extra=None):
  565. return _ProactorDatagramTransport(self, sock, protocol, address,
  566. waiter, extra)
  567. def _make_duplex_pipe_transport(self, sock, protocol, waiter=None,
  568. extra=None):
  569. return _ProactorDuplexPipeTransport(self,
  570. sock, protocol, waiter, extra)
  571. def _make_read_pipe_transport(self, sock, protocol, waiter=None,
  572. extra=None):
  573. return _ProactorReadPipeTransport(self, sock, protocol, waiter, extra)
  574. def _make_write_pipe_transport(self, sock, protocol, waiter=None,
  575. extra=None):
  576. # We want connection_lost() to be called when other end closes
  577. return _ProactorWritePipeTransport(self,
  578. sock, protocol, waiter, extra)
  579. def close(self):
  580. if self.is_running():
  581. raise RuntimeError("Cannot close a running event loop")
  582. if self.is_closed():
  583. return
  584. if threading.current_thread() is threading.main_thread():
  585. signal.set_wakeup_fd(-1)
  586. # Call these methods before closing the event loop (before calling
  587. # BaseEventLoop.close), because they can schedule callbacks with
  588. # call_soon(), which is forbidden when the event loop is closed.
  589. self._stop_accept_futures()
  590. self._close_self_pipe()
  591. self._proactor.close()
  592. self._proactor = None
  593. self._selector = None
  594. # Close the event loop
  595. super().close()
  596. async def sock_recv(self, sock, n):
  597. return await self._proactor.recv(sock, n)
  598. async def sock_recv_into(self, sock, buf):
  599. return await self._proactor.recv_into(sock, buf)
  600. async def sock_recvfrom(self, sock, bufsize):
  601. return await self._proactor.recvfrom(sock, bufsize)
  602. async def sock_recvfrom_into(self, sock, buf, nbytes=0):
  603. if not nbytes:
  604. nbytes = len(buf)
  605. return await self._proactor.recvfrom_into(sock, buf, nbytes)
  606. async def sock_sendall(self, sock, data):
  607. return await self._proactor.send(sock, data)
  608. async def sock_sendto(self, sock, data, address):
  609. return await self._proactor.sendto(sock, data, 0, address)
  610. async def sock_connect(self, sock, address):
  611. return await self._proactor.connect(sock, address)
  612. async def sock_accept(self, sock):
  613. return await self._proactor.accept(sock)
  614. async def _sock_sendfile_native(self, sock, file, offset, count):
  615. try:
  616. fileno = file.fileno()
  617. except (AttributeError, io.UnsupportedOperation) as err:
  618. raise exceptions.SendfileNotAvailableError("not a regular file")
  619. try:
  620. fsize = os.fstat(fileno).st_size
  621. except OSError:
  622. raise exceptions.SendfileNotAvailableError("not a regular file")
  623. blocksize = count if count else fsize
  624. if not blocksize:
  625. return 0 # empty file
  626. blocksize = min(blocksize, 0xffff_ffff)
  627. end_pos = min(offset + count, fsize) if count else fsize
  628. offset = min(offset, fsize)
  629. total_sent = 0
  630. try:
  631. while True:
  632. blocksize = min(end_pos - offset, blocksize)
  633. if blocksize <= 0:
  634. return total_sent
  635. await self._proactor.sendfile(sock, file, offset, blocksize)
  636. offset += blocksize
  637. total_sent += blocksize
  638. finally:
  639. if total_sent > 0:
  640. file.seek(offset)
  641. async def _sendfile_native(self, transp, file, offset, count):
  642. resume_reading = transp.is_reading()
  643. transp.pause_reading()
  644. await transp._make_empty_waiter()
  645. try:
  646. return await self.sock_sendfile(transp._sock, file, offset, count,
  647. fallback=False)
  648. finally:
  649. transp._reset_empty_waiter()
  650. if resume_reading:
  651. transp.resume_reading()
  652. def _close_self_pipe(self):
  653. if self._self_reading_future is not None:
  654. self._self_reading_future.cancel()
  655. self._self_reading_future = None
  656. self._ssock.close()
  657. self._ssock = None
  658. self._csock.close()
  659. self._csock = None
  660. self._internal_fds -= 1
  661. def _make_self_pipe(self):
  662. # A self-socket, really. :-)
  663. self._ssock, self._csock = socket.socketpair()
  664. self._ssock.setblocking(False)
  665. self._csock.setblocking(False)
  666. self._internal_fds += 1
  667. def _loop_self_reading(self, f=None):
  668. try:
  669. if f is not None:
  670. f.result() # may raise
  671. if self._self_reading_future is not f:
  672. # When we scheduled this Future, we assigned it to
  673. # _self_reading_future. If it's not there now, something has
  674. # tried to cancel the loop while this callback was still in the
  675. # queue (see windows_events.ProactorEventLoop.run_forever). In
  676. # that case stop here instead of continuing to schedule a new
  677. # iteration.
  678. return
  679. f = self._proactor.recv(self._ssock, 4096)
  680. except exceptions.CancelledError:
  681. # _close_self_pipe() has been called, stop waiting for data
  682. return
  683. except (SystemExit, KeyboardInterrupt):
  684. raise
  685. except BaseException as exc:
  686. self.call_exception_handler({
  687. 'message': 'Error on reading from the event loop self pipe',
  688. 'exception': exc,
  689. 'loop': self,
  690. })
  691. else:
  692. self._self_reading_future = f
  693. f.add_done_callback(self._loop_self_reading)
  694. def _write_to_self(self):
  695. # This may be called from a different thread, possibly after
  696. # _close_self_pipe() has been called or even while it is
  697. # running. Guard for self._csock being None or closed. When
  698. # a socket is closed, send() raises OSError (with errno set to
  699. # EBADF, but let's not rely on the exact error code).
  700. csock = self._csock
  701. if csock is None:
  702. return
  703. try:
  704. csock.send(b'\0')
  705. except OSError:
  706. if self._debug:
  707. logger.debug("Fail to write a null byte into the "
  708. "self-pipe socket",
  709. exc_info=True)
  710. def _start_serving(self, protocol_factory, sock,
  711. sslcontext=None, server=None, backlog=100,
  712. ssl_handshake_timeout=None,
  713. ssl_shutdown_timeout=None):
  714. def loop(f=None):
  715. try:
  716. if f is not None:
  717. conn, addr = f.result()
  718. if self._debug:
  719. logger.debug("%r got a new connection from %r: %r",
  720. server, addr, conn)
  721. protocol = protocol_factory()
  722. if sslcontext is not None:
  723. self._make_ssl_transport(
  724. conn, protocol, sslcontext, server_side=True,
  725. extra={'peername': addr}, server=server,
  726. ssl_handshake_timeout=ssl_handshake_timeout,
  727. ssl_shutdown_timeout=ssl_shutdown_timeout)
  728. else:
  729. self._make_socket_transport(
  730. conn, protocol,
  731. extra={'peername': addr}, server=server)
  732. if self.is_closed():
  733. return
  734. f = self._proactor.accept(sock)
  735. except OSError as exc:
  736. if sock.fileno() != -1:
  737. self.call_exception_handler({
  738. 'message': 'Accept failed on a socket',
  739. 'exception': exc,
  740. 'socket': trsock.TransportSocket(sock),
  741. })
  742. sock.close()
  743. elif self._debug:
  744. logger.debug("Accept failed on socket %r",
  745. sock, exc_info=True)
  746. except exceptions.CancelledError:
  747. sock.close()
  748. else:
  749. self._accept_futures[sock.fileno()] = f
  750. f.add_done_callback(loop)
  751. self.call_soon(loop)
  752. def _process_events(self, event_list):
  753. # Events are processed in the IocpProactor._poll() method
  754. pass
  755. def _stop_accept_futures(self):
  756. for future in self._accept_futures.values():
  757. future.cancel()
  758. self._accept_futures.clear()
  759. def _stop_serving(self, sock):
  760. future = self._accept_futures.pop(sock.fileno(), None)
  761. if future:
  762. future.cancel()
  763. self._proactor._stop_serving(sock)
  764. sock.close()