Skip to content

python.sync_dns_lookup

Performance Medium

Detects socket.gethostbyname() or socket.getaddrinfo() in async functions.

DNS lookups are blocking operations:

  • Hidden latency — DNS resolution can take hundreds of milliseconds
  • Event loop blocked — No other tasks can run during the lookup
  • Resolver failures — If DNS resolvers are slow, everything stalls
  • No timeout control — System DNS has its own timeout, often too long

In async code, blocking DNS destroys the concurrency benefits you’re trying to achieve.

# ❌ Before
async def resolve_host(hostname):
return socket.gethostbyname(hostname)

This blocks the event loop for the entire DNS resolution.

# ✅ After
async def resolve_host(hostname):
loop = asyncio.get_event_loop()
return await loop.getaddrinfo(hostname, None)

getaddrinfo is async-aware and doesn’t block.

  • socket.gethostbyname() in async functions
  • socket.gethostbyname_ex() in async functions
  • socket.getaddrinfo() (without await) in async functions
  • socket.gethostbyaddr() in async functions

Unfault replaces blocking socket calls with async equivalents.

# Use asyncio's built-in resolver
loop = asyncio.get_event_loop()
result = await loop.getaddrinfo(host, port)
# Or use aiohttp which handles this internally
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
... # DNS is async
# For complex DNS needs, use aiodns
import aiodns
resolver = aiodns.DNSResolver()
result = await resolver.query(hostname, 'A')