windows_events.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. """Selector and proactor event loops for Windows."""
  2. import sys
  3. if sys.platform != 'win32': # pragma: no cover
  4. raise ImportError('win32 only')
  5. import _overlapped
  6. import _winapi
  7. import errno
  8. import math
  9. import msvcrt
  10. import socket
  11. import struct
  12. import time
  13. import weakref
  14. from . import events
  15. from . import base_subprocess
  16. from . import futures
  17. from . import exceptions
  18. from . import proactor_events
  19. from . import selector_events
  20. from . import tasks
  21. from . import windows_utils
  22. from .log import logger
  23. __all__ = (
  24. 'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
  25. 'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy',
  26. 'WindowsProactorEventLoopPolicy',
  27. )
  28. NULL = _winapi.NULL
  29. INFINITE = _winapi.INFINITE
  30. ERROR_CONNECTION_REFUSED = 1225
  31. ERROR_CONNECTION_ABORTED = 1236
  32. # Initial delay in seconds for connect_pipe() before retrying to connect
  33. CONNECT_PIPE_INIT_DELAY = 0.001
  34. # Maximum delay in seconds for connect_pipe() before retrying to connect
  35. CONNECT_PIPE_MAX_DELAY = 0.100
  36. class _OverlappedFuture(futures.Future):
  37. """Subclass of Future which represents an overlapped operation.
  38. Cancelling it will immediately cancel the overlapped operation.
  39. """
  40. def __init__(self, ov, *, loop=None):
  41. super().__init__(loop=loop)
  42. if self._source_traceback:
  43. del self._source_traceback[-1]
  44. self._ov = ov
  45. def _repr_info(self):
  46. info = super()._repr_info()
  47. if self._ov is not None:
  48. state = 'pending' if self._ov.pending else 'completed'
  49. info.insert(1, f'overlapped=<{state}, {self._ov.address:#x}>')
  50. return info
  51. def _cancel_overlapped(self):
  52. if self._ov is None:
  53. return
  54. try:
  55. self._ov.cancel()
  56. except OSError as exc:
  57. context = {
  58. 'message': 'Cancelling an overlapped future failed',
  59. 'exception': exc,
  60. 'future': self,
  61. }
  62. if self._source_traceback:
  63. context['source_traceback'] = self._source_traceback
  64. self._loop.call_exception_handler(context)
  65. self._ov = None
  66. def cancel(self, msg=None):
  67. self._cancel_overlapped()
  68. return super().cancel(msg=msg)
  69. def set_exception(self, exception):
  70. super().set_exception(exception)
  71. self._cancel_overlapped()
  72. def set_result(self, result):
  73. super().set_result(result)
  74. self._ov = None
  75. class _BaseWaitHandleFuture(futures.Future):
  76. """Subclass of Future which represents a wait handle."""
  77. def __init__(self, ov, handle, wait_handle, *, loop=None):
  78. super().__init__(loop=loop)
  79. if self._source_traceback:
  80. del self._source_traceback[-1]
  81. # Keep a reference to the Overlapped object to keep it alive until the
  82. # wait is unregistered
  83. self._ov = ov
  84. self._handle = handle
  85. self._wait_handle = wait_handle
  86. # Should we call UnregisterWaitEx() if the wait completes
  87. # or is cancelled?
  88. self._registered = True
  89. def _poll(self):
  90. # non-blocking wait: use a timeout of 0 millisecond
  91. return (_winapi.WaitForSingleObject(self._handle, 0) ==
  92. _winapi.WAIT_OBJECT_0)
  93. def _repr_info(self):
  94. info = super()._repr_info()
  95. info.append(f'handle={self._handle:#x}')
  96. if self._handle is not None:
  97. state = 'signaled' if self._poll() else 'waiting'
  98. info.append(state)
  99. if self._wait_handle is not None:
  100. info.append(f'wait_handle={self._wait_handle:#x}')
  101. return info
  102. def _unregister_wait_cb(self, fut):
  103. # The wait was unregistered: it's not safe to destroy the Overlapped
  104. # object
  105. self._ov = None
  106. def _unregister_wait(self):
  107. if not self._registered:
  108. return
  109. self._registered = False
  110. wait_handle = self._wait_handle
  111. self._wait_handle = None
  112. try:
  113. _overlapped.UnregisterWait(wait_handle)
  114. except OSError as exc:
  115. if exc.winerror != _overlapped.ERROR_IO_PENDING:
  116. context = {
  117. 'message': 'Failed to unregister the wait handle',
  118. 'exception': exc,
  119. 'future': self,
  120. }
  121. if self._source_traceback:
  122. context['source_traceback'] = self._source_traceback
  123. self._loop.call_exception_handler(context)
  124. return
  125. # ERROR_IO_PENDING means that the unregister is pending
  126. self._unregister_wait_cb(None)
  127. def cancel(self, msg=None):
  128. self._unregister_wait()
  129. return super().cancel(msg=msg)
  130. def set_exception(self, exception):
  131. self._unregister_wait()
  132. super().set_exception(exception)
  133. def set_result(self, result):
  134. self._unregister_wait()
  135. super().set_result(result)
  136. class _WaitCancelFuture(_BaseWaitHandleFuture):
  137. """Subclass of Future which represents a wait for the cancellation of a
  138. _WaitHandleFuture using an event.
  139. """
  140. def __init__(self, ov, event, wait_handle, *, loop=None):
  141. super().__init__(ov, event, wait_handle, loop=loop)
  142. self._done_callback = None
  143. def cancel(self):
  144. raise RuntimeError("_WaitCancelFuture must not be cancelled")
  145. def set_result(self, result):
  146. super().set_result(result)
  147. if self._done_callback is not None:
  148. self._done_callback(self)
  149. def set_exception(self, exception):
  150. super().set_exception(exception)
  151. if self._done_callback is not None:
  152. self._done_callback(self)
  153. class _WaitHandleFuture(_BaseWaitHandleFuture):
  154. def __init__(self, ov, handle, wait_handle, proactor, *, loop=None):
  155. super().__init__(ov, handle, wait_handle, loop=loop)
  156. self._proactor = proactor
  157. self._unregister_proactor = True
  158. self._event = _overlapped.CreateEvent(None, True, False, None)
  159. self._event_fut = None
  160. def _unregister_wait_cb(self, fut):
  161. if self._event is not None:
  162. _winapi.CloseHandle(self._event)
  163. self._event = None
  164. self._event_fut = None
  165. # If the wait was cancelled, the wait may never be signalled, so
  166. # it's required to unregister it. Otherwise, IocpProactor.close() will
  167. # wait forever for an event which will never come.
  168. #
  169. # If the IocpProactor already received the event, it's safe to call
  170. # _unregister() because we kept a reference to the Overlapped object
  171. # which is used as a unique key.
  172. self._proactor._unregister(self._ov)
  173. self._proactor = None
  174. super()._unregister_wait_cb(fut)
  175. def _unregister_wait(self):
  176. if not self._registered:
  177. return
  178. self._registered = False
  179. wait_handle = self._wait_handle
  180. self._wait_handle = None
  181. try:
  182. _overlapped.UnregisterWaitEx(wait_handle, self._event)
  183. except OSError as exc:
  184. if exc.winerror != _overlapped.ERROR_IO_PENDING:
  185. context = {
  186. 'message': 'Failed to unregister the wait handle',
  187. 'exception': exc,
  188. 'future': self,
  189. }
  190. if self._source_traceback:
  191. context['source_traceback'] = self._source_traceback
  192. self._loop.call_exception_handler(context)
  193. return
  194. # ERROR_IO_PENDING is not an error, the wait was unregistered
  195. self._event_fut = self._proactor._wait_cancel(self._event,
  196. self._unregister_wait_cb)
  197. class PipeServer(object):
  198. """Class representing a pipe server.
  199. This is much like a bound, listening socket.
  200. """
  201. def __init__(self, address):
  202. self._address = address
  203. self._free_instances = weakref.WeakSet()
  204. # initialize the pipe attribute before calling _server_pipe_handle()
  205. # because this function can raise an exception and the destructor calls
  206. # the close() method
  207. self._pipe = None
  208. self._accept_pipe_future = None
  209. self._pipe = self._server_pipe_handle(True)
  210. def _get_unconnected_pipe(self):
  211. # Create new instance and return previous one. This ensures
  212. # that (until the server is closed) there is always at least
  213. # one pipe handle for address. Therefore if a client attempt
  214. # to connect it will not fail with FileNotFoundError.
  215. tmp, self._pipe = self._pipe, self._server_pipe_handle(False)
  216. return tmp
  217. def _server_pipe_handle(self, first):
  218. # Return a wrapper for a new pipe handle.
  219. if self.closed():
  220. return None
  221. flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED
  222. if first:
  223. flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE
  224. h = _winapi.CreateNamedPipe(
  225. self._address, flags,
  226. _winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
  227. _winapi.PIPE_WAIT,
  228. _winapi.PIPE_UNLIMITED_INSTANCES,
  229. windows_utils.BUFSIZE, windows_utils.BUFSIZE,
  230. _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL)
  231. pipe = windows_utils.PipeHandle(h)
  232. self._free_instances.add(pipe)
  233. return pipe
  234. def closed(self):
  235. return (self._address is None)
  236. def close(self):
  237. if self._accept_pipe_future is not None:
  238. self._accept_pipe_future.cancel()
  239. self._accept_pipe_future = None
  240. # Close all instances which have not been connected to by a client.
  241. if self._address is not None:
  242. for pipe in self._free_instances:
  243. pipe.close()
  244. self._pipe = None
  245. self._address = None
  246. self._free_instances.clear()
  247. __del__ = close
  248. class _WindowsSelectorEventLoop(selector_events.BaseSelectorEventLoop):
  249. """Windows version of selector event loop."""
  250. class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
  251. """Windows version of proactor event loop using IOCP."""
  252. def __init__(self, proactor=None):
  253. if proactor is None:
  254. proactor = IocpProactor()
  255. super().__init__(proactor)
  256. def run_forever(self):
  257. try:
  258. assert self._self_reading_future is None
  259. self.call_soon(self._loop_self_reading)
  260. super().run_forever()
  261. finally:
  262. if self._self_reading_future is not None:
  263. ov = self._self_reading_future._ov
  264. self._self_reading_future.cancel()
  265. # self_reading_future was just cancelled so if it hasn't been
  266. # finished yet, it never will be (it's possible that it has
  267. # already finished and its callback is waiting in the queue,
  268. # where it could still happen if the event loop is restarted).
  269. # Unregister it otherwise IocpProactor.close will wait for it
  270. # forever
  271. if ov is not None:
  272. self._proactor._unregister(ov)
  273. self._self_reading_future = None
  274. async def create_pipe_connection(self, protocol_factory, address):
  275. f = self._proactor.connect_pipe(address)
  276. pipe = await f
  277. protocol = protocol_factory()
  278. trans = self._make_duplex_pipe_transport(pipe, protocol,
  279. extra={'addr': address})
  280. return trans, protocol
  281. async def start_serving_pipe(self, protocol_factory, address):
  282. server = PipeServer(address)
  283. def loop_accept_pipe(f=None):
  284. pipe = None
  285. try:
  286. if f:
  287. pipe = f.result()
  288. server._free_instances.discard(pipe)
  289. if server.closed():
  290. # A client connected before the server was closed:
  291. # drop the client (close the pipe) and exit
  292. pipe.close()
  293. return
  294. protocol = protocol_factory()
  295. self._make_duplex_pipe_transport(
  296. pipe, protocol, extra={'addr': address})
  297. pipe = server._get_unconnected_pipe()
  298. if pipe is None:
  299. return
  300. f = self._proactor.accept_pipe(pipe)
  301. except BrokenPipeError:
  302. if pipe and pipe.fileno() != -1:
  303. pipe.close()
  304. self.call_soon(loop_accept_pipe)
  305. except OSError as exc:
  306. if pipe and pipe.fileno() != -1:
  307. self.call_exception_handler({
  308. 'message': 'Pipe accept failed',
  309. 'exception': exc,
  310. 'pipe': pipe,
  311. })
  312. pipe.close()
  313. elif self._debug:
  314. logger.warning("Accept pipe failed on pipe %r",
  315. pipe, exc_info=True)
  316. self.call_soon(loop_accept_pipe)
  317. except exceptions.CancelledError:
  318. if pipe:
  319. pipe.close()
  320. else:
  321. server._accept_pipe_future = f
  322. f.add_done_callback(loop_accept_pipe)
  323. self.call_soon(loop_accept_pipe)
  324. return [server]
  325. async def _make_subprocess_transport(self, protocol, args, shell,
  326. stdin, stdout, stderr, bufsize,
  327. extra=None, **kwargs):
  328. waiter = self.create_future()
  329. transp = _WindowsSubprocessTransport(self, protocol, args, shell,
  330. stdin, stdout, stderr, bufsize,
  331. waiter=waiter, extra=extra,
  332. **kwargs)
  333. try:
  334. await waiter
  335. except (SystemExit, KeyboardInterrupt):
  336. raise
  337. except BaseException:
  338. transp.close()
  339. await transp._wait()
  340. raise
  341. return transp
  342. class IocpProactor:
  343. """Proactor implementation using IOCP."""
  344. def __init__(self, concurrency=INFINITE):
  345. self._loop = None
  346. self._results = []
  347. self._iocp = _overlapped.CreateIoCompletionPort(
  348. _overlapped.INVALID_HANDLE_VALUE, NULL, 0, concurrency)
  349. self._cache = {}
  350. self._registered = weakref.WeakSet()
  351. self._unregistered = []
  352. self._stopped_serving = weakref.WeakSet()
  353. def _check_closed(self):
  354. if self._iocp is None:
  355. raise RuntimeError('IocpProactor is closed')
  356. def __repr__(self):
  357. info = ['overlapped#=%s' % len(self._cache),
  358. 'result#=%s' % len(self._results)]
  359. if self._iocp is None:
  360. info.append('closed')
  361. return '<%s %s>' % (self.__class__.__name__, " ".join(info))
  362. def set_loop(self, loop):
  363. self._loop = loop
  364. def select(self, timeout=None):
  365. if not self._results:
  366. self._poll(timeout)
  367. tmp = self._results
  368. self._results = []
  369. try:
  370. return tmp
  371. finally:
  372. # Needed to break cycles when an exception occurs.
  373. tmp = None
  374. def _result(self, value):
  375. fut = self._loop.create_future()
  376. fut.set_result(value)
  377. return fut
  378. def recv(self, conn, nbytes, flags=0):
  379. self._register_with_iocp(conn)
  380. ov = _overlapped.Overlapped(NULL)
  381. try:
  382. if isinstance(conn, socket.socket):
  383. ov.WSARecv(conn.fileno(), nbytes, flags)
  384. else:
  385. ov.ReadFile(conn.fileno(), nbytes)
  386. except BrokenPipeError:
  387. return self._result(b'')
  388. def finish_recv(trans, key, ov):
  389. try:
  390. return ov.getresult()
  391. except OSError as exc:
  392. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  393. _overlapped.ERROR_OPERATION_ABORTED):
  394. raise ConnectionResetError(*exc.args)
  395. else:
  396. raise
  397. return self._register(ov, conn, finish_recv)
  398. def recv_into(self, conn, buf, flags=0):
  399. self._register_with_iocp(conn)
  400. ov = _overlapped.Overlapped(NULL)
  401. try:
  402. if isinstance(conn, socket.socket):
  403. ov.WSARecvInto(conn.fileno(), buf, flags)
  404. else:
  405. ov.ReadFileInto(conn.fileno(), buf)
  406. except BrokenPipeError:
  407. return self._result(0)
  408. def finish_recv(trans, key, ov):
  409. try:
  410. return ov.getresult()
  411. except OSError as exc:
  412. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  413. _overlapped.ERROR_OPERATION_ABORTED):
  414. raise ConnectionResetError(*exc.args)
  415. else:
  416. raise
  417. return self._register(ov, conn, finish_recv)
  418. def recvfrom(self, conn, nbytes, flags=0):
  419. self._register_with_iocp(conn)
  420. ov = _overlapped.Overlapped(NULL)
  421. try:
  422. ov.WSARecvFrom(conn.fileno(), nbytes, flags)
  423. except BrokenPipeError:
  424. return self._result((b'', None))
  425. def finish_recv(trans, key, ov):
  426. try:
  427. return ov.getresult()
  428. except OSError as exc:
  429. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  430. _overlapped.ERROR_OPERATION_ABORTED):
  431. raise ConnectionResetError(*exc.args)
  432. else:
  433. raise
  434. return self._register(ov, conn, finish_recv)
  435. def recvfrom_into(self, conn, buf, flags=0):
  436. self._register_with_iocp(conn)
  437. ov = _overlapped.Overlapped(NULL)
  438. try:
  439. ov.WSARecvFromInto(conn.fileno(), buf, flags)
  440. except BrokenPipeError:
  441. return self._result((0, None))
  442. def finish_recv(trans, key, ov):
  443. try:
  444. return ov.getresult()
  445. except OSError as exc:
  446. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  447. _overlapped.ERROR_OPERATION_ABORTED):
  448. raise ConnectionResetError(*exc.args)
  449. else:
  450. raise
  451. return self._register(ov, conn, finish_recv)
  452. def sendto(self, conn, buf, flags=0, addr=None):
  453. self._register_with_iocp(conn)
  454. ov = _overlapped.Overlapped(NULL)
  455. ov.WSASendTo(conn.fileno(), buf, flags, addr)
  456. def finish_send(trans, key, ov):
  457. try:
  458. return ov.getresult()
  459. except OSError as exc:
  460. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  461. _overlapped.ERROR_OPERATION_ABORTED):
  462. raise ConnectionResetError(*exc.args)
  463. else:
  464. raise
  465. return self._register(ov, conn, finish_send)
  466. def send(self, conn, buf, flags=0):
  467. self._register_with_iocp(conn)
  468. ov = _overlapped.Overlapped(NULL)
  469. if isinstance(conn, socket.socket):
  470. ov.WSASend(conn.fileno(), buf, flags)
  471. else:
  472. ov.WriteFile(conn.fileno(), buf)
  473. def finish_send(trans, key, ov):
  474. try:
  475. return ov.getresult()
  476. except OSError as exc:
  477. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  478. _overlapped.ERROR_OPERATION_ABORTED):
  479. raise ConnectionResetError(*exc.args)
  480. else:
  481. raise
  482. return self._register(ov, conn, finish_send)
  483. def accept(self, listener):
  484. self._register_with_iocp(listener)
  485. conn = self._get_accept_socket(listener.family)
  486. ov = _overlapped.Overlapped(NULL)
  487. ov.AcceptEx(listener.fileno(), conn.fileno())
  488. def finish_accept(trans, key, ov):
  489. ov.getresult()
  490. # Use SO_UPDATE_ACCEPT_CONTEXT so getsockname() etc work.
  491. buf = struct.pack('@P', listener.fileno())
  492. conn.setsockopt(socket.SOL_SOCKET,
  493. _overlapped.SO_UPDATE_ACCEPT_CONTEXT, buf)
  494. conn.settimeout(listener.gettimeout())
  495. return conn, conn.getpeername()
  496. async def accept_coro(future, conn):
  497. # Coroutine closing the accept socket if the future is cancelled
  498. try:
  499. await future
  500. except exceptions.CancelledError:
  501. conn.close()
  502. raise
  503. future = self._register(ov, listener, finish_accept)
  504. coro = accept_coro(future, conn)
  505. tasks.ensure_future(coro, loop=self._loop)
  506. return future
  507. def connect(self, conn, address):
  508. if conn.type == socket.SOCK_DGRAM:
  509. # WSAConnect will complete immediately for UDP sockets so we don't
  510. # need to register any IOCP operation
  511. _overlapped.WSAConnect(conn.fileno(), address)
  512. fut = self._loop.create_future()
  513. fut.set_result(None)
  514. return fut
  515. self._register_with_iocp(conn)
  516. # The socket needs to be locally bound before we call ConnectEx().
  517. try:
  518. _overlapped.BindLocal(conn.fileno(), conn.family)
  519. except OSError as e:
  520. if e.winerror != errno.WSAEINVAL:
  521. raise
  522. # Probably already locally bound; check using getsockname().
  523. if conn.getsockname()[1] == 0:
  524. raise
  525. ov = _overlapped.Overlapped(NULL)
  526. ov.ConnectEx(conn.fileno(), address)
  527. def finish_connect(trans, key, ov):
  528. ov.getresult()
  529. # Use SO_UPDATE_CONNECT_CONTEXT so getsockname() etc work.
  530. conn.setsockopt(socket.SOL_SOCKET,
  531. _overlapped.SO_UPDATE_CONNECT_CONTEXT, 0)
  532. return conn
  533. return self._register(ov, conn, finish_connect)
  534. def sendfile(self, sock, file, offset, count):
  535. self._register_with_iocp(sock)
  536. ov = _overlapped.Overlapped(NULL)
  537. offset_low = offset & 0xffff_ffff
  538. offset_high = (offset >> 32) & 0xffff_ffff
  539. ov.TransmitFile(sock.fileno(),
  540. msvcrt.get_osfhandle(file.fileno()),
  541. offset_low, offset_high,
  542. count, 0, 0)
  543. def finish_sendfile(trans, key, ov):
  544. try:
  545. return ov.getresult()
  546. except OSError as exc:
  547. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  548. _overlapped.ERROR_OPERATION_ABORTED):
  549. raise ConnectionResetError(*exc.args)
  550. else:
  551. raise
  552. return self._register(ov, sock, finish_sendfile)
  553. def accept_pipe(self, pipe):
  554. self._register_with_iocp(pipe)
  555. ov = _overlapped.Overlapped(NULL)
  556. connected = ov.ConnectNamedPipe(pipe.fileno())
  557. if connected:
  558. # ConnectNamePipe() failed with ERROR_PIPE_CONNECTED which means
  559. # that the pipe is connected. There is no need to wait for the
  560. # completion of the connection.
  561. return self._result(pipe)
  562. def finish_accept_pipe(trans, key, ov):
  563. ov.getresult()
  564. return pipe
  565. return self._register(ov, pipe, finish_accept_pipe)
  566. async def connect_pipe(self, address):
  567. delay = CONNECT_PIPE_INIT_DELAY
  568. while True:
  569. # Unfortunately there is no way to do an overlapped connect to
  570. # a pipe. Call CreateFile() in a loop until it doesn't fail with
  571. # ERROR_PIPE_BUSY.
  572. try:
  573. handle = _overlapped.ConnectPipe(address)
  574. break
  575. except OSError as exc:
  576. if exc.winerror != _overlapped.ERROR_PIPE_BUSY:
  577. raise
  578. # ConnectPipe() failed with ERROR_PIPE_BUSY: retry later
  579. delay = min(delay * 2, CONNECT_PIPE_MAX_DELAY)
  580. await tasks.sleep(delay)
  581. return windows_utils.PipeHandle(handle)
  582. def wait_for_handle(self, handle, timeout=None):
  583. """Wait for a handle.
  584. Return a Future object. The result of the future is True if the wait
  585. completed, or False if the wait did not complete (on timeout).
  586. """
  587. return self._wait_for_handle(handle, timeout, False)
  588. def _wait_cancel(self, event, done_callback):
  589. fut = self._wait_for_handle(event, None, True)
  590. # add_done_callback() cannot be used because the wait may only complete
  591. # in IocpProactor.close(), while the event loop is not running.
  592. fut._done_callback = done_callback
  593. return fut
  594. def _wait_for_handle(self, handle, timeout, _is_cancel):
  595. self._check_closed()
  596. if timeout is None:
  597. ms = _winapi.INFINITE
  598. else:
  599. # RegisterWaitForSingleObject() has a resolution of 1 millisecond,
  600. # round away from zero to wait *at least* timeout seconds.
  601. ms = math.ceil(timeout * 1e3)
  602. # We only create ov so we can use ov.address as a key for the cache.
  603. ov = _overlapped.Overlapped(NULL)
  604. wait_handle = _overlapped.RegisterWaitWithQueue(
  605. handle, self._iocp, ov.address, ms)
  606. if _is_cancel:
  607. f = _WaitCancelFuture(ov, handle, wait_handle, loop=self._loop)
  608. else:
  609. f = _WaitHandleFuture(ov, handle, wait_handle, self,
  610. loop=self._loop)
  611. if f._source_traceback:
  612. del f._source_traceback[-1]
  613. def finish_wait_for_handle(trans, key, ov):
  614. # Note that this second wait means that we should only use
  615. # this with handles types where a successful wait has no
  616. # effect. So events or processes are all right, but locks
  617. # or semaphores are not. Also note if the handle is
  618. # signalled and then quickly reset, then we may return
  619. # False even though we have not timed out.
  620. return f._poll()
  621. self._cache[ov.address] = (f, ov, 0, finish_wait_for_handle)
  622. return f
  623. def _register_with_iocp(self, obj):
  624. # To get notifications of finished ops on this objects sent to the
  625. # completion port, were must register the handle.
  626. if obj not in self._registered:
  627. self._registered.add(obj)
  628. _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
  629. # XXX We could also use SetFileCompletionNotificationModes()
  630. # to avoid sending notifications to completion port of ops
  631. # that succeed immediately.
  632. def _register(self, ov, obj, callback):
  633. self._check_closed()
  634. # Return a future which will be set with the result of the
  635. # operation when it completes. The future's value is actually
  636. # the value returned by callback().
  637. f = _OverlappedFuture(ov, loop=self._loop)
  638. if f._source_traceback:
  639. del f._source_traceback[-1]
  640. if not ov.pending:
  641. # The operation has completed, so no need to postpone the
  642. # work. We cannot take this short cut if we need the
  643. # NumberOfBytes, CompletionKey values returned by
  644. # PostQueuedCompletionStatus().
  645. try:
  646. value = callback(None, None, ov)
  647. except OSError as e:
  648. f.set_exception(e)
  649. else:
  650. f.set_result(value)
  651. # Even if GetOverlappedResult() was called, we have to wait for the
  652. # notification of the completion in GetQueuedCompletionStatus().
  653. # Register the overlapped operation to keep a reference to the
  654. # OVERLAPPED object, otherwise the memory is freed and Windows may
  655. # read uninitialized memory.
  656. # Register the overlapped operation for later. Note that
  657. # we only store obj to prevent it from being garbage
  658. # collected too early.
  659. self._cache[ov.address] = (f, ov, obj, callback)
  660. return f
  661. def _unregister(self, ov):
  662. """Unregister an overlapped object.
  663. Call this method when its future has been cancelled. The event can
  664. already be signalled (pending in the proactor event queue). It is also
  665. safe if the event is never signalled (because it was cancelled).
  666. """
  667. self._check_closed()
  668. self._unregistered.append(ov)
  669. def _get_accept_socket(self, family):
  670. s = socket.socket(family)
  671. s.settimeout(0)
  672. return s
  673. def _poll(self, timeout=None):
  674. if timeout is None:
  675. ms = INFINITE
  676. elif timeout < 0:
  677. raise ValueError("negative timeout")
  678. else:
  679. # GetQueuedCompletionStatus() has a resolution of 1 millisecond,
  680. # round away from zero to wait *at least* timeout seconds.
  681. ms = math.ceil(timeout * 1e3)
  682. if ms >= INFINITE:
  683. raise ValueError("timeout too big")
  684. while True:
  685. status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
  686. if status is None:
  687. break
  688. ms = 0
  689. err, transferred, key, address = status
  690. try:
  691. f, ov, obj, callback = self._cache.pop(address)
  692. except KeyError:
  693. if self._loop.get_debug():
  694. self._loop.call_exception_handler({
  695. 'message': ('GetQueuedCompletionStatus() returned an '
  696. 'unexpected event'),
  697. 'status': ('err=%s transferred=%s key=%#x address=%#x'
  698. % (err, transferred, key, address)),
  699. })
  700. # key is either zero, or it is used to return a pipe
  701. # handle which should be closed to avoid a leak.
  702. if key not in (0, _overlapped.INVALID_HANDLE_VALUE):
  703. _winapi.CloseHandle(key)
  704. continue
  705. if obj in self._stopped_serving:
  706. f.cancel()
  707. # Don't call the callback if _register() already read the result or
  708. # if the overlapped has been cancelled
  709. elif not f.done():
  710. try:
  711. value = callback(transferred, key, ov)
  712. except OSError as e:
  713. f.set_exception(e)
  714. self._results.append(f)
  715. else:
  716. f.set_result(value)
  717. self._results.append(f)
  718. finally:
  719. f = None
  720. # Remove unregistered futures
  721. for ov in self._unregistered:
  722. self._cache.pop(ov.address, None)
  723. self._unregistered.clear()
  724. def _stop_serving(self, obj):
  725. # obj is a socket or pipe handle. It will be closed in
  726. # BaseProactorEventLoop._stop_serving() which will make any
  727. # pending operations fail quickly.
  728. self._stopped_serving.add(obj)
  729. def close(self):
  730. if self._iocp is None:
  731. # already closed
  732. return
  733. # Cancel remaining registered operations.
  734. for fut, ov, obj, callback in list(self._cache.values()):
  735. if fut.cancelled():
  736. # Nothing to do with cancelled futures
  737. pass
  738. elif isinstance(fut, _WaitCancelFuture):
  739. # _WaitCancelFuture must not be cancelled
  740. pass
  741. else:
  742. try:
  743. fut.cancel()
  744. except OSError as exc:
  745. if self._loop is not None:
  746. context = {
  747. 'message': 'Cancelling a future failed',
  748. 'exception': exc,
  749. 'future': fut,
  750. }
  751. if fut._source_traceback:
  752. context['source_traceback'] = fut._source_traceback
  753. self._loop.call_exception_handler(context)
  754. # Wait until all cancelled overlapped complete: don't exit with running
  755. # overlapped to prevent a crash. Display progress every second if the
  756. # loop is still running.
  757. msg_update = 1.0
  758. start_time = time.monotonic()
  759. next_msg = start_time + msg_update
  760. while self._cache:
  761. if next_msg <= time.monotonic():
  762. logger.debug('%r is running after closing for %.1f seconds',
  763. self, time.monotonic() - start_time)
  764. next_msg = time.monotonic() + msg_update
  765. # handle a few events, or timeout
  766. self._poll(msg_update)
  767. self._results = []
  768. _winapi.CloseHandle(self._iocp)
  769. self._iocp = None
  770. def __del__(self):
  771. self.close()
  772. class _WindowsSubprocessTransport(base_subprocess.BaseSubprocessTransport):
  773. def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
  774. self._proc = windows_utils.Popen(
  775. args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
  776. bufsize=bufsize, **kwargs)
  777. def callback(f):
  778. returncode = self._proc.poll()
  779. self._process_exited(returncode)
  780. f = self._loop._proactor.wait_for_handle(int(self._proc._handle))
  781. f.add_done_callback(callback)
  782. SelectorEventLoop = _WindowsSelectorEventLoop
  783. class WindowsSelectorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
  784. _loop_factory = SelectorEventLoop
  785. class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
  786. _loop_factory = ProactorEventLoop
  787. DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy