Fix concurrency issue in DnsNameResolver when DefaultDnsCache is used.

Motivation:

We need to ensure we only call List.* methods in the synchronized block as the returned List may not be thread-safe.

Modifications:

Do not call isEmpty() outside of the synchronized block.

Result:

Fixes [#7583]
This commit is contained in:
Norman Maurer 2018-01-15 14:17:52 +01:00
parent 819b870b8a
commit f3c6da32d7

View File

@ -600,7 +600,7 @@ public class DnsNameResolver extends InetNameResolver {
Promise<InetAddress> promise,
DnsCache resolveCache) {
final List<? extends DnsCacheEntry> cachedEntries = resolveCache.get(hostname, additionals);
if (cachedEntries == null || cachedEntries.isEmpty()) {
if (cachedEntries == null) {
return false;
}
@ -608,7 +608,9 @@ public class DnsNameResolver extends InetNameResolver {
Throwable cause = null;
synchronized (cachedEntries) {
final int numEntries = cachedEntries.size();
assert numEntries > 0;
if (numEntries == 0) {
return false;
}
if (cachedEntries.get(0).cause() != null) {
cause = cachedEntries.get(0).cause();
@ -730,7 +732,7 @@ public class DnsNameResolver extends InetNameResolver {
Promise<List<InetAddress>> promise,
DnsCache resolveCache) {
final List<? extends DnsCacheEntry> cachedEntries = resolveCache.get(hostname, additionals);
if (cachedEntries == null || cachedEntries.isEmpty()) {
if (cachedEntries == null) {
return false;
}
@ -738,7 +740,9 @@ public class DnsNameResolver extends InetNameResolver {
Throwable cause = null;
synchronized (cachedEntries) {
final int numEntries = cachedEntries.size();
assert numEntries > 0;
if (numEntries == 0) {
return false;
}
if (cachedEntries.get(0).cause() != null) {
cause = cachedEntries.get(0).cause();