Skip to content

Commit ac8eb8f

Browse files
lepaperwanmiss-islington
authored andcommitted
bpo-35545: Fix asyncio discarding IPv6 scopes (GH-11271)
This PR proposes a solution to [bpo-35545](https://bugs.python.org/issue35545) by adding an optional `flowinfo` and `scopeid` to `asyncio.base_events._ipaddr_info` to carry the full address information into `_ipaddr_info` and avoid discarding IPv6 specific information. Changelog entry & regression tests to come. https://bugs.python.org/issue35545
1 parent 14514d9 commit ac8eb8f

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _set_reuseport(sock):
102102
'SO_REUSEPORT defined but not implemented.')
103103

104104

105-
def _ipaddr_info(host, port, family, type, proto):
105+
def _ipaddr_info(host, port, family, type, proto, flowinfo=0, scopeid=0):
106106
# Try to skip getaddrinfo if "host" is already an IP. Users might have
107107
# handled name resolution in their own code and pass in resolved IPs.
108108
if not hasattr(socket, 'inet_pton'):
@@ -151,7 +151,7 @@ def _ipaddr_info(host, port, family, type, proto):
151151
socket.inet_pton(af, host)
152152
# The host has already been resolved.
153153
if _HAS_IPv6 and af == socket.AF_INET6:
154-
return af, type, proto, '', (host, port, 0, 0)
154+
return af, type, proto, '', (host, port, flowinfo, scopeid)
155155
else:
156156
return af, type, proto, '', (host, port)
157157
except OSError:
@@ -1348,7 +1348,7 @@ async def _ensure_resolved(self, address, *,
13481348
family=0, type=socket.SOCK_STREAM,
13491349
proto=0, flags=0, loop):
13501350
host, port = address[:2]
1351-
info = _ipaddr_info(host, port, family, type, proto)
1351+
info = _ipaddr_info(host, port, family, type, proto, *address[2:])
13521352
if info is not None:
13531353
# "host" is already a resolved IP.
13541354
return [info]

Lib/test/test_asyncio/test_base_events.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,28 @@ def _test_create_connection_ip_addr(self, m_socket, allow_inet_pton):
12991299
t.close()
13001300
test_utils.run_briefly(self.loop) # allow transport to close
13011301

1302+
@patch_socket
1303+
def test_create_connection_ipv6_scope(self, m_socket):
1304+
m_socket.getaddrinfo = socket.getaddrinfo
1305+
sock = m_socket.socket.return_value
1306+
sock.family = socket.AF_INET6
1307+
1308+
self.loop._add_reader = mock.Mock()
1309+
self.loop._add_reader._is_coroutine = False
1310+
self.loop._add_writer = mock.Mock()
1311+
self.loop._add_writer._is_coroutine = False
1312+
1313+
coro = self.loop.create_connection(asyncio.Protocol, 'fe80::1%1', 80)
1314+
t, p = self.loop.run_until_complete(coro)
1315+
try:
1316+
sock.connect.assert_called_with(('fe80::1', 80, 0, 1))
1317+
_, kwargs = m_socket.socket.call_args
1318+
self.assertEqual(kwargs['family'], m_socket.AF_INET6)
1319+
self.assertEqual(kwargs['type'], m_socket.SOCK_STREAM)
1320+
finally:
1321+
t.close()
1322+
test_utils.run_briefly(self.loop) # allow transport to close
1323+
13021324
@patch_socket
13031325
def test_create_connection_ip_addr(self, m_socket):
13041326
self._test_create_connection_ip_addr(m_socket, True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix asyncio discarding IPv6 scopes when ensuring hostname resolutions
2+
internally

0 commit comments

Comments
 (0)