⚡ Sangsakala Proxy 🏠 Home
Skip to content

Prevent lookups from seeing a hosts file reload in progress - #430

Open
oshoukry wants to merge 2 commits into
dnsjava:masterfrom
oshoukry:429-hosts-file-reload-race
Open

oshoukry wants to merge 2 commits into
dnsjava:masterfrom
oshoukry:429-hosts-file-reload-race

Conversation

@oshoukry

@oshoukry oshoukry commented Nov 2, 2026 •

Copy link
Copy Markdown

Closes #429.

Problem

  • While a hosts file reload is in progress, hosts file lookups come back empty, since the reload process clears the map first, then loads a new set, failing those lookups even for names that are in both the old and new file. The very first read of the file has the same gap.

Fix

  • Thread safe changes to enable loading of the new file first, before performing an atomic swap so all incoming requests either use the old or new mapping, never blank.
  • Careful thought was put to
    • Avoid any new locks in this critically performant code path; lookups during a reload use the old mapping instead of waiting. Only lookups during the very first read wait for it to finish.
    • If the new file exceeds the allowed limit, the system swaps it in and searches it entry by entry, instead of the old behavior of returning nothing for names that weren't in the previous, smaller file.

Tests

  • The tests provided enable demonstrating the failure on the old code before the fix was put in place.

HostsFileParser reloaded a changed file by clearing its maps in place and
refilling them, while lookups read the maps without the lock. A lookup in
that window found nothing, even for an entry present in both versions of
the file. The first read had the same window: the maps were published
before the file was parsed.

Each read of the file now builds its own cache, published with a single
volatile write and never cleared, and each lookup works on one snapshot.

This also resets the "entire file parsed" flag per read, so a file that
grows beyond dnsjava.hostsfile.max_size_bytes is searched again instead of
reporting every uncached name as absent.

Closes dnsjava#429
@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.11111% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.44%. Comparing base (9e26eba) to head (396fe73).

Files with missing lines Patch % Lines
...main/java/org/xbill/DNS/hosts/HostsFileParser.java 86.11% 0 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #430      +/-   ##
============================================
+ Coverage     71.42%   71.44%   +0.01%     
  Complexity     3401     3401              
============================================
  Files           198      198              
  Lines         14031    14031              
  Branches       2245     2245              
============================================
+ Hits          10022    10024       +2     
+ Misses         3378     3376       -2     
  Partials        631      631              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…lling thread state

The test paused the first read and slept in a loop until the concurrent
lookup showed as BLOCKED. It now holds the read on a latch, checks that no
cache has been published and that the concurrent lookup has not answered,
then releases the read and expects both lookups to find the entry. A
package-private hasCache(), next to cacheSize(), exposes the published
state.
@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

@ibauersachs

Copy link
Copy Markdown
Member

Thanks, I'll look at it this week.

Meanwhile, please rewrite the description - by hand. Please don't use AI for it. The changes are sensible at first glance, but the description is just a wall of text for me.

@oshoukry

oshoukry commented Nov 2, 2026

Copy link
Copy Markdown
Author

Updated

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HostsFileParser: lookups during a hosts file reload miss existing entries

2 participants