From d2ba8ea54a4089959afdeecdd963e3c4ff391748 Mon Sep 17 00:00:00 2001 From: KJ Tsanaktsidis Date: Thu, 7 Dec 2023 19:55:15 +1100 Subject: Set AI_ADDRCONFIG when making getaddrinfo(3) calls for outgoing conns (#7295) When making an outgoing TCP or UDP connection, set AI_ADDRCONFIG in the hints we send to getaddrinfo(3) (if supported). This will prompt the resolver to _NOT_ issue A or AAAA queries if the system does not actually have an IPv4 or IPv6 address (respectively). This makes outgoing connections marginally more efficient on non-dual-stack systems, since we don't have to try connecting to an address which can't possibly work. More importantly, however, this works around a race condition present in some older versions of glibc on aarch64 where it could accidently send the two outgoing DNS queries with the same DNS txnid, and get confused when receiving the responses. This manifests as outgoing connections sometimes taking 5 seconds (the DNS timeout before retry) to be made. Fixes #19144 --- ext/socket/udpsocket.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'ext/socket/udpsocket.c') diff --git a/ext/socket/udpsocket.c b/ext/socket/udpsocket.c index 5224e48a96..cf3eb6ab9a 100644 --- a/ext/socket/udpsocket.c +++ b/ext/socket/udpsocket.c @@ -88,9 +88,16 @@ udp_connect(VALUE sock, VALUE host, VALUE port) { struct udp_arg arg; VALUE ret; + int addrinfo_hints = 0; GetOpenFile(sock, arg.fptr); - arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0); + +#ifdef HAVE_CONST_AI_ADDRCONFIG + addrinfo_hints |= AI_ADDRCONFIG; +#endif + + arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, + addrinfo_hints); ret = rb_ensure(udp_connect_internal, (VALUE)&arg, rsock_freeaddrinfo, (VALUE)arg.res); if (!ret) rsock_sys_fail_host_port("connect(2)", host, port); -- cgit v1.2.3