From 08df8fc15d8463f8b258d9ae7c1478fa8bcda576 Mon Sep 17 00:00:00 2001 From: Tony Solomonik Date: Wed, 8 Jul 2020 20:34:21 +0300 Subject: [PATCH] bpo-41246: IOCP Proactor same socket overlapped callbacks Give the same callback function for when the overlapped operation is done for the functions recv, recv_into, recvfrom, sendto, send and sendfile inside IocpProactor. --- Lib/asyncio/windows_events.py | 82 ++++--------------- .../2020-07-08-20-32-13.bpo-41246.2trYf3.rst | 3 + 2 files changed, 20 insertions(+), 65 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-07-08-20-32-13.bpo-41246.2trYf3.rst diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py index c07fe3241c5..28a7c0714a8 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -437,6 +437,17 @@ class IocpProactor: fut.set_result(value) return fut + @staticmethod + def finish_socket_func(trans, key, ov): + try: + return ov.getresult() + except OSError as exc: + if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, + _overlapped.ERROR_OPERATION_ABORTED): + raise ConnectionResetError(*exc.args) + else: + raise + def recv(self, conn, nbytes, flags=0): self._register_with_iocp(conn) ov = _overlapped.Overlapped(NULL) @@ -448,17 +459,7 @@ class IocpProactor: except BrokenPipeError: return self._result(b'') - def finish_recv(trans, key, ov): - try: - return ov.getresult() - except OSError as exc: - if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, - _overlapped.ERROR_OPERATION_ABORTED): - raise ConnectionResetError(*exc.args) - else: - raise - - return self._register(ov, conn, finish_recv) + return self._register(ov, conn, self.finish_socket_func) def recv_into(self, conn, buf, flags=0): self._register_with_iocp(conn) @@ -471,17 +472,7 @@ class IocpProactor: except BrokenPipeError: return self._result(b'') - def finish_recv(trans, key, ov): - try: - return ov.getresult() - except OSError as exc: - if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, - _overlapped.ERROR_OPERATION_ABORTED): - raise ConnectionResetError(*exc.args) - else: - raise - - return self._register(ov, conn, finish_recv) + return self._register(ov, conn, self.finish_socket_func) def recvfrom(self, conn, nbytes, flags=0): self._register_with_iocp(conn) @@ -491,17 +482,7 @@ class IocpProactor: except BrokenPipeError: return self._result((b'', None)) - def finish_recv(trans, key, ov): - try: - return ov.getresult() - except OSError as exc: - if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, - _overlapped.ERROR_OPERATION_ABORTED): - raise ConnectionResetError(*exc.args) - else: - raise - - return self._register(ov, conn, finish_recv) + return self._register(ov, conn, self.finish_socket_func) def sendto(self, conn, buf, flags=0, addr=None): self._register_with_iocp(conn) @@ -509,17 +490,7 @@ class IocpProactor: ov.WSASendTo(conn.fileno(), buf, flags, addr) - def finish_send(trans, key, ov): - try: - return ov.getresult() - except OSError as exc: - if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, - _overlapped.ERROR_OPERATION_ABORTED): - raise ConnectionResetError(*exc.args) - else: - raise - - return self._register(ov, conn, finish_send) + return self._register(ov, conn, self.finish_socket_func) def send(self, conn, buf, flags=0): self._register_with_iocp(conn) @@ -529,17 +500,7 @@ class IocpProactor: else: ov.WriteFile(conn.fileno(), buf) - def finish_send(trans, key, ov): - try: - return ov.getresult() - except OSError as exc: - if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, - _overlapped.ERROR_OPERATION_ABORTED): - raise ConnectionResetError(*exc.args) - else: - raise - - return self._register(ov, conn, finish_send) + return self._register(ov, conn, self.finish_socket_func) def accept(self, listener): self._register_with_iocp(listener) @@ -610,16 +571,7 @@ class IocpProactor: offset_low, offset_high, count, 0, 0) - def finish_sendfile(trans, key, ov): - try: - return ov.getresult() - except OSError as exc: - if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED, - _overlapped.ERROR_OPERATION_ABORTED): - raise ConnectionResetError(*exc.args) - else: - raise - return self._register(ov, sock, finish_sendfile) + return self._register(ov, sock, self.finish_socket_func) def accept_pipe(self, pipe): self._register_with_iocp(pipe) diff --git a/Misc/NEWS.d/next/Library/2020-07-08-20-32-13.bpo-41246.2trYf3.rst b/Misc/NEWS.d/next/Library/2020-07-08-20-32-13.bpo-41246.2trYf3.rst new file mode 100644 index 00000000000..17b884e777e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-08-20-32-13.bpo-41246.2trYf3.rst @@ -0,0 +1,3 @@ +Give the same callback function for when the overlapped operation is done to +the functions ``recv``, ``recv_into``, ``recvfrom``, ``sendto``, ``send`` +and ``sendfile`` inside ``IocpProactor``.