Coverage for Lib/asyncio/windows_events.py: 0%

530 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 05:46 +0000

1"""Selector and proactor event loops for Windows.""" 

2 

3import sys 

4 

5if sys.platform != 'win32': # pragma: no cover 

6 raise ImportError('win32 only') 

7 

8import _overlapped 

9import _winapi 

10import errno 

11import math 

12import msvcrt 

13import socket 

14import struct 

15import time 

16import weakref 

17 

18from . import base_subprocess 

19from . import futures 

20from . import exceptions 

21from . import proactor_events 

22from . import selector_events 

23from . import tasks 

24from . import windows_utils 

25from .log import logger 

26 

27 

28__all__ = ( 

29 'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor', 

30 'EventLoop', 

31) 

32 

33 

34NULL = _winapi.NULL 

35INFINITE = _winapi.INFINITE 

36ERROR_CONNECTION_REFUSED = 1225 

37ERROR_CONNECTION_ABORTED = 1236 

38 

39# Initial delay in seconds for connect_pipe() before retrying to connect 

40CONNECT_PIPE_INIT_DELAY = 0.001 

41 

42# Maximum delay in seconds for connect_pipe() before retrying to connect 

43CONNECT_PIPE_MAX_DELAY = 0.100 

44 

45 

46class _OverlappedFuture(futures.Future): 

47 """Subclass of Future which represents an overlapped operation. 

48 

49 Cancelling it will immediately cancel the overlapped operation. 

50 """ 

51 

52 def __init__(self, ov, *, loop=None): 

53 super().__init__(loop=loop) 

54 if self._source_traceback: 

55 del self._source_traceback[-1] 

56 self._ov = ov 

57 

58 def _cancel_overlapped(self): 

59 if self._ov is None: 

60 return 

61 try: 

62 self._ov.cancel() 

63 except OSError as exc: 

64 context = { 

65 'message': 'Cancelling an overlapped future failed', 

66 'exception': exc, 

67 'future': self, 

68 } 

69 if self._source_traceback: 

70 context['source_traceback'] = self._source_traceback 

71 self._loop.call_exception_handler(context) 

72 self._ov = None 

73 

74 def cancel(self, msg=None): 

75 self._cancel_overlapped() 

76 return super().cancel(msg=msg) 

77 

78 def set_exception(self, exception): 

79 super().set_exception(exception) 

80 self._cancel_overlapped() 

81 

82 def set_result(self, result): 

83 super().set_result(result) 

84 self._ov = None 

85 

86 

87class _BaseWaitHandleFuture(futures.Future): 

88 """Subclass of Future which represents a wait handle.""" 

89 

90 def __init__(self, ov, handle, wait_handle, *, loop=None): 

91 super().__init__(loop=loop) 

92 if self._source_traceback: 

93 del self._source_traceback[-1] 

94 # Keep a reference to the Overlapped object to keep it alive until the 

95 # wait is unregistered 

96 self._ov = ov 

97 self._handle = handle 

98 self._wait_handle = wait_handle 

99 

100 # Should we call UnregisterWaitEx() if the wait completes 

101 # or is cancelled? 

102 self._registered = True 

103 

104 def _poll(self): 

105 # non-blocking wait: use a timeout of 0 millisecond 

106 return (_winapi.WaitForSingleObject(self._handle, 0) == 

107 _winapi.WAIT_OBJECT_0) 

108 

109 def _unregister_wait_cb(self, fut): 

110 # The wait was unregistered: it's not safe to destroy the Overlapped 

111 # object 

112 self._ov = None 

113 

114 def _unregister_wait(self): 

115 if not self._registered: 

116 return 

117 self._registered = False 

118 

119 wait_handle = self._wait_handle 

120 self._wait_handle = None 

121 try: 

122 _overlapped.UnregisterWait(wait_handle) 

123 except OSError as exc: 

124 if exc.winerror != _overlapped.ERROR_IO_PENDING: 

125 context = { 

126 'message': 'Failed to unregister the wait handle', 

127 'exception': exc, 

128 'future': self, 

129 } 

130 if self._source_traceback: 

131 context['source_traceback'] = self._source_traceback 

132 self._loop.call_exception_handler(context) 

133 return 

134 # ERROR_IO_PENDING means that the unregister is pending 

135 

136 self._unregister_wait_cb(None) 

137 

138 def cancel(self, msg=None): 

139 self._unregister_wait() 

140 return super().cancel(msg=msg) 

141 

142 def set_exception(self, exception): 

143 self._unregister_wait() 

144 super().set_exception(exception) 

145 

146 def set_result(self, result): 

147 self._unregister_wait() 

148 super().set_result(result) 

149 

150 

151class _WaitCancelFuture(_BaseWaitHandleFuture): 

152 """Subclass of Future which represents a wait for the cancellation of a 

153 _WaitHandleFuture using an event. 

154 """ 

155 

156 def __init__(self, ov, event, wait_handle, *, loop=None): 

157 super().__init__(ov, event, wait_handle, loop=loop) 

158 

159 self._done_callback = None 

160 

161 def cancel(self): 

162 raise RuntimeError("_WaitCancelFuture must not be cancelled") 

163 

164 def set_result(self, result): 

165 super().set_result(result) 

166 if self._done_callback is not None: 

167 self._done_callback(self) 

168 

169 def set_exception(self, exception): 

170 super().set_exception(exception) 

171 if self._done_callback is not None: 

172 self._done_callback(self) 

173 

174 

175class _WaitHandleFuture(_BaseWaitHandleFuture): 

176 def __init__(self, ov, handle, wait_handle, proactor, *, loop=None): 

177 super().__init__(ov, handle, wait_handle, loop=loop) 

178 self._proactor = proactor 

179 self._unregister_proactor = True 

180 self._event = _overlapped.CreateEvent(None, True, False, None) 

181 self._event_fut = None 

182 

183 def _unregister_wait_cb(self, fut): 

184 if self._event is not None: 

185 _winapi.CloseHandle(self._event) 

186 self._event = None 

187 self._event_fut = None 

188 

189 # If the wait was cancelled, the wait may never be signalled, so 

190 # it's required to unregister it. Otherwise, IocpProactor.close() will 

191 # wait forever for an event which will never come. 

192 # 

193 # If the IocpProactor already received the event, it's safe to call 

194 # _unregister() because we kept a reference to the Overlapped object 

195 # which is used as a unique key. 

196 self._proactor._unregister(self._ov) 

197 self._proactor = None 

198 

199 super()._unregister_wait_cb(fut) 

200 

201 def _unregister_wait(self): 

202 if not self._registered: 

203 return 

204 self._registered = False 

205 

206 wait_handle = self._wait_handle 

207 self._wait_handle = None 

208 try: 

209 _overlapped.UnregisterWaitEx(wait_handle, self._event) 

210 except OSError as exc: 

211 if exc.winerror != _overlapped.ERROR_IO_PENDING: 

212 context = { 

213 'message': 'Failed to unregister the wait handle', 

214 'exception': exc, 

215 'future': self, 

216 } 

217 if self._source_traceback: 

218 context['source_traceback'] = self._source_traceback 

219 self._loop.call_exception_handler(context) 

220 return 

221 # ERROR_IO_PENDING is not an error, the wait was unregistered 

222 

223 self._event_fut = self._proactor._wait_cancel(self._event, 

224 self._unregister_wait_cb) 

225 

226 

227class PipeServer(object): 

228 """Class representing a pipe server. 

229 

230 This is much like a bound, listening socket. 

231 """ 

232 def __init__(self, address): 

233 self._address = address 

234 self._free_instances = weakref.WeakSet() 

235 # initialize the pipe attribute before calling _server_pipe_handle() 

236 # because this function can raise an exception and the destructor calls 

237 # the close() method 

238 self._pipe = None 

239 self._accept_pipe_future = None 

240 self._pipe = self._server_pipe_handle(True) 

241 

242 def _get_unconnected_pipe(self): 

243 # Create new instance and return previous one. This ensures 

244 # that (until the server is closed) there is always at least 

245 # one pipe handle for address. Therefore if a client attempt 

246 # to connect it will not fail with FileNotFoundError. 

247 tmp, self._pipe = self._pipe, self._server_pipe_handle(False) 

248 return tmp 

249 

250 def _server_pipe_handle(self, first): 

251 # Return a wrapper for a new pipe handle. 

252 if self.closed(): 

253 return None 

254 flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED 

255 if first: 

256 flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE 

257 h = _winapi.CreateNamedPipe( 

258 self._address, flags, 

259 _winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE | 

260 _winapi.PIPE_WAIT, 

261 _winapi.PIPE_UNLIMITED_INSTANCES, 

262 windows_utils.BUFSIZE, windows_utils.BUFSIZE, 

263 _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL) 

264 pipe = windows_utils.PipeHandle(h) 

265 self._free_instances.add(pipe) 

266 return pipe 

267 

268 def closed(self): 

269 return (self._address is None) 

270 

271 def close(self): 

272 if self._accept_pipe_future is not None: 

273 self._accept_pipe_future.cancel() 

274 self._accept_pipe_future = None 

275 # Close all instances which have not been connected to by a client. 

276 if self._address is not None: 

277 for pipe in self._free_instances: 

278 pipe.close() 

279 self._pipe = None 

280 self._address = None 

281 self._free_instances.clear() 

282 

283 __del__ = close 

284 

285 

286class _WindowsSelectorEventLoop(selector_events.BaseSelectorEventLoop): 

287 """Windows version of selector event loop.""" 

288 

289 

290class ProactorEventLoop(proactor_events.BaseProactorEventLoop): 

291 """Windows version of proactor event loop using IOCP.""" 

292 

293 def __init__(self, proactor=None): 

294 if proactor is None: 

295 proactor = IocpProactor() 

296 super().__init__(proactor) 

297 

298 def _run_forever_setup(self): 

299 assert self._self_reading_future is None 

300 self.call_soon(self._loop_self_reading) 

301 super()._run_forever_setup() 

302 

303 def _run_forever_cleanup(self): 

304 super()._run_forever_cleanup() 

305 if self._self_reading_future is not None: 

306 ov = self._self_reading_future._ov 

307 self._self_reading_future.cancel() 

308 # self_reading_future always uses IOCP, so even though it's 

309 # been cancelled, we need to make sure that the IOCP message 

310 # is received so that the kernel is not holding on to the 

311 # memory, possibly causing memory corruption later. Only 

312 # unregister it if IO is complete in all respects. Otherwise 

313 # we need another _poll() later to complete the IO. 

314 if ov is not None and not ov.pending: 

315 self._proactor._unregister(ov) 

316 self._self_reading_future = None 

317 

318 async def create_pipe_connection(self, protocol_factory, address): 

319 f = self._proactor.connect_pipe(address) 

320 pipe = await f 

321 protocol = protocol_factory() 

322 trans = self._make_duplex_pipe_transport(pipe, protocol, 

323 extra={'addr': address}) 

324 return trans, protocol 

325 

326 async def start_serving_pipe(self, protocol_factory, address): 

327 server = PipeServer(address) 

328 

329 def loop_accept_pipe(f=None): 

330 pipe = None 

331 try: 

332 if f: 

333 pipe = f.result() 

334 server._free_instances.discard(pipe) 

335 

336 if server.closed(): 

337 # A client connected before the server was closed: 

338 # drop the client (close the pipe) and exit 

339 pipe.close() 

340 return 

341 

342 protocol = protocol_factory() 

343 self._make_duplex_pipe_transport( 

344 pipe, protocol, extra={'addr': address}) 

345 

346 pipe = server._get_unconnected_pipe() 

347 if pipe is None: 

348 return 

349 

350 f = self._proactor.accept_pipe(pipe) 

351 except BrokenPipeError: 

352 if pipe and pipe.fileno() != -1: 

353 pipe.close() 

354 self.call_soon(loop_accept_pipe) 

355 except OSError as exc: 

356 if pipe and pipe.fileno() != -1: 

357 self.call_exception_handler({ 

358 'message': 'Pipe accept failed', 

359 'exception': exc, 

360 'pipe': pipe, 

361 }) 

362 pipe.close() 

363 elif self._debug: 

364 logger.warning("Accept pipe failed on pipe %r", 

365 pipe, exc_info=True) 

366 self.call_soon(loop_accept_pipe) 

367 except exceptions.CancelledError: 

368 if pipe: 

369 pipe.close() 

370 else: 

371 server._accept_pipe_future = f 

372 f.add_done_callback(loop_accept_pipe) 

373 

374 self.call_soon(loop_accept_pipe) 

375 return [server] 

376 

377 async def _make_subprocess_transport(self, protocol, args, shell, 

378 stdin, stdout, stderr, bufsize, 

379 extra=None, **kwargs): 

380 waiter = self.create_future() 

381 transp = _WindowsSubprocessTransport(self, protocol, args, shell, 

382 stdin, stdout, stderr, bufsize, 

383 waiter=waiter, extra=extra, 

384 **kwargs) 

385 try: 

386 await waiter 

387 except (SystemExit, KeyboardInterrupt): 

388 raise 

389 except BaseException: 

390 transp.close() 

391 await tasks.shield(transp._wait()) 

392 raise 

393 

394 return transp 

395 

396 

397class IocpProactor: 

398 """Proactor implementation using IOCP.""" 

399 

400 def __init__(self, concurrency=INFINITE): 

401 self._loop = None 

402 self._results = [] 

403 self._iocp = _overlapped.CreateIoCompletionPort( 

404 _overlapped.INVALID_HANDLE_VALUE, NULL, 0, concurrency) 

405 self._cache = {} 

406 self._registered = weakref.WeakSet() 

407 self._unregistered = [] 

408 self._stopped_serving = weakref.WeakSet() 

409 

410 def _check_closed(self): 

411 if self._iocp is None: 

412 raise RuntimeError('IocpProactor is closed') 

413 

414 def __repr__(self): 

415 info = ['overlapped#=%s' % len(self._cache), 

416 'result#=%s' % len(self._results)] 

417 if self._iocp is None: 

418 info.append('closed') 

419 return '<%s %s>' % (self.__class__.__name__, " ".join(info)) 

420 

421 def set_loop(self, loop): 

422 self._loop = loop 

423 

424 def select(self, timeout=None): 

425 if not self._results: 

426 self._poll(timeout) 

427 tmp = self._results 

428 self._results = [] 

429 try: 

430 return tmp 

431 finally: 

432 # Needed to break cycles when an exception occurs. 

433 tmp = None 

434 

435 def _result(self, value): 

436 fut = self._loop.create_future() 

437 fut.set_result(value) 

438 return fut 

439 

440 @staticmethod 

441 def finish_socket_func(trans, key, ov): 

442 try: 

443 return ov.getresult() 

444 except OSError as exc: 

445 # ERROR_PORT_UNREACHABLE is reported by WSARecvFrom when the 

446 # same socket was previously used to send to an address that 

447 # isn't listening (gh-91227). Surface it as a 

448 # ConnectionResetError, like the other recoverable codes here, 

449 # so it propagates to the caller the same way a plain 

450 # socket.recvfrom() already does on SelectorEventLoop, instead 

451 # of being silently swallowed. 

452 if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, 

453 _overlapped.ERROR_OPERATION_ABORTED, 

454 _overlapped.ERROR_PORT_UNREACHABLE): 

455 raise ConnectionResetError(*exc.args) 

456 else: 

457 raise 

458 

459 def recv(self, conn, nbytes, flags=0): 

460 self._register_with_iocp(conn) 

461 ov = _overlapped.Overlapped(NULL) 

462 try: 

463 if isinstance(conn, socket.socket): 

464 ov.WSARecv(conn.fileno(), nbytes, flags) 

465 else: 

466 ov.ReadFile(conn.fileno(), nbytes) 

467 except BrokenPipeError: 

468 return self._result(b'') 

469 

470 return self._register(ov, conn, self.finish_socket_func) 

471 

472 def recv_into(self, conn, buf, flags=0): 

473 self._register_with_iocp(conn) 

474 ov = _overlapped.Overlapped(NULL) 

475 try: 

476 if isinstance(conn, socket.socket): 

477 ov.WSARecvInto(conn.fileno(), buf, flags) 

478 else: 

479 ov.ReadFileInto(conn.fileno(), buf) 

480 except BrokenPipeError: 

481 return self._result(0) 

482 

483 return self._register(ov, conn, self.finish_socket_func) 

484 

485 def recvfrom(self, conn, nbytes, flags=0): 

486 self._register_with_iocp(conn) 

487 ov = _overlapped.Overlapped(NULL) 

488 try: 

489 ov.WSARecvFrom(conn.fileno(), nbytes, flags) 

490 except BrokenPipeError: 

491 return self._result((b'', None)) 

492 

493 return self._register(ov, conn, self.finish_socket_func) 

494 

495 def recvfrom_into(self, conn, buf, nbytes=0): 

496 self._register_with_iocp(conn) 

497 ov = _overlapped.Overlapped(NULL) 

498 try: 

499 ov.WSARecvFromInto(conn.fileno(), buf, nbytes) 

500 except BrokenPipeError: 

501 return self._result((0, None)) 

502 

503 return self._register(ov, conn, self.finish_socket_func) 

504 

505 def sendto(self, conn, buf, flags=0, addr=None): 

506 self._register_with_iocp(conn) 

507 ov = _overlapped.Overlapped(NULL) 

508 

509 ov.WSASendTo(conn.fileno(), buf, flags, addr) 

510 

511 return self._register(ov, conn, self.finish_socket_func) 

512 

513 def send(self, conn, buf, flags=0): 

514 self._register_with_iocp(conn) 

515 ov = _overlapped.Overlapped(NULL) 

516 if isinstance(conn, socket.socket): 

517 ov.WSASend(conn.fileno(), buf, flags) 

518 else: 

519 ov.WriteFile(conn.fileno(), buf) 

520 

521 return self._register(ov, conn, self.finish_socket_func) 

522 

523 def accept(self, listener): 

524 self._register_with_iocp(listener) 

525 conn = self._get_accept_socket(listener.family) 

526 ov = _overlapped.Overlapped(NULL) 

527 ov.AcceptEx(listener.fileno(), conn.fileno()) 

528 

529 def finish_accept(trans, key, ov): 

530 try: 

531 ov.getresult() 

532 except OSError as exc: 

533 conn.close() 

534 # The peer reset the connection before it was accepted. 

535 if exc.winerror == _overlapped.ERROR_NETNAME_DELETED: 

536 raise ConnectionResetError(*exc.args) 

537 raise 

538 # Use SO_UPDATE_ACCEPT_CONTEXT so getsockname() etc work. 

539 buf = struct.pack('@P', listener.fileno()) 

540 conn.setsockopt(socket.SOL_SOCKET, 

541 _overlapped.SO_UPDATE_ACCEPT_CONTEXT, buf) 

542 conn.settimeout(listener.gettimeout()) 

543 return conn, conn.getpeername() 

544 

545 async def accept_coro(future, conn): 

546 # Coroutine closing the accept socket if the future is cancelled 

547 try: 

548 await future 

549 except exceptions.CancelledError: 

550 conn.close() 

551 raise 

552 except ConnectionResetError: 

553 # Reported to the caller through the future. 

554 pass 

555 

556 future = self._register(ov, listener, finish_accept) 

557 coro = accept_coro(future, conn) 

558 tasks.ensure_future(coro, loop=self._loop) 

559 return future 

560 

561 def connect(self, conn, address): 

562 if conn.type == socket.SOCK_DGRAM: 

563 # WSAConnect will complete immediately for UDP sockets so we don't 

564 # need to register any IOCP operation 

565 _overlapped.WSAConnect(conn.fileno(), address) 

566 fut = self._loop.create_future() 

567 fut.set_result(None) 

568 return fut 

569 

570 self._register_with_iocp(conn) 

571 # The socket needs to be locally bound before we call ConnectEx(). 

572 try: 

573 _overlapped.BindLocal(conn.fileno(), conn.family) 

574 except OSError as e: 

575 if e.winerror != errno.WSAEINVAL: 

576 raise 

577 # Probably already locally bound; check using getsockname(). 

578 if conn.getsockname()[1] == 0: 

579 raise 

580 ov = _overlapped.Overlapped(NULL) 

581 ov.ConnectEx(conn.fileno(), address) 

582 

583 def finish_connect(trans, key, ov): 

584 ov.getresult() 

585 # Use SO_UPDATE_CONNECT_CONTEXT so getsockname() etc work. 

586 conn.setsockopt(socket.SOL_SOCKET, 

587 _overlapped.SO_UPDATE_CONNECT_CONTEXT, 0) 

588 return conn 

589 

590 return self._register(ov, conn, finish_connect) 

591 

592 def sendfile(self, sock, file, offset, count): 

593 self._register_with_iocp(sock) 

594 ov = _overlapped.Overlapped(NULL) 

595 offset_low = offset & 0xffff_ffff 

596 offset_high = (offset >> 32) & 0xffff_ffff 

597 # TransmitFile ignores OVERLAPPED.Offset for handles not opened with 

598 # FILE_FLAG_OVERLAPPED, so seek the CRT file pointer to match. 

599 file.seek(offset) 

600 ov.TransmitFile(sock.fileno(), 

601 msvcrt.get_osfhandle(file.fileno()), 

602 offset_low, offset_high, 

603 count, 0, 0) 

604 

605 return self._register(ov, sock, self.finish_socket_func) 

606 

607 def accept_pipe(self, pipe): 

608 self._register_with_iocp(pipe) 

609 ov = _overlapped.Overlapped(NULL) 

610 connected = ov.ConnectNamedPipe(pipe.fileno()) 

611 

612 if connected: 

613 # ConnectNamePipe() failed with ERROR_PIPE_CONNECTED which means 

614 # that the pipe is connected. There is no need to wait for the 

615 # completion of the connection. 

616 return self._result(pipe) 

617 

618 def finish_accept_pipe(trans, key, ov): 

619 ov.getresult() 

620 return pipe 

621 

622 return self._register(ov, pipe, finish_accept_pipe) 

623 

624 async def connect_pipe(self, address): 

625 delay = CONNECT_PIPE_INIT_DELAY 

626 while True: 

627 # Unfortunately there is no way to do an overlapped connect to 

628 # a pipe. Call CreateFile() in a loop until it doesn't fail with 

629 # ERROR_PIPE_BUSY. 

630 try: 

631 handle = _overlapped.ConnectPipe(address) 

632 break 

633 except OSError as exc: 

634 if exc.winerror != _overlapped.ERROR_PIPE_BUSY: 

635 raise 

636 

637 # ConnectPipe() failed with ERROR_PIPE_BUSY: retry later 

638 delay = min(delay * 2, CONNECT_PIPE_MAX_DELAY) 

639 await tasks.sleep(delay) 

640 

641 return windows_utils.PipeHandle(handle) 

642 

643 def wait_for_handle(self, handle, timeout=None): 

644 """Wait for a handle. 

645 

646 Return a Future object. The result of the future is True if the wait 

647 completed, or False if the wait did not complete (on timeout). 

648 """ 

649 return self._wait_for_handle(handle, timeout, False) 

650 

651 def _wait_cancel(self, event, done_callback): 

652 fut = self._wait_for_handle(event, None, True) 

653 # add_done_callback() cannot be used because the wait may only complete 

654 # in IocpProactor.close(), while the event loop is not running. 

655 fut._done_callback = done_callback 

656 return fut 

657 

658 def _wait_for_handle(self, handle, timeout, _is_cancel): 

659 self._check_closed() 

660 

661 if timeout is None: 

662 ms = _winapi.INFINITE 

663 else: 

664 # RegisterWaitForSingleObject() has a resolution of 1 millisecond, 

665 # round away from zero to wait *at least* timeout seconds. 

666 ms = math.ceil(timeout * 1e3) 

667 

668 # We only create ov so we can use ov.address as a key for the cache. 

669 ov = _overlapped.Overlapped(NULL) 

670 wait_handle = _overlapped.RegisterWaitWithQueue( 

671 handle, self._iocp, ov.address, ms) 

672 if _is_cancel: 

673 f = _WaitCancelFuture(ov, handle, wait_handle, loop=self._loop) 

674 else: 

675 f = _WaitHandleFuture(ov, handle, wait_handle, self, 

676 loop=self._loop) 

677 if f._source_traceback: 

678 del f._source_traceback[-1] 

679 

680 def finish_wait_for_handle(trans, key, ov): 

681 # Note that this second wait means that we should only use 

682 # this with handles types where a successful wait has no 

683 # effect. So events or processes are all right, but locks 

684 # or semaphores are not. Also note if the handle is 

685 # signalled and then quickly reset, then we may return 

686 # False even though we have not timed out. 

687 return f._poll() 

688 

689 self._cache[ov.address] = (f, ov, 0, finish_wait_for_handle) 

690 return f 

691 

692 def _register_with_iocp(self, obj): 

693 # To get notifications of finished ops on this objects sent to the 

694 # completion port, were must register the handle. 

695 if obj not in self._registered: 

696 self._registered.add(obj) 

697 _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0) 

698 # XXX We could also use SetFileCompletionNotificationModes() 

699 # to avoid sending notifications to completion port of ops 

700 # that succeed immediately. 

701 

702 def _register(self, ov, obj, callback): 

703 self._check_closed() 

704 

705 # Return a future which will be set with the result of the 

706 # operation when it completes. The future's value is actually 

707 # the value returned by callback(). 

708 f = _OverlappedFuture(ov, loop=self._loop) 

709 if f._source_traceback: 

710 del f._source_traceback[-1] 

711 if not ov.pending: 

712 # The operation has completed, so no need to postpone the 

713 # work. We cannot take this short cut if we need the 

714 # NumberOfBytes, CompletionKey values returned by 

715 # PostQueuedCompletionStatus(). 

716 try: 

717 value = callback(None, None, ov) 

718 except OSError as e: 

719 f.set_exception(e) 

720 else: 

721 f.set_result(value) 

722 # Even if GetOverlappedResult() was called, we have to wait for the 

723 # notification of the completion in GetQueuedCompletionStatus(). 

724 # Register the overlapped operation to keep a reference to the 

725 # OVERLAPPED object, otherwise the memory is freed and Windows may 

726 # read uninitialized memory. 

727 

728 # Register the overlapped operation for later. Note that 

729 # we only store obj to prevent it from being garbage 

730 # collected too early. 

731 self._cache[ov.address] = (f, ov, obj, callback) 

732 return f 

733 

734 def _unregister(self, ov): 

735 """Unregister an overlapped object. 

736 

737 Call this method when its future has been cancelled. The event can 

738 already be signalled (pending in the proactor event queue). It is also 

739 safe if the event is never signalled (because it was cancelled). 

740 """ 

741 self._check_closed() 

742 self._unregistered.append(ov) 

743 

744 def _get_accept_socket(self, family): 

745 s = socket.socket(family) 

746 s.settimeout(0) 

747 return s 

748 

749 def _process_completion_status(self, status): 

750 """Process a single status from the completion port. 

751 

752 A caller that waits on the completion port itself can pass each 

753 status it receives here. 

754 """ 

755 err, transferred, key, address = status 

756 try: 

757 f, ov, obj, callback = self._cache.pop(address) 

758 except KeyError: 

759 if self._loop.get_debug(): 

760 self._loop.call_exception_handler({ 

761 'message': ('GetQueuedCompletionStatus() returned an ' 

762 'unexpected event'), 

763 'status': ('err=%s transferred=%s key=%#x address=%#x' 

764 % (err, transferred, key, address)), 

765 }) 

766 

767 # key is either zero, or it is used to return a pipe 

768 # handle which should be closed to avoid a leak. 

769 if key not in (0, _overlapped.INVALID_HANDLE_VALUE): 

770 _winapi.CloseHandle(key) 

771 return 

772 

773 if obj in self._stopped_serving: 

774 f.cancel() 

775 # Don't call the callback if _register() already read the result or 

776 # if the overlapped has been cancelled 

777 elif not f.done(): 

778 try: 

779 value = callback(transferred, key, ov) 

780 except OSError as e: 

781 f.set_exception(e) 

782 self._results.append(f) 

783 else: 

784 f.set_result(value) 

785 self._results.append(f) 

786 finally: 

787 f = None 

788 

789 def _poll(self, timeout=None): 

790 if timeout is None: 

791 ms = INFINITE 

792 elif timeout < 0: 

793 raise ValueError("negative timeout") 

794 else: 

795 # GetQueuedCompletionStatus() has a resolution of 1 millisecond, 

796 # round away from zero to wait *at least* timeout seconds. 

797 ms = math.ceil(timeout * 1e3) 

798 if ms >= INFINITE: 

799 raise ValueError("timeout too big") 

800 

801 while True: 

802 status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms) 

803 if status is None: 

804 break 

805 ms = 0 

806 

807 # gh-154971: split out so custom event loops can call it directly 

808 self._process_completion_status(status) 

809 

810 # Remove unregistered futures 

811 for ov in self._unregistered: 

812 self._cache.pop(ov.address, None) 

813 self._unregistered.clear() 

814 

815 def _stop_serving(self, obj): 

816 # obj is a socket or pipe handle. It will be closed in 

817 # BaseProactorEventLoop._stop_serving() which will make any 

818 # pending operations fail quickly. 

819 self._stopped_serving.add(obj) 

820 

821 def close(self): 

822 if self._iocp is None: 

823 # already closed 

824 return 

825 

826 # Cancel remaining registered operations. 

827 for fut, ov, obj, callback in list(self._cache.values()): 

828 if fut.cancelled(): 

829 # Nothing to do with cancelled futures 

830 pass 

831 elif isinstance(fut, _WaitCancelFuture): 

832 # _WaitCancelFuture must not be cancelled 

833 pass 

834 else: 

835 try: 

836 fut.cancel() 

837 except OSError as exc: 

838 if self._loop is not None: 

839 context = { 

840 'message': 'Cancelling a future failed', 

841 'exception': exc, 

842 'future': fut, 

843 } 

844 if fut._source_traceback: 

845 context['source_traceback'] = fut._source_traceback 

846 self._loop.call_exception_handler(context) 

847 

848 # Wait until all cancelled overlapped complete: don't exit with running 

849 # overlapped to prevent a crash. Display progress every second if the 

850 # loop is still running. 

851 msg_update = 1.0 

852 start_time = time.monotonic() 

853 next_msg = start_time + msg_update 

854 while self._cache: 

855 if next_msg <= time.monotonic(): 

856 logger.debug('%r is running after closing for %.1f seconds', 

857 self, time.monotonic() - start_time) 

858 next_msg = time.monotonic() + msg_update 

859 

860 # handle a few events, or timeout 

861 self._poll(msg_update) 

862 

863 self._results = [] 

864 

865 _winapi.CloseHandle(self._iocp) 

866 self._iocp = None 

867 

868 def __del__(self): 

869 self.close() 

870 

871 

872class _WindowsSubprocessTransport(base_subprocess.BaseSubprocessTransport): 

873 

874 def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs): 

875 self._proc = windows_utils.Popen( 

876 args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, 

877 bufsize=bufsize, **kwargs) 

878 

879 def callback(f): 

880 returncode = self._proc.poll() 

881 self._process_exited(returncode) 

882 

883 f = self._loop._proactor.wait_for_handle(int(self._proc._handle)) 

884 f.add_done_callback(callback) 

885 

886 

887SelectorEventLoop = _WindowsSelectorEventLoop 

888 

889EventLoop = ProactorEventLoop