23

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.

2
  • 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?
    – athena
    Commented Mar 4, 2015 at 18:30
  • This points to Apple doing the wrong DNS queries in some circumstances (and then waiting until they time out). Which begs the question: why are they getting it wrong? Commented Jun 8, 2021 at 13:57

4 Answers 4

23

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
1
  • 1
    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). Commented Oct 28, 2016 at 22:34
9

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.

3

Don't use the .local top level domain. Use .home.arpa instead.

RFC 6762 specifies:

... that the DNS top-level domain ".local." is a special domain with special semantics...

Any DNS query for a name ending with ".local." MUST be sent to the mDNS IPv4 link-local multicast address 224.0.0.251 (or its IPv6 equivalent FF02::FB).

On macOS this uses Bonjour and from what I have experienced is that after 1 minute of inactivity it will take about 10 seconds to resolve a .local name. Presumably it is trying to find the service via Bonjour and only falling back to DNS after a 10 second timeout.

I did some digging and found RFC 8375 which specifies that .home.arpa:

... is designated for non-unique use in residential home networks.

I'm not sure why it says residential. The RFC for Home Networking Control Protocol doesn't make a distinction between residential and non-residential "home" networks.

1
  • I keep running into this and keep forgetting. It is taking golang net.Dial 5+ seconds to resolve and I thought it was a code issue until I narrowed it to a dns issue.
    – Scott
    Commented Nov 2, 2022 at 22:57
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 .