From 778ce20f6ed6232ce81bbc0598db25054f738242 Mon Sep 17 00:00:00 2001 From: kalarina Date: Sun, 25 Oct 2026 12:19:21 +0200 Subject: [PATCH] Close DoH connections off the main thread when a session stops --- .../java/cc/skysparkle/sparkledns/DohVpnService.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt b/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt index cc6bde6..a1fe734 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/DohVpnService.kt @@ -36,6 +36,10 @@ private const val CHANNEL_ID = "vpn_status" private const val NOTIFICATION_ID = 1 private const val MAX_BAD_PACKETS = 100 +// 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 } } + // Local VPN that routes only the fake DNS address into the tunnel; all other traffic is untouched class DohVpnService : VpnService() { @@ -205,8 +209,14 @@ private class Session( fun stop() { if (!alive.getAndSet(false)) return pool.shutdownNow() - doh.shutdown() runCatching { tun.close() } + closer.execute { + try { + doh.shutdown() + } catch (e: Exception) { + Log.w(TAG, "DoH client shutdown failed", e) + } + } } private fun readLoop() {