Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-29757: don't swallows errors in the socket.create_connection() utility loop #562

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
"""

host, port = address
err = None
errors = []
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
af, socktype, proto, canonname, sa = res
sock = None
Expand All @@ -713,13 +713,15 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
sock.connect(sa)
return sock

except error as _:
err = _
except error as ex:
errors.append(ex)
if sock is not None:
sock.close()

if err is not None:
raise err
nerr = len(errors)
if nerr == 1:
raise errors[0]
elif nerr > 1:
raise error("no connection possible due to %d errors" % nerr, errors)
else:
raise error("getaddrinfo returns an empty list")

Expand Down