From 2cb3439efd59271213b3f6162fc7eee5a9a5e156 Mon Sep 17 00:00:00 2001 From: kalarina Date: Sun, 25 Oct 2026 12:25:11 +0200 Subject: [PATCH] Fail lookups that waited too long in the worker queue With a slow server all 16 workers can be busy for up to 8 s each, and queries kept piling up long after the asking app had retried or given up, delaying every newer one. --- .../cc/skysparkle/sparkledns/DohVpnService.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt b/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt index a1fe734..0a808d6 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt @@ -36,6 +36,9 @@ private const val CHANNEL_ID = "vpn_status" private const val NOTIFICATION_ID = 1 private const val MAX_BAD_PACKETS = 100 +// Android's resolver retries after 5 s, so an older lookup no longer has anyone waiting for it +private const val QUEUE_TIMEOUT_MS = 5000L + // Closing TLS connections writes to the network, so sessions release their DoH client here // rather than on the main thread, where the service stops them private val closer = Executors.newSingleThreadExecutor { r -> Thread(r, "doh-close").apply { isDaemon = true } } @@ -283,12 +286,21 @@ private class Session( QueryLog.add(question, QueryStatus.CACHED, 0) return } - pool.execute { handle(query, question) } + val queued = SystemClock.elapsedRealtime() + pool.execute { handle(query, question, queued) } } - private fun handle(query: UdpPacket, question: DnsMessage.Question?) { + private fun handle(query: UdpPacket, question: DnsMessage.Question?, queued: Long) { try { val start = SystemClock.elapsedRealtime() + // On a slow network lookups pile up behind the workers. Answer the stale ones with + // SERVFAIL at once instead of spending a worker on them, so the queue drains. + if (start - queued > QUEUE_TIMEOUT_MS) { + if (!alive.get()) return + QueryLog.add(question, QueryStatus.FAILED, (start - queued).toInt()) + DnsMessage.servfail(query.payload)?.let { write(query, it) } + return + } var status = QueryStatus.OK val answer = try { doh.query(query.payload).also { cache.put(query.payload, it) }