Skip to content

Commit ad60912

Browse files
committed
Record connected sockets in _connect_sock()
Pass a list to _connect_sock() instead of wrapping it, as suggested in review.
1 parent b6aeaf9 commit ad60912

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎Lib/asyncio/base_events.py‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,12 @@ def _check_sendfile_params(self, sock, file, offset, count):
10201020
"offset must be a non-negative integer (got {!r})".format(
10211021
offset))
10221022

1023-
async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
1024-
"""Create, bind and connect one socket."""
1023+
async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None,
1024+
connected_sockets=None):
1025+
"""Create, bind and connect one socket.
1026+
1027+
If *connected_sockets* is a list, the connected socket is appended to it.
1028+
"""
10251029
my_exceptions = []
10261030
exceptions.append(my_exceptions)
10271031
family, type_, proto, _, address = addr_info
@@ -1051,6 +1055,8 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
10511055
else:
10521056
raise OSError(f"no matching local address with {family=} found")
10531057
await self.sock_connect(sock, address)
1058+
if connected_sockets is not None:
1059+
connected_sockets.append(sock)
10541060
return sock
10551061
except OSError as exc:
10561062
my_exceptions.append(exc)
@@ -1160,19 +1166,14 @@ async def create_connection(
11601166
# Sockets connected by the attempts, so that the winner is
11611167
# closed if the race is cancelled before it returns.
11621168
connected = []
1163-
1164-
async def connect_sock(addrinfo):
1165-
sock = await self._connect_sock(
1166-
exceptions, addrinfo, laddr_infos)
1167-
connected.append(sock)
1168-
return sock
1169-
11701169
try:
11711170
sock = (await staggered.staggered_race(
11721171
(
11731172
# can't use functools.partial as it keeps a
11741173
# reference to exceptions
1175-
lambda addrinfo=addrinfo: connect_sock(addrinfo)
1174+
lambda addrinfo=addrinfo: self._connect_sock(
1175+
exceptions, addrinfo, laddr_infos, connected
1176+
)
11761177
for addrinfo in infos
11771178
),
11781179
happy_eyeballs_delay,

‎Lib/test/test_asyncio/test_base_events.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,9 +1421,11 @@ def test_create_connection_happy_eyeballs_cancelled_closes_winner(self):
14211421
async def getaddrinfo(*args, **kw):
14221422
return [(socket.AF_INET, socket.SOCK_STREAM, 0, '', ('192.0.2.1', 80))]
14231423

1424-
async def connect_sock(exceptions, addr_info, local_addr_infos=None):
1424+
async def connect_sock(exceptions, addr_info, local_addr_infos=None,
1425+
connected_sockets=None):
14251426
nonlocal connected
14261427
connected = True
1428+
connected_sockets.append(sock)
14271429
return sock
14281430

14291431
self.loop.getaddrinfo = getaddrinfo

0 commit comments

Comments
 (0)