gh-91227: Ignore ERROR_PORT_UNREACHABLE in proactor recvfrom() (#32011)

This commit is contained in:
Erik Soma 2024-03-23 11:39:35 -04:00 committed by GitHub
parent 9967b568ed
commit f11d0d8be8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 174 additions and 12 deletions

View file

@ -8,6 +8,7 @@ if sys.platform != 'win32': # pragma: no cover
import _overlapped
import _winapi
import errno
from functools import partial
import math
import msvcrt
import socket
@ -467,6 +468,18 @@ class IocpProactor:
else:
raise
@classmethod
def _finish_recvfrom(cls, trans, key, ov, *, empty_result):
try:
return cls.finish_socket_func(trans, key, ov)
except OSError as exc:
# WSARecvFrom will report ERROR_PORT_UNREACHABLE when the same
# socket is used to send to an address that is not listening.
if exc.winerror == _overlapped.ERROR_PORT_UNREACHABLE:
return empty_result, None
else:
raise
def recv(self, conn, nbytes, flags=0):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
@ -501,7 +514,8 @@ class IocpProactor:
except BrokenPipeError:
return self._result((b'', None))
return self._register(ov, conn, self.finish_socket_func)
return self._register(ov, conn, partial(self._finish_recvfrom,
empty_result=b''))
def recvfrom_into(self, conn, buf, flags=0):
self._register_with_iocp(conn)
@ -511,17 +525,8 @@ class IocpProactor:
except BrokenPipeError:
return self._result((0, 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, partial(self._finish_recvfrom,
empty_result=0))
def sendto(self, conn, buf, flags=0, addr=None):
self._register_with_iocp(conn)