getaddrinfo() blocks indefinitely when DNS query times out
0 reputation · 13 Aug 2025, 15:11 UTC
Goal: Apply a per‑call timeout to the getaddrinfo() function in a portable C program while correctly interpreting EAI_AGAIN (transient) and EAI_NONAME (permanent) results.
Constraints: The standard getaddrinfo() API lacks a timeout argument; blocking time is dictated by system resolver configuration (/etc/resolv.conf options on Linux, hard‑coded defaults in musl, or registry settings on Windows). Implementing a timeout therefore requires external mechanisms such as running the call in a worker thread with cancellation, using non‑blocking socket tricks, or delegating to an asynchronous DNS library. Variations in AI_ADDRCONFIG handling and thread‑cancellation safety across glibc, musl, and Windows add uncertainty.
Questions:
- What is the safest way to bound the blocking time of
getaddrinfo()across glibc, musl, and Windows implementations? - How should an application distinguish between transient (
EAI_AGAIN) and permanent (EAI_NONAME) failures when a timeout wrapper is in place? - Are there portability‑safe patterns for cancelling a thread blocked in
getaddrinfo()that avoid leaking resolver‑internal locks or file descriptors?