mercredi 4 mars 2015

Why is my hostname resolution taking so long?


Several months ago I noticed that my text editor (emacs) and IDE (IntelliJ) were taking a really long time to start up. The time appeared to vary based on the DNS servers OS X was using.


I was able to isolate the issue when a project's test suite was running slowly. I found the (higher level) culprit to be a call to socket.getfqdn().


Running the following command in the terminal on OS X 10.10.2, demonstrates the problem:



$ time python -c 'import socket; socket.getfqdn()'
python -c 'import socket; socket.getfqdn()' 0.02s user 0.00s system 0% cpu 5.122 total


I traced the code that runs when socket.getfqdn() is called and the delay is caused by getaddrinfo(3). I wrote a small program that isolates the problem and gai_strerror(3) indicates provides this message:



$ time ./hostinfo
Hostname: MacBook-Pro.local
getaddrinfo: nodename nor servname provided, or not known
./hostinfo 0.00s user 0.00s system 0% cpu 5.101 total


It seems like the delay is waiting for the DNS query to time out. The above results were using Google's public DNS servers. If I use my ISP's DNS servers, however, the time increases to 30 seconds:



$ time python -c 'import socket; socket.getfqdn()'
python -c 'import socket; socket.getfqdn()' 0.01s user 0.01s system 0% cpu 30.114 total


(curiously the C program hostinfo still takes just above 5 seconds)


What is causing this issue? Is my hostname invalid or causing problems?



$ hostname
MacBook-Pro


This problem doesn't happen on a Macbook Air on the same network.


The major difference I can see is that on the problematic machine, the following DNS confiugration is listed:



$ scutil --dns
DNS configuration

resolver #1
search domain[0] : Home
nameserver[0] : 8.8.8.8
nameserver[1] : 8.8.4.4
flags : Request A records
reach : Reachable

DNS configuration (for scoped queries)

resolver #1
search domain[0] : Home
nameserver[0] : 8.8.8.8
nameserver[1] : 8.8.4.4
if_index : 4 (en0)
flags : Scoped, Request A records
reach : Reachable


On the Macbook Air, several extra entries are included that relate to mDNS. For example:



resolver #2
domain : local
options : mdns
timeout : 5
flags : Request A records
order : 300000


Interestingly, the timeout listed above is about the same as the runtimes above.





Aucun commentaire:

Enregistrer un commentaire