Close DoH connections off the main thread when a session stops

This commit is contained in:
2026-10-25 12:19:21 +02:00
parent 7da1a34495
commit 778ce20f6e
@@ -36,6 +36,10 @@ private const val CHANNEL_ID = "vpn_status"
private const val NOTIFICATION_ID = 1 private const val NOTIFICATION_ID = 1
private const val MAX_BAD_PACKETS = 100 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 // Local VPN that routes only the fake DNS address into the tunnel; all other traffic is untouched
class DohVpnService : VpnService() { class DohVpnService : VpnService() {
@@ -205,8 +209,14 @@ private class Session(
fun stop() { fun stop() {
if (!alive.getAndSet(false)) return if (!alive.getAndSet(false)) return
pool.shutdownNow() pool.shutdownNow()
doh.shutdown()
runCatching { tun.close() } runCatching { tun.close() }
closer.execute {
try {
doh.shutdown()
} catch (e: Exception) {
Log.w(TAG, "DoH client shutdown failed", e)
}
}
} }
private fun readLoop() { private fun readLoop() {