Add cycle-breaking logic to create_connection()
This commit synchronizes create_connection() with the implementation in the socket module
@@ -74,17 +74,22 @@ def create_connection(
if source_address:
sock.bind(source_address)
sock.connect(sa)
+ # Break explicitly a reference cycle
+ err = None
return sock
- except OSError as e:
- err = e
+ except OSError as _:
+ err = _
if sock is not None:
sock.close()
- sock = None
if err is not None:
+ try:
raise err
-
+ finally:
+ # Break explicitly a reference cycle
+ err = None
+ else:
raise OSError("getaddrinfo returns an empty list")
