Skip to content

Commit ad7e39c

Browse files
committed
Fix IndexError in asyncio.create_connection with empty exceptions list
Fix IndexError that occurs in asyncio.create_connection() when Happy Eyeballs algorithm leaves empty sublists in the exceptions list, which after flattening becomes an empty list causing str(exceptions[0]) to crash. The fix adds explicit handling for empty exceptions list by changing 'else:' to 'elif len(exceptions) > 1:' and adding a new else clause that raises OSError('create_connection failed') instead of crashing.
1 parent 396ca9a commit ad7e39c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Lib/asyncio/base_events.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ async def create_connection(
11611161
raise ExceptionGroup("create_connection failed", exceptions)
11621162
if len(exceptions) == 1:
11631163
raise exceptions[0]
1164-
else:
1164+
elif len(exceptions) > 1:
11651165
# If they all have the same str(), raise one.
11661166
model = str(exceptions[0])
11671167
if all(str(exc) == model for exc in exceptions):
@@ -1170,6 +1170,9 @@ async def create_connection(
11701170
# the various error messages.
11711171
raise OSError('Multiple exceptions: {}'.format(
11721172
', '.join(str(exc) for exc in exceptions)))
1173+
else:
1174+
# No exceptions were collected, raise a generic connection error
1175+
raise OSError('create_connection failed')
11731176
finally:
11741177
exceptions = None
11751178

0 commit comments

Comments
 (0)