windows_events.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. @staticmethod
  379. def finish_socket_func(trans, key, ov):
  380. try:
  381. return ov.getresult()
  382. except OSError as exc:
  383. if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
  384. _overlapped.ERROR_OPERATION_ABORTED):
  385. raise ConnectionResetError(*exc.args)
  386. else:
  387. raise
  388. def recv(self, conn, nbytes, flags=0):
  389. self._register_with_iocp(conn)
  390. ov = _overlapped.Overlapped(NULL)
  391. try:
  392. if isinstance(conn, socket.socket):
  393. ov.WSARecv(conn.fileno(), nbytes, flags)
  394. else:
  395. ov.ReadFile(conn.fileno(), nbytes)
  396. except BrokenPipeError:
  397. return self._result(b'')
  398. return self._register(ov, conn, self.finish_socket_func)
  399. def recv_into(self, conn, buf, flags=0):
  400. self._register_with_iocp(conn)
  401. ov = _overlapped.Overlapped(NULL)
  402. try:
  403. if isinstance(conn, socket.socket):
  404. ov.WSARecvInto(conn.fileno(), buf, flags)
  405. else:
  406. ov.ReadFileInto(conn.fileno(), buf)
  407. except BrokenPipeError:
  408. return self._result(0)
  409. return self._register(ov, conn, self.finish_socket_func)
  410. def recvfrom(self, conn, nbytes, flags=0):
  411. self._register_with_iocp(conn)
  412. ov = _overlapped.Overlapped(NULL)
  413. try:
  414. ov.WSARecvFrom(conn.fileno(), nbytes, flags)
  415. except BrokenPipeError:
  416. return self._result((b'', None))
  417. return self._register(ov, conn, self.finish_socket_func)
  418. def recvfrom_into(self, conn, buf, flags=0):
  419. self._register_with_iocp(conn)
  420. ov = _overlapped.Overlapped(NULL)
  421. try:
  422. ov.WSARecvFromInto(conn.fileno(), buf, flags)
  423. except BrokenPipeError:
  424. return self._result((0, 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 sendto(self, conn, buf, flags=0, addr=None):
  436. self._register_with_iocp(conn)
  437. ov = _overlapped.Overlapped(NULL)
  438. ov.WSASendTo(conn.fileno(), buf, flags, addr)
  439. return self._register(ov, conn, self.finish_socket_func)
  440. def send(self, conn, buf, flags=0):
  441. self._register_with_iocp(conn)
  442. ov = _overlapped.Overlapped(NULL)
  443. if isinstance(conn, socket.socket):
  444. ov.WSASend(conn.fileno(), buf, flags)
  445. else:
  446. ov.WriteFile(conn.fileno(), buf)
  447. return self._register(ov, conn, self.finish_socket_func)
  448. def accept(self, listener):
  449. self._register_with_iocp(listener)
  450. conn = self._get_accept_socket(listener.family)
  451. ov = _overlapped.Overlapped(NULL)
  452. ov.AcceptEx(listener.fileno(), conn.fileno())
  453. def finish_accept(trans, key, ov):
  454. ov.getresult()
  455. # Use SO_UPDATE_ACCEPT_CONTEXT so getsockname() etc work.
  456. buf = struct.pack('@P', listener.fileno())
  457. conn.setsockopt(socket.SOL_SOCKET,
  458. _overlapped.SO_UPDATE_ACCEPT_CONTEXT, buf)
  459. conn.settimeout(listener.gettimeout())
  460. return conn, conn.getpeername()
  461. async def accept_coro(future, conn):
  462. # Coroutine closing the accept socket if the future is cancelled
  463. try:
  464. await future
  465. except exceptions.CancelledError:
  466. conn.close()
  467. raise
  468. future = self._register(ov, listener, finish_accept)
  469. coro = accept_coro(future, conn)
  470. tasks.ensure_future(coro, loop=self._loop)
  471. return future
  472. def connect(self, conn, address):
  473. if conn.type == socket.SOCK_DGRAM:
  474. # WSAConnect will complete immediately for UDP sockets so we don't
  475. # need to register any IOCP operation
  476. _overlapped.WSAConnect(conn.fileno(), address)
  477. fut = self._loop.create_future()
  478. fut.set_result(None)
  479. return fut
  480. self._register_with_iocp(conn)
  481. # The socket needs to be locally bound before we call ConnectEx().
  482. try:
  483. _overlapped.BindLocal(conn.fileno(), conn.family)
  484. except OSError as e:
  485. if e.winerror != errno.WSAEINVAL:
  486. raise
  487. # Probably already locally bound; check using getsockname().
  488. if conn.getsockname()[1] == 0:
  489. raise
  490. ov = _overlapped.Overlapped(NULL)
  491. ov.ConnectEx(conn.fileno(), address)
  492. def finish_connect(trans, key, ov):
  493. ov.getresult()
  494. # Use SO_UPDATE_CONNECT_CONTEXT so getsockname() etc work.
  495. conn.setsockopt(socket.SOL_SOCKET,
  496. _overlapped.SO_UPDATE_CONNECT_CONTEXT, 0)
  497. return conn
  498. return self._register(ov, conn, finish_connect)
  499. def sendfile(self, sock, file, offset, count):
  500. self._register_with_iocp(sock)
  501. ov = _overlapped.Overlapped(NULL)
  502. offset_low = offset & 0xffff_ffff
  503. offset_high = (offset >> 32) & 0xffff_ffff
  504. ov.TransmitFile(sock.fileno(),
  505. msvcrt.get_osfhandle(file.fileno()),
  506. offset_low, offset_high,
  507. count, 0, 0)
  508. return self._register(ov, sock, self.finish_socket_func)
  509. def accept_pipe(self, pipe):
  510. self._register_with_iocp(pipe)
  511. ov = _overlapped.Overlapped(NULL)
  512. connected = ov.ConnectNamedPipe(pipe.fileno())
  513. if connected:
  514. # ConnectNamePipe() failed with ERROR_PIPE_CONNECTED which means
  515. # that the pipe is connected. There is no need to wait for the
  516. # completion of the connection.
  517. return self._result(pipe)
  518. def finish_accept_pipe(trans, key, ov):
  519. ov.getresult()
  520. return pipe
  521. return self._register(ov, pipe, finish_accept_pipe)
  522. async def connect_pipe(self, address):
  523. delay = CONNECT_PIPE_INIT_DELAY
  524. while True:
  525. # Unfortunately there is no way to do an overlapped connect to
  526. # a pipe. Call CreateFile() in a loop until it doesn't fail with
  527. # ERROR_PIPE_BUSY.
  528. try:
  529. handle = _overlapped.ConnectPipe(address)
  530. break
  531. except OSError as exc:
  532. if exc.winerror != _overlapped.ERROR_PIPE_BUSY:
  533. raise
  534. # ConnectPipe() failed with ERROR_PIPE_BUSY: retry later
  535. delay = min(delay * 2, CONNECT_PIPE_MAX_DELAY)
  536. await tasks.sleep(delay)
  537. return windows_utils.PipeHandle(handle)
  538. def wait_for_handle(self, handle, timeout=None):
  539. """Wait for a handle.
  540. Return a Future object. The result of the future is True if the wait
  541. completed, or False if the wait did not complete (on timeout).
  542. """
  543. return self._wait_for_handle(handle, timeout, False)
  544. def _wait_cancel(self, event, done_callback):
  545. fut = self._wait_for_handle(event, None, True)
  546. # add_done_callback() cannot be used because the wait may only complete
  547. # in IocpProactor.close(), while the event loop is not running.
  548. fut._done_callback = done_callback
  549. return fut
  550. def _wait_for_handle(self, handle, timeout, _is_cancel):
  551. self._check_closed()
  552. if timeout is None:
  553. ms = _winapi.INFINITE
  554. else:
  555. # RegisterWaitForSingleObject() has a resolution of 1 millisecond,
  556. # round away from zero to wait *at least* timeout seconds.
  557. ms = math.ceil(timeout * 1e3)
  558. # We only create ov so we can use ov.address as a key for the cache.
  559. ov = _overlapped.Overlapped(NULL)
  560. wait_handle = _overlapped.RegisterWaitWithQueue(
  561. handle, self._iocp, ov.address, ms)
  562. if _is_cancel:
  563. f = _WaitCancelFuture(ov, handle, wait_handle, loop=self._loop)
  564. else:
  565. f = _WaitHandleFuture(ov, handle, wait_handle, self,
  566. loop=self._loop)
  567. if f._source_traceback:
  568. del f._source_traceback[-1]
  569. def finish_wait_for_handle(trans, key, ov):
  570. # Note that this second wait means that we should only use
  571. # this with handles types where a successful wait has no
  572. # effect. So events or processes are all right, but locks
  573. # or semaphores are not. Also note if the handle is
  574. # signalled and then quickly reset, then we may return
  575. # False even though we have not timed out.
  576. return f._poll()
  577. self._cache[ov.address] = (f, ov, 0, finish_wait_for_handle)
  578. return f
  579. def _register_with_iocp(self, obj):
  580. # To get notifications of finished ops on this objects sent to the
  581. # completion port, were must register the handle.
  582. if obj not in self._registered:
  583. self._registered.add(obj)
  584. _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
  585. # XXX We could also use SetFileCompletionNotificationModes()
  586. # to avoid sending notifications to completion port of ops
  587. # that succeed immediately.
  588. def _register(self, ov, obj, callback):
  589. self._check_closed()
  590. # Return a future which will be set with the result of the
  591. # operation when it completes. The future's value is actually
  592. # the value returned by callback().
  593. f = _OverlappedFuture(ov, loop=self._loop)
  594. if f._source_traceback:
  595. del f._source_traceback[-1]
  596. if not ov.pending:
  597. # The operation has completed, so no need to postpone the
  598. # work. We cannot take this short cut if we need the
  599. # NumberOfBytes, CompletionKey values returned by
  600. # PostQueuedCompletionStatus().
  601. try:
  602. value = callback(None, None, ov)
  603. except OSError as e:
  604. f.set_exception(e)
  605. else:
  606. f.set_result(value)
  607. # Even if GetOverlappedResult() was called, we have to wait for the
  608. # notification of the completion in GetQueuedCompletionStatus().
  609. # Register the overlapped operation to keep a reference to the
  610. # OVERLAPPED object, otherwise the memory is freed and Windows may
  611. # read uninitialized memory.
  612. # Register the overlapped operation for later. Note that
  613. # we only store obj to prevent it from being garbage
  614. # collected too early.
  615. self._cache[ov.address] = (f, ov, obj, callback)
  616. return f
  617. def _unregister(self, ov):
  618. """Unregister an overlapped object.
  619. Call this method when its future has been cancelled. The event can
  620. already be signalled (pending in the proactor event queue). It is also
  621. safe if the event is never signalled (because it was cancelled).
  622. """
  623. self._check_closed()
  624. self._unregistered.append(ov)
  625. def _get_accept_socket(self, family):
  626. s = socket.socket(family)
  627. s.settimeout(0)
  628. return s
  629. def _poll(self, timeout=None):
  630. if timeout is None:
  631. ms = INFINITE
  632. elif timeout < 0:
  633. raise ValueError("negative timeout")
  634. else:
  635. # GetQueuedCompletionStatus() has a resolution of 1 millisecond,
  636. # round away from zero to wait *at least* timeout seconds.
  637. ms = math.ceil(timeout * 1e3)
  638. if ms >= INFINITE:
  639. raise ValueError("timeout too big")
  640. while True:
  641. status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
  642. if status is None:
  643. break
  644. ms = 0
  645. err, transferred, key, address = status
  646. try:
  647. f, ov, obj, callback = self._cache.pop(address)
  648. except KeyError:
  649. if self._loop.get_debug():
  650. self._loop.call_exception_handler({
  651. 'message': ('GetQueuedCompletionStatus() returned an '
  652. 'unexpected event'),
  653. 'status': ('err=%s transferred=%s key=%#x address=%#x'
  654. % (err, transferred, key, address)),
  655. })
  656. # key is either zero, or it is used to return a pipe
  657. # handle which should be closed to avoid a leak.
  658. if key not in (0, _overlapped.INVALID_HANDLE_VALUE):
  659. _winapi.CloseHandle(key)
  660. continue
  661. if obj in self._stopped_serving:
  662. f.cancel()
  663. # Don't call the callback if _register() already read the result or
  664. # if the overlapped has been cancelled
  665. elif not f.done():
  666. try:
  667. value = callback(transferred, key, ov)
  668. except OSError as e:
  669. f.set_exception(e)
  670. self._results.append(f)
  671. else:
  672. f.set_result(value)
  673. self._results.append(f)
  674. finally:
  675. f = None
  676. # Remove unregistered futures
  677. for ov in self._unregistered:
  678. self._cache.pop(ov.address, None)
  679. self._unregistered.clear()
  680. def _stop_serving(self, obj):
  681. # obj is a socket or pipe handle. It will be closed in
  682. # BaseProactorEventLoop._stop_serving() which will make any
  683. # pending operations fail quickly.
  684. self._stopped_serving.add(obj)
  685. def close(self):
  686. if self._iocp is None:
  687. # already closed
  688. return
  689. # Cancel remaining registered operations.
  690. for fut, ov, obj, callback in list(self._cache.values()):
  691. if fut.cancelled():
  692. # Nothing to do with cancelled futures
  693. pass
  694. elif isinstance(fut, _WaitCancelFuture):
  695. # _WaitCancelFuture must not be cancelled
  696. pass
  697. else:
  698. try:
  699. fut.cancel()
  700. except OSError as exc:
  701. if self._loop is not None:
  702. context = {
  703. 'message': 'Cancelling a future failed',
  704. 'exception': exc,
  705. 'future': fut,
  706. }
  707. if fut._source_traceback:
  708. context['source_traceback'] = fut._source_traceback
  709. self._loop.call_exception_handler(context)
  710. # Wait until all cancelled overlapped complete: don't exit with running
  711. # overlapped to prevent a crash. Display progress every second if the
  712. # loop is still running.
  713. msg_update = 1.0
  714. start_time = time.monotonic()
  715. next_msg = start_time + msg_update
  716. while self._cache:
  717. if next_msg <= time.monotonic():
  718. logger.debug('%r is running after closing for %.1f seconds',
  719. self, time.monotonic() - start_time)
  720. next_msg = time.monotonic() + msg_update
  721. # handle a few events, or timeout
  722. self._poll(msg_update)
  723. self._results = []
  724. _winapi.CloseHandle(self._iocp)
  725. self._iocp = None
  726. def __del__(self):
  727. self.close()
  728. class _WindowsSubprocessTransport(base_subprocess.BaseSubprocessTransport):
  729. def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
  730. self._proc = windows_utils.Popen(
  731. args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
  732. bufsize=bufsize, **kwargs)
  733. def callback(f):
  734. returncode = self._proc.poll()
  735. self._process_exited(returncode)
  736. f = self._loop._proactor.wait_for_handle(int(self._proc._handle))
  737. f.add_done_callback(callback)
  738. SelectorEventLoop = _WindowsSelectorEventLoop
  739. class WindowsSelectorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
  740. _loop_factory = SelectorEventLoop
  741. class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
  742. _loop_factory = ProactorEventLoop
  743. DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy