Title: HostsFileParser: lookups during a hosts file reload miss existing entries · Issue #429 · dnsjava/dnsjava · GitHub
Open Graph Title: HostsFileParser: lookups during a hosts file reload miss existing entries · Issue #429 · dnsjava/dnsjava
X Title: HostsFileParser: lookups during a hosts file reload miss existing entries · Issue #429 · dnsjava/dnsjava
Description: Summary HostsFileParser reloads a changed hosts file by clearing its maps in place and then refilling them. Lookups read those maps without the lock, so a lookup that runs between the clear and the refill gets Optional.empty() for a name...
Open Graph Description: Summary HostsFileParser reloads a changed hosts file by clearing its maps in place and then refilling them. Lookups read those maps without the lock, so a lookup that runs between the clear and the...
X Description: Summary HostsFileParser reloads a changed hosts file by clearing its maps in place and then refilling them. Lookups read those maps without the lock, so a lookup that runs between the clear and the...
Opengraph URL: https://github.com/dnsjava/dnsjava/issues/429
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"HostsFileParser: lookups during a hosts file reload miss existing entries","articleBody":"## Summary\n\n`HostsFileParser` reloads a changed hosts file by clearing its maps in place and then refilling them. Lookups read those maps without the lock, so a lookup that runs between the clear and the refill gets `Optional.empty()` for a name that is in both the old and the new file. The first read of the file has the same window. Since v3.6.4.\n\nImpact: for each lookup that coincides with a reload, `Lookup` falls through to DNS (usually NXDOMAIN) for a name the hosts file defines. The window opens on every reload, and more often the lower `dnsjava.hostsfile.change_check_interval_ms` is and the more threads resolve.\n\nA second, smaller bug in the same code: `isEntireFileParsed` is never reset, so if the file grows past `dnsjava.hostsfile.max_size_bytes` after a full parse, every name not already cached is reported absent until the JVM restarts.\n\n## How it was found\n\n[DNSControl](https://github.com/OpenPojo/dnscontrol) routes JVM-wide DNS through dnsjava's `Lookup` and honours `jdk.net.hosts.file` via `HostsFileParser`. Its quality pass ([OpenPojo/dnscontrol#23](https://github.com/OpenPojo/dnscontrol/issues/23)) added [a concurrency test](https://github.com/OpenPojo/dnscontrol/blob/0c74e4bdba66b8112d847cff880f1e30148cc25e/src/test/java/com/openpojo/dns/HostsFileReloadConcurrencyTest.java): 32 threads run `new Lookup(\"racing.a.test.\", Type.A).run()` while the main thread rewrites the hosts file between two addresses. Every answer must be one of the two addresses. Files are replaced atomically (write aside, `ATOMIC_MOVE`), so half-written files are ruled out, and `dnsjava.hostsfile.change_check_interval_ms=0`.\n\nAgainst dnsjava 3.7.0's parser (macOS, Corretto 21.0.6), runs that failed with a lookup getting no answer:\n\n| Rewrites | Pause between rewrites | Failed runs |\n|---|---|---|\n| 5 | 50 ms | 0 / 8 |\n| 300 | 2 ms | 1 / 5 |\n| 1000 | 0 | 3 / 5 |\n| 3000 | 0 | 8 / 10 |\n\nDNSControl now works around it ([`HostsFileWatcher`](https://github.com/OpenPojo/dnscontrol/blob/0c74e4bdba66b8112d847cff880f1e30148cc25e/src/main/java/com/openpojo/dns/hosts/HostsFileWatcher.java)): it builds a new `HostsFileParser(path, false)`, reads it once, then swaps it into `Lookup` whole. Result: 0 / 20 at 3000 rewrites. We would rather drop that workaround once dnsjava's own reload is safe.\n\n## Root cause\n\nOn `master` ([9e26eba2](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java)):\n\n1. [`validateCache()` L324-L326](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java#L324-L326) returns without the lock while the check interval has not elapsed.\n2. The reloading thread sets [`lastFileModificationCheckTime` (L341)](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java#L341) before reading, so every other thread takes that fast path for the whole reload.\n3. [`readHostsFile()` L351](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java#L351) calls [`createOrClearCache()` L366-L374](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java#L366-L374), which clears the live maps (or, on the first read, publishes new empty ones) before `parseEntireHostsFile()` fills them.\n4. Meanwhile [`getAddressForHost` L115](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java#L115) reads the map and finds nothing.\n\n`isEntireFileParsed` is only ever set to `true` ([L356](https://github.com/dnsjava/dnsjava/blob/9e26eba2832e197d91ba28c91f7a23207faa4726/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java#L356)), which is the second bug.\n\nThe unlocked path came from 56099984 (\"Reduce lock contention and disk access in hosts file parsing\", #369), first released in v3.6.4; before it, `getAddressForHost` was `synchronized`. #400 (an NPE on `hostsCache`) is another symptom of the same unlocked read.\n\n## Deterministic reproduction\n\nThree tests in `HostsFileParserTest` (in the PR that follows). The first two pause the reload inside the file read with a spied `FileSystemProvider` (the same technique as the existing `handleNoModificationTime`) and look up in that window, using a mocked `Clock` to stay on the fast path. No sleeps or loops:\n\n- `testLookupDuringReloadFindsUnchangedEntry`: a lookup during a reload must find an entry present in both versions.\n- `testLookupDuringFirstReadWaitsForIt`: a lookup during the first read must wait for it instead of answering from the empty map.\n- `testFileGrownBeyondFullCacheSizeIsSearched`: after a small file is replaced by one over the full-cache limit, names only in the new file must be found.\n\nAgainst `master`'s `HostsFileParser`, all 27 tests run, and only these 3 fail, with no retries, on Corretto 11.0.15, 21.0.6 and 25:\n\n```\norg.xbill.DNS.hosts.HostsFileParserTest.testLookupDuringReloadFindsUnchangedEntry \u003c\u003c\u003c FAILURE!\njava.lang.AssertionError:\nExpecting Optional to contain:\n kubernetes.docker.internal./127.0.0.1\nbut was empty.\n\norg.xbill.DNS.hosts.HostsFileParserTest.testLookupDuringFirstReadWaitsForIt \u003c\u003c\u003c FAILURE!\njava.lang.AssertionError:\nExpecting Optional to contain:\n kubernetes.docker.internal./127.0.0.1\nbut was empty.\n\norg.xbill.DNS.hosts.HostsFileParserTest.testFileGrownBeyondFullCacheSizeIsSearched \u003c\u003c\u003c FAILURE!\njava.lang.AssertionError:\nExpecting Optional to contain:\n localhost-10./127.0.0.10\nbut was empty.\n```\n\nA fix with these tests follows as a PR.\n","author":{"url":"https://github.com/oshoukry","@type":"Person","name":"oshoukry"},"datePublished":"2026-11-02T06:47:38Z","url":"https://github.com/dnsjava/dnsjava/issues/429"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e5e370a3-070b-7866-f3e9-888392dba917 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E200:17E4EA:67F362:8A742A:6B0629DA |
| html-safe-nonce | 15094e6e01042109fdde059ba71368e47051cc626708a4fe28828487a120fc4d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMjAwOjE3RTRFQTo2N0YzNjI6OEE3NDJBOjZCMDYyOURBIiwidmlzaXRvcl9pZCI6IjY0ODYzNDk5MTgxOTcxMzE3MzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 574aa345000a04934525b6fddcff171ec267e0e8d05e1c0129b9b60c95e3fd03 |
| hovercard-subject-tag | issue:6157694063 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/dnsjava/dnsjava/429/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a9dc77f4bcb63dbb33929d364f210969912c113a213426576bfd080642ce2dde/dnsjava/dnsjava/issues/429 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a9dc77f4bcb63dbb33929d364f210969912c113a213426576bfd080642ce2dde/dnsjava/dnsjava/issues/429 |
| og:image:alt | Summary HostsFileParser reloads a changed hosts file by clearing its maps in place and then refilling them. Lookups read those maps without the lock, so a lookup that runs between the clear and the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | oshoukry |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7f961de5dd5c5d4aca24d33f8b3ffb3fb8b2ef53d0a4f32f01f0a790e512f6d3 |
| turbo-cache-control | no-preview |
| go-import | github.com/dnsjava/dnsjava git https://github.com/dnsjava/dnsjava.git |
| octolytics-dimension-user_id | 6755615 |
| octolytics-dimension-user_login | dnsjava |
| octolytics-dimension-repository_id | 17084567 |
| octolytics-dimension-repository_nwo | dnsjava/dnsjava |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 17084567 |
| octolytics-dimension-repository_network_root_nwo | dnsjava/dnsjava |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 494ea0bb1e0feef734a83e2628697647755e8864 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width