Skip to content

Commit 716e499

Browse files
authored
Avoid masking the cause of availability failures for the Mac DNS provider (netty#12039)
Motivation: If the MacOSDnsServerAddressStreamProvider is unavailable, we'd the root cause of it to show up. Modification: Move the initialisation of the `MacOSDnsServerAddressStreamProvider.currentMappings` field to the constructor. Object initialisers run before the constructor does, so since `MacOSDnsServerAddressStreamProvider.retrieveCurrentMappings` relies on the native code, it would throw an `UnsatisfiedLinkError` before `ensureAvailability` would get to run. This way, it was masking the root cause of the unavailability. Now, the `currentMappings` is initialised *after* we've called `ensureAvailability`, so we don't lose the root cause if we cannot use native DNS on Mac. Result: Easier to debug problems with initialising native MacOS DNS.
1 parent 13fd022 commit 716e499

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

resolver-dns-classes-macos/src/main/java/io/netty/resolver/dns/macos/MacOSDnsServerAddressStreamProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final class MacOSDnsServerAddressStreamProvider implements DnsServerAddre
4949
public int compare(DnsResolver r1, DnsResolver r2) {
5050
// Note: order is descending (from higher to lower) so entries with lower search order override
5151
// entries with higher search order.
52-
return r1.searchOrder() < r2.searchOrder() ? 1 : (r1.searchOrder() == r2.searchOrder() ? 0 : -1);
52+
return r1.searchOrder() < r2.searchOrder() ? 1 : r1.searchOrder() == r2.searchOrder() ? 0 : -1;
5353
}
5454
};
5555

@@ -118,10 +118,12 @@ public static Throwable unavailabilityCause() {
118118

119119
public MacOSDnsServerAddressStreamProvider() {
120120
ensureAvailability();
121+
currentMappings = retrieveCurrentMappings();
122+
lastRefresh = new AtomicLong(System.nanoTime());
121123
}
122124

123-
private volatile Map<String, DnsServerAddresses> currentMappings = retrieveCurrentMappings();
124-
private final AtomicLong lastRefresh = new AtomicLong(System.nanoTime());
125+
private volatile Map<String, DnsServerAddresses> currentMappings;
126+
private final AtomicLong lastRefresh;
125127

126128
private static Map<String, DnsServerAddresses> retrieveCurrentMappings() {
127129
DnsResolver[] resolvers = resolvers();

0 commit comments

Comments
 (0)