From c3178d853146ef3402bf228d27e3bc94edc1cac0 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Sat, 12 Jun 2021 12:51:56 -0300 Subject: [PATCH] fix dns memory leak --- src/main/kotlin/com/viaversion/aas/Util.kt | 9 +++++++-- .../kotlin/com/viaversion/aas/web/WebDashboardServer.kt | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/viaversion/aas/Util.kt b/src/main/kotlin/com/viaversion/aas/Util.kt index 00b4500..2611e50 100644 --- a/src/main/kotlin/com/viaversion/aas/Util.kt +++ b/src/main/kotlin/com/viaversion/aas/Util.kt @@ -30,6 +30,7 @@ import io.netty.handler.codec.dns.DefaultDnsQuestion import io.netty.handler.codec.dns.DefaultDnsRawRecord import io.netty.handler.codec.dns.DefaultDnsRecordDecoder import io.netty.handler.codec.dns.DnsRecordType +import io.netty.util.ReferenceCountUtil import org.slf4j.LoggerFactory import java.math.BigInteger import java.net.InetAddress @@ -57,10 +58,11 @@ suspend fun resolveSrv(hostAndPort: HostAndPort): HostAndPort { if (hostAndPort.port == 25565) { try { // stolen from PacketLib (MIT) https://github.com/Camotoy/PacketLib/blob/312cff5f975be54cf2d92208ae2947dbda8b9f59/src/main/java/com/github/steveice10/packetlib/tcp/TcpClientSession.java - dnsResolver + val records = dnsResolver .resolveAll(DefaultDnsQuestion("_minecraft._tcp.${hostAndPort.host}", DnsRecordType.SRV)) .suspendAwait() - .forEach { record -> + try { + records.forEach { record -> if (record is DefaultDnsRawRecord && record.type() == DnsRecordType.SRV) { val content = record.content() @@ -71,6 +73,9 @@ suspend fun resolveSrv(hostAndPort: HostAndPort): HostAndPort { return HostAndPort.fromParts(address, port) } } + } finally { + records.forEach { ReferenceCountUtil.release(it) } + } } catch (e: Exception) { viaaasLogger.debug("Couldn't resolve SRV", e) } diff --git a/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt b/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt index 32e111a..f63fdfb 100644 --- a/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt +++ b/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt @@ -20,6 +20,7 @@ import io.ktor.websocket.* import io.netty.handler.codec.dns.DefaultDnsQuestion import io.netty.handler.codec.dns.DnsPtrRecord import io.netty.handler.codec.dns.DnsRecordType +import io.netty.util.ReferenceCountUtil import kotlinx.coroutines.* import kotlinx.coroutines.future.asCompletableFuture import java.net.InetSocketAddress @@ -100,7 +101,13 @@ class WebDashboardServer { DefaultDnsQuestion(reverseLookup(it.address), DnsRecordType.PTR) ) info = ipLookup.await() - ptr = dnsQuery.suspendAwait().firstNotNullOfOrNull { it as? DnsPtrRecord }?.hostname() + ptr = dnsQuery.suspendAwait().let { + try { + it.firstNotNullOfOrNull { it as? DnsPtrRecord }?.hostname() + } finally { + it.forEach { ReferenceCountUtil.release(it) } + } + } } catch (ignored: Exception) { } }