From ab54b8569d50cf5e00c0a726d600eb2e8cadd703 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 24 Jun 2025 07:14:14 +0100 Subject: [PATCH 1/3] gh-135836: take into account staggered exceptions --- Lib/asyncio/base_events.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 04fb961e9985e4..06a00a0656639f 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -1131,6 +1131,7 @@ async def create_connection( infos = _interleave_addrinfos(infos, interleave) exceptions = [] + staggered_exceptions = [] if happy_eyeballs_delay is None: # not using happy eyeballs for addrinfo in infos: @@ -1141,7 +1142,7 @@ async def create_connection( except OSError: continue else: # using happy eyeballs - sock = (await staggered.staggered_race( + sock, _, staggered_exceptions = (await staggered.staggered_race( ( # can't use functools.partial as it keeps a reference # to exceptions @@ -1152,10 +1153,15 @@ async def create_connection( ), happy_eyeballs_delay, loop=self, - ))[0] # can't use sock, _, _ as it keeks a reference to exceptions + )) if sock is None: exceptions = [exc for sub in exceptions for exc in sub] + for exc in staggered_exceptions: + if not (isinstance(exc, CancelledError) or exc in exceptions): + exceptions.append(exc) + del exc + try: if all_errors: raise ExceptionGroup("create_connection failed", exceptions) @@ -1172,6 +1178,7 @@ async def create_connection( ', '.join(str(exc) for exc in exceptions))) finally: exceptions = None + staggered_exceptions = None else: if sock is None: From d48e9e17d93fa9533c677e14895cd955b4eb2326 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 24 Jun 2025 06:18:57 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst b/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst new file mode 100644 index 00000000000000..154fbd6bfa78ee --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst @@ -0,0 +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, by capturing any exceptions raises by ``staggered_race`` From a03895fbd37eae426652dccc87c5dfde4de18687 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 24 Jun 2025 07:19:41 +0100 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst --- .../next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst b/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst index 154fbd6bfa78ee..1ce37aa42a359a 100644 --- a/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst +++ b/Misc/NEWS.d/next/Library/2025-06-24-06-18-49.gh-issue-135836.7-KHAd.rst @@ -1 +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, by capturing any exceptions raises by ``staggered_race`` +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, by capturing any exceptions raised by ``staggered_race``