14

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) 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.local

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

This seems to be significant. Interestingly, the timeout listed above is about the same as the runtimes above.

I should note that I'm connected to the internet using WiFi and the problem only exists when trying to resolve my computer's host name.

  • Welcome on board n°67590. Please add in your question how your MBP is connected to the Internet (Ethernet, Wi-Fi, Bluetooth…). Did you try a basic ping my_ISP_DNS_server & ping Google_DNS_server? – dan Mar 4 '15 at 18:30
11

I was able to fix this issue by explicitly setting the HostName using scutil to be the same value as LocalHostName:

$ scutil --set HostName $(scutil --get LocalHostName)

Now:

$ sudo scutil --set HostName MacBook-Pro
$ time python -c 'import socket; print(socket.getfqdn())'
MacBook-Pro
python -c 'import socket; print(socket.getfqdn())'  0.01s user 0.00s system 86% cpu 0.016 total

I was confused before because of the following:

$ scutil --get LocalHostName
MacBook-Pro

$ hostname
MacBook-Pro.local

But:

$ scutil --get HostName
HostName: not set
  • As stated in the other answer it seems like to fix this not only must it be set but it must contain .local as well (or maybe just a . in general). – Keith Smiley Oct 28 '16 at 22:34
4

I originally had the same issue, but eventually resolved it.

I used to have:

$ scutil --get HostName
Michaels-Macbook-Pro
$ scutil --get LocalHostName
Michaels-Macbook-Pro
$ time python -c 'import socket; socket.getfqdn()'
... 30s ...

Then I changed:

$ scutil --set HostName Michaels-Macbook-Pro.local
$ time python -c 'import socket; socket.getfqdn()'
... 5s ...

Then I added into: /etc/hosts

127.0.0.1       localhost Michaels-Macbook-Pro.local
::1             localhost Michaels-Macbook-Pro.local

It became fast.

2

Had the same issue, but your suggestions didn't work for me.

Eventually, I fixed it by doing this once I did this:

sudo scutil --set HostName uranus.local

My machine host is 'uranus'. I only had to add '.local' to it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .