Skip to content

Commit 0e2fb84

Browse files
committed
Add test asyncio.create_connection Happy Eyeballs empty exceptions and update NEWS entry.
1 parent 8e9a353 commit 0e2fb84

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lib/test/test_asyncio/test_base_events.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,37 @@ def getaddrinfo(*args, **kw):
11901190
self.loop.run_until_complete(coro)
11911191
self.assertTrue(sock.close.called)
11921192

1193+
@patch_socket
1194+
def test_create_connection_happy_eyeballs_empty_exceptions(self, m_socket):
1195+
# Test for gh-135836: Fix IndexError when Happy Eyeballs algorithm
1196+
# results in empty exceptions list
1197+
from unittest import mock
1198+
1199+
async def getaddrinfo(*args, **kw):
1200+
return [(socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 80)),
1201+
(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 80))]
1202+
1203+
def getaddrinfo_task(*args, **kwds):
1204+
return self.loop.create_task(getaddrinfo(*args, **kwds))
1205+
1206+
self.loop.getaddrinfo = getaddrinfo_task
1207+
1208+
# Mock staggered_race to return empty exceptions list
1209+
# This simulates the scenario where Happy Eyeballs algorithm
1210+
# cancels all attempts but doesn't properly collect exceptions
1211+
with mock.patch('asyncio.staggered.staggered_race') as mock_staggered:
1212+
# Return (None, []) - no winner, empty exceptions list
1213+
async def mock_race(coro_fns, delay, loop):
1214+
return None, []
1215+
mock_staggered.side_effect = mock_race
1216+
1217+
coro = self.loop.create_connection(
1218+
MyProto, 'example.com', 80, happy_eyeballs_delay=0.1)
1219+
1220+
# Should raise TimeoutError instead of IndexError
1221+
with self.assertRaises(TimeoutError):
1222+
self.loop.run_until_complete(coro)
1223+
11931224
def test_create_connection_host_port_sock(self):
11941225
coro = self.loop.create_connection(
11951226
MyProto, 'example.com', 80, sock=object())
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fixed :exc:`IndexError` in ``asyncio.create_connection()`` that could occur when the Happy Eyeballs algorithm resulted in an empty exceptions list during connection attempts.
1+
Fix :exc:`IndexError` in :meth:`asyncio.loop.create_connection` that could occur when the Happy Eyeballs algorithm resulted in an empty exceptions list during connection attempts.

0 commit comments

Comments
 (0)