diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java index f1f5a1eb82..ade799b75d 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java @@ -726,14 +726,17 @@ public class DnsNameResolver extends SimpleNameResolver { } void cache(final DnsQuestion question, DnsCacheEntry entry, long delaySeconds) { - queryCache.put(question, entry); + DnsCacheEntry oldEntry = queryCache.put(question, entry); + if (oldEntry != null) { + oldEntry.release(); + } + boolean scheduled = false; try { entry.expirationFuture = ch.eventLoop().schedule(new OneTimeTask() { @Override public void run() { - Object response = queryCache.remove(question); - ReferenceCountUtil.safeRelease(response); + clearCache(question); } }, delaySeconds, TimeUnit.SECONDS); @@ -742,7 +745,7 @@ public class DnsNameResolver extends SimpleNameResolver { if (!scheduled) { // If failed to schedule the expiration task, // remove the entry from the cache so that it does not leak. - queryCache.remove(question); + clearCache(question); entry.release(); } } @@ -789,12 +792,16 @@ public class DnsNameResolver extends SimpleNameResolver { if (res.header().responseCode() == DnsResponseCode.NOERROR) { cache(q, res); promises.set(queryId, null); - qCtx.promise().trySuccess(res.retain()); + + Promise qPromise = qCtx.promise(); + if (qPromise.setUncancellable()) { + qPromise.setSuccess(res.retain()); + } } else { qCtx.retry(res.sender(), - "response code: " + res.header().responseCode() + - " with " + res.answers().size() + " answer(s) and " + - res.authorityResources().size() + " authority resource(s)"); + "response code: " + res.header().responseCode() + + " with " + res.answers().size() + " answer(s) and " + + res.authorityResources().size() + " authority resource(s)"); } } finally { ReferenceCountUtil.safeRelease(msg); diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java index eb6503743f..65d5418aa7 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java @@ -30,8 +30,8 @@ import io.netty.util.internal.StringUtil; import io.netty.util.internal.ThreadLocalRandom; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; +import org.junit.After; import org.junit.AfterClass; -import org.junit.Before; import org.junit.Test; import java.net.Inet4Address; @@ -253,8 +253,8 @@ public class DnsNameResolverTest { group.shutdownGracefully(); } - @Before - public void reset() { + @After + public void reset() throws Exception { resolver.clearCache(); }