Skip to content

systemd-resolve hits watchdog timeout when /etc/hosts contains many entries #9718

Closed

Description

@rpost

systemd version the issue has been seen with

237

Used distribution

Linux Mint 19

Expected behaviour you didn't see

network name resolution happening in finite time

Unexpected behaviour you saw

network name resolution timeouting
systemd-resolve keeps running with 100% cpu utilization

Steps to reproduce the problem

  1. add 1 million entries to /etc/hosts
  2. try to execute host google.com

Activity

  1. yuwata commented on Jul 26, 2018

    @yuwata
    Member
    1. add 1 million entries to /etc/hosts

    Wow...

  2. filbranden commented on Jul 26, 2018

    @filbranden
    Contributor
    1. add 1 million entries to /etc/hosts

    Wow...

    Well, for a while there, this was a somewhat common practice... I hoped these times were behind us now... maybe?

  3. yuwata commented on Jul 26, 2018

    @yuwata
    Member

    somewhat common practice

    I've confirmed this with the hosts file provided in the above link...

  4. yuwata commented on Jul 26, 2018

    @yuwata
    Member

    A workaround for this is adding an option to toggle reading /etc/hosts in resolved.conf, e.g., ReadEtcHosts=[BOOL]. What do you think?

  5. yuwata commented on Jul 26, 2018

    @yuwata
    Member

    Hmm... With 100000 lines of a blacklist /etc/hosts,

    • 23sec for reading manager_etc_hosts_read().
    • 10sec for reading manager_etc_hosts_read() with commenting out hashmap_put() in add_item().
  6. yuwata commented on Jul 28, 2018

    @yuwata
    Member

    I am not sure whether it is a appropriate way or not, but introduced a workaround for this in #9742.

  7. poettering commented on Jul 30, 2018

    @poettering
    Member

    I don't think #9742 should be considered a "fix"...

    I am not sure the usecase is particularly valid (I mean, who does this?). That said, work on optimizing this certainly would be welcome.

    Does nss-files actually work better for this kind of stuff? I somehow doubt that, given that on each and every single lookup it will parse and iterate through the file again and again

  8. keszybz commented on Aug 1, 2018

    @keszybz
    Member

    I looked into this. On top of @yuwata's cleanups, I made the storing of "blacklist" entries slightly more efficient, so that here reading of hosts with ~1.3e6 entries takes less time and uses less memory (about 25% less).

    In effect, reading of /etc/hosts is about 3 times slower than a libnss_files.so.2 query for a filename at the end of file (which forces the reading of the whole file). In our code, a lot of time is spent in the parsing functions:

        14.34%  test-resolved-e  libsystemd-shared-239.so  [.] dns_label_unescape
         8.18%  test-resolved-e  libsystemd-shared-239.so  [.] sipround
         6.46%  test-resolved-e  libsystemd-shared-239.so  [.] rotate_left
         5.37%  test-resolved-e  libsystemd-shared-239.so  [.] siphash24_compress
         5.27%  test-resolved-e  libc-2.27.so              [.] __strchr_avx2
         4.30%  test-resolved-e  libsystemd-shared-239.so  [.] extract_first_word
         3.88%  test-resolved-e  libsystemd-shared-239.so  [.] hashmap_put_robin_hood
         3.49%  test-resolved-e  libsystemd-shared-239.so  [.] base_bucket_scan
         3.49%  test-resolved-e  libc-2.27.so              [.] malloc_consolidate
         3.37%  test-resolved-e  libsystemd-shared-239.so  [.] dns_label_escape
         3.05%  test-resolved-e  libc-2.27.so              [.] __strlen_avx2
         2.95%  test-resolved-e  libsystemd-shared-239.so  [.] ascii_strlower_n
         2.37%  test-resolved-e  libc-2.27.so              [.] cfree@GLIBC_2.2.5
         2.17%  test-resolved-e  libc-2.27.so              [.] __memmove_avx_unaligned_erms
         2.01%  test-resolved-e  libsystemd-shared-239.so  [.] greedy_realloc
         1.73%  test-resolved-e  libsystemd-shared-239.so  [.] ascii_tolower
         1.34%  test-resolved-e  libsystemd-shared-239.so  [.] delete_trailing_chars
         1.24%  test-resolved-e  libsystemd-shared-239.so  [.] next_idx
         1.20%  test-resolved-e  libc-2.27.so              [.] __strcasecmp_l_avx
         1.16%  test-resolved-e  libsystemd-shared-239.so  [.] dns_label_unescape_suffix
         1.11%  test-resolved-e  libsystemd-shared-239.so  [.] bucket_move_entry
    

    dns_label_unescape is indeed not very efficient. I tried to make it faster, but it just does a lot of unaligned memory accesses and comparisons, and I don't see a way to make it significantly faster without complicating things a lot. Apart from that we do a lot of small memory allocations and comparisons, which is slow too.

    Nevertheless, I think being 3 times slower than a simple function which flies through the file in a tight loop is acceptable. I expect allocating storage and copying all the data in bits should take at least as much time as simply reading the file, and we're not much slower than that. systemd-resolved could probably be made more memory-efficient by using a trie structure for the blacklisted strings. But having such a large hosts file is something of a special case, and I think our level of support is adequate. If somebody cares about this use case, they can work on the code and file a PR.

    --

    The original report suggests that resolved keeps reading the file. But it should read the file just once, and then should never read it again until it is modified. So with libnss_files each lookup takes e.g. 1.5s, and with libnss_resolve the first lookup takes 5s, and all subsequent ones are "instantaneous". @rpost do you see something different?

  9. added
    needs-reporter-feedback ❓
    on Aug 1, 2018
  10. rpost commented on Aug 1, 2018

    @rpost
    Author

    But it should read the file just once, and then should never read it again until it is modified.

    I ran host google.com and waited until cpu is calm again (took 3 minutes) - I assume it finished reading hosts file. Unfortunately this doesn't helped: running host command again resulted with timeout as well (and cpu running at 100% for 2 mins this time).

    So with libnss_files each lookup takes e.g. 1.5s, and with libnss_resolve the first lookup takes 5s, and all subsequent ones are "instantaneous".

    I'm sorry but I don't know what this libnss stuff is. If you are asking me how it was behaving before I started using systemd-resolve (reads: on my Mint 18.3) then each name resolution took about 0.5 sec (for the names present in hosts file)

  11. floppym commented on Aug 1, 2018

    @floppym
    Contributor

    Running host google.com would completely bypass the /etc/hosts file without a resolv.conf entry pointed at 127.0.0.53. That's probably why it resolves so quickly.

    Could you try running getent hosts google.com on a system that doesn't use systemd-resolved? It will probably be much slower than the host command.

  12. rpost commented on Aug 1, 2018

    @rpost
    Author
    radek@t550 ~ $ time getent hosts google.com
    2a00:1450:401b:806::200e google.com
    
    real	0m0.219s
    user	0m0.208s
    sys	0m0.008s
    radek@t550 ~ $ time getent hosts 0-0-0-0-0-0-0-0-0-0-0-0-0-16-0-0-0-0-0-0-0-0-0-0-0-0-0.info #sample host from hosts file
    127.0.0.1       0-0-0-0-0-0-0-0-0-0-0-0-0-16-0-0-0-0-0-0-0-0-0-0-0-0-0.info
    
    real	0m0.639s
    user	0m0.521s
    sys	0m0.017s
    
  13. yuwata commented on Aug 2, 2018

    @yuwata
    Member

    @rpost Could you paste /etc/resolv.conf and /etc/nsswitch.conf ?

  14. keszybz commented on Aug 2, 2018

    @keszybz
    Member

    Please attach your /etc/hosts file too if possible and output of journalctl -bu systemd-resolved.

    I ran host google.com and waited until cpu is calm again (took 3 minutes) - I assume it finished reading hosts file. Unfortunately this doesn't helped: running host command again resulted with timeout as well (and cpu running at 100% for 2 mins this time).

    systemd-resolved.service has a watchdog timeout of 3 minutes. So it's possible that resolved gets killed by the watchdog, repeatedly. This would explain the original report of constant cpu utilization.

  15. floppym commented on Aug 2, 2018

    @floppym
    Contributor

    FWIW, I tested a resolved lookup with that million entry hosts file, and got a result back in around 5 seconds on the first query, and less than a second on subsequent queries. I wasn't able to reproduce the multi-minute delay.

  16. rpost commented on Aug 2, 2018

    @rpost
    Author
    $ cat /etc/resolv.conf 
    # This file is managed by man:systemd-resolved(8). Do not edit.
    #
    # This is a dynamic resolv.conf file for connecting local clients to the
    # internal DNS stub resolver of systemd-resolved. This file lists all
    # configured search domains.
    #
    # Run "systemd-resolve --status" to see details about the uplink DNS servers
    # currently in use.
    #
    # Third party programs must not access this file directly, but only through the
    # symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
    # replace this symlink by a static file or a different symlink.
    #
    # See man:systemd-resolved.service(8) for details about the supported modes of
    # operation for /etc/resolv.conf.
    
    nameserver 127.0.0.53
    
    $ cat /etc/nsswitch.conf 
    # /etc/nsswitch.conf
    #
    # Example configuration of GNU Name Service Switch functionality.
    # If you have the `glibc-doc-reference' and `info' packages installed, try:
    # `info libc "Name Service Switch"' for information about this file.
    
    passwd:         compat systemd
    group:          compat systemd
    shadow:         compat
    gshadow:        files
    
    hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
    networks:       files
    
    protocols:      db files
    services:       db files
    ethers:         db files
    rpc:            db files
    
    netgroup:       nis
    
    $ journalctl -b -u systemd-resolved
    -- Logs begin at Wed 2018-07-25 21:09:05 CEST, end at Thu 2018-08-02 22:30:06 CEST. --
    Aug 02 22:16:06 radek-VirtualBox systemd[1]: Starting Network Name Resolution...
    Aug 02 22:16:06 radek-VirtualBox systemd-resolved[359]: Positive Trust Anchors:
    Aug 02 22:16:06 radek-VirtualBox systemd-resolved[359]: . IN DS 19036 8 2 49aac11d7b6f6446702e54a1607371607a1a41855200fd2ce1cdde32f24e8fb5
    Aug 02 22:16:06 radek-VirtualBox systemd-resolved[359]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
    Aug 02 22:16:06 radek-VirtualBox systemd-resolved[359]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa 168.192.in-addr.arpa d.f.ip6.arpa corp home internal intranet lan local private test
    Aug 02 22:16:06 radek-VirtualBox systemd-resolved[359]: Using system hostname 'radek-VirtualBox'.
    Aug 02 22:16:06 radek-VirtualBox systemd[1]: Started Network Name Resolution.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Watchdog timeout (limit 3min)!
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Killing process 359 (systemd-resolve) with signal SIGABRT.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Main process exited, code=dumped, status=6/ABRT
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Failed with result 'watchdog'.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Service has no hold-off time, scheduling restart.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Scheduled restart job, restart counter is at 1.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: Stopped Network Name Resolution.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: Starting Network Name Resolution...
    Aug 02 22:19:06 radek-VirtualBox systemd-resolved[1951]: Positive Trust Anchors:
    Aug 02 22:19:06 radek-VirtualBox systemd-resolved[1951]: . IN DS 19036 8 2 49aac11d7b6f6446702e54a1607371607a1a41855200fd2ce1cdde32f24e8fb5
    Aug 02 22:19:06 radek-VirtualBox systemd-resolved[1951]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
    Aug 02 22:19:06 radek-VirtualBox systemd-resolved[1951]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa 168.192.in-addr.arpa d.f.ip6.arpa corp home internal intranet lan local private test
    Aug 02 22:19:06 radek-VirtualBox systemd-resolved[1951]: Using system hostname 'radek-VirtualBox'.
    Aug 02 22:19:06 radek-VirtualBox systemd[1]: Started Network Name Resolution.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Watchdog timeout (limit 3min)!
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Killing process 1951 (systemd-resolve) with signal SIGABRT.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Main process exited, code=dumped, status=6/ABRT
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Failed with result 'watchdog'.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Service has no hold-off time, scheduling restart.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Scheduled restart job, restart counter is at 2.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: Stopped Network Name Resolution.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: Starting Network Name Resolution...
    Aug 02 22:22:06 radek-VirtualBox systemd-resolved[2348]: Positive Trust Anchors:
    Aug 02 22:22:06 radek-VirtualBox systemd-resolved[2348]: . IN DS 19036 8 2 49aac11d7b6f6446702e54a1607371607a1a41855200fd2ce1cdde32f24e8fb5
    Aug 02 22:22:06 radek-VirtualBox systemd-resolved[2348]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
    Aug 02 22:22:06 radek-VirtualBox systemd-resolved[2348]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa 168.192.in-addr.arpa d.f.ip6.arpa corp home internal intranet lan local private test
    Aug 02 22:22:06 radek-VirtualBox systemd-resolved[2348]: Using system hostname 'radek-VirtualBox'.
    Aug 02 22:22:06 radek-VirtualBox systemd[1]: Started Network Name Resolution.
    Aug 02 22:25:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Watchdog timeout (limit 3min)!
    Aug 02 22:25:06 radek-VirtualBox systemd[1]: systemd-resolved.service: Killing process 2348 (systemd-resolve) with signal SIGABRT.
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Main process exited, code=dumped, status=6/ABRT
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Failed with result 'watchdog'.
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Service has no hold-off time, scheduling restart.
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Scheduled restart job, restart counter is at 3.
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: Stopped Network Name Resolution.
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: Starting Network Name Resolution...
    Aug 02 22:25:07 radek-VirtualBox systemd-resolved[2386]: Positive Trust Anchors:
    Aug 02 22:25:07 radek-VirtualBox systemd-resolved[2386]: . IN DS 19036 8 2 49aac11d7b6f6446702e54a1607371607a1a41855200fd2ce1cdde32f24e8fb5
    Aug 02 22:25:07 radek-VirtualBox systemd-resolved[2386]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
    Aug 02 22:25:07 radek-VirtualBox systemd-resolved[2386]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa 168.192.in-addr.arpa d.f.ip6.arpa corp home internal intranet lan local private test
    Aug 02 22:25:07 radek-VirtualBox systemd-resolved[2386]: Using system hostname 'radek-VirtualBox'.
    Aug 02 22:25:07 radek-VirtualBox systemd[1]: Started Network Name Resolution.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Watchdog timeout (limit 3min)!
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Killing process 2386 (systemd-resolve) with signal SIGABRT.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Main process exited, code=dumped, status=6/ABRT
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Failed with result 'watchdog'.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Service has no hold-off time, scheduling restart.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: systemd-resolved.service: Scheduled restart job, restart counter is at 4.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: Stopped Network Name Resolution.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: Starting Network Name Resolution...
    Aug 02 22:28:07 radek-VirtualBox systemd-resolved[2449]: Positive Trust Anchors:
    Aug 02 22:28:07 radek-VirtualBox systemd-resolved[2449]: . IN DS 19036 8 2 49aac11d7b6f6446702e54a1607371607a1a41855200fd2ce1cdde32f24e8fb5
    Aug 02 22:28:07 radek-VirtualBox systemd-resolved[2449]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
    Aug 02 22:28:07 radek-VirtualBox systemd-resolved[2449]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa 168.192.in-addr.arpa d.f.ip6.arpa corp home internal intranet lan local private test
    Aug 02 22:28:07 radek-VirtualBox systemd-resolved[2449]: Using system hostname 'radek-VirtualBox'.
    Aug 02 22:28:07 radek-VirtualBox systemd[1]: Started Network Name Resolution.
    

    Indeed: systemd[1]: systemd-resolved.service: Watchdog timeout (limit 3min)!. hosts.zip attached.

  17. allen-munsch commented on Nov 14, 2018

    @allen-munsch

    fwiw : being inspired by https://github.com/pi-hole/pi-hole

    I have also attempted a huge blacklist/whitelist using systemd, but have yet to find a solution using systemd-resolved

    For more context on the particulars of what I had attempted : https://unix.stackexchange.com/questions/479600/systemd-resolved-block-a-domain-name

  18. removed
    needs-reporter-feedback ❓
    on Nov 15, 2018
  19. changed the title [-]systemd-resolve hangs when /etc/hosts contains many entries[/-] [+]systemd-resolve hits watchdog timeout when /etc/hosts contains many entries[/+] on Nov 15, 2018
  20. keszybz commented on Nov 15, 2018

    @keszybz
    Member

    It seems that @rpost's system is just very very slow. On my machine, with git master it takes about 4s to read the long /etc/hosts file. Even if the system was 10 times slower, that still only 40 s, and the timeout is 180 s. So maybe it's not just slow, but something else is going on.

    @rpost: what if you add

    # /etc/systemd/system/systemd-resolved.service/10-timeout.conf
    [Service]
    WatchdogSec=

    Does this solve the issue?

  21. added
    needs-reporter-feedback ❓
    on Nov 15, 2018
  22. poettering commented on Jul 10, 2019

    @poettering
    Member

    Let's close this for now, as optimizations have been added, and there hasn't been a reply in a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-reporter-feedback ❓resolvescalability

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions