ayncio, Tulip issue 129: BaseEventLoop.sock_connect() now raises an error if
the address is not resolved (hostname instead of an IP address) for AF_INET and
AF_INET6 address families.
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 7d12042..3bbf6b5 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -41,6 +41,31 @@
"""Raised to stop the event loop."""
+def _check_resolved_address(sock, address):
+ # Ensure that the address is already resolved to avoid the trap of hanging
+ # the entire event loop when the address requires doing a DNS lookup.
+ family = sock.family
+ if family not in (socket.AF_INET, socket.AF_INET6):
+ return
+
+ host, port = address
+ type_mask = 0
+ if hasattr(socket, 'SOCK_NONBLOCK'):
+ type_mask |= socket.SOCK_NONBLOCK
+ if hasattr(socket, 'SOCK_CLOEXEC'):
+ type_mask |= socket.SOCK_CLOEXEC
+ # Use getaddrinfo(AI_NUMERICHOST) to ensure that the address is
+ # already resolved.
+ try:
+ socket.getaddrinfo(host, port,
+ family=family,
+ type=(sock.type & ~type_mask),
+ proto=sock.proto,
+ flags=socket.AI_NUMERICHOST)
+ except socket.gaierror as err:
+ raise ValueError("address must be resolved (IP address), got %r: %s"
+ % (address, err))
+
def _raise_stop_error(*args):
raise _StopError