From 5807ee08fd27a35b73eb38957953fd83ed75abcd Mon Sep 17 00:00:00 2001 From: Abhijeet Chugh Date: Sun, 29 Nov 2026 08:52:49 +0800 Subject: [PATCH] fix: forget in-flight dns lookups on ResetConnection (#3533) A lookup that starts while the old network is going away (for example Wi-Fi dropping on Android) can be sent to servers that will never answer. It runs on its own DefaultDNSTimeout inside the singleflight group, and every later request for the same name joins it, even after the platform has signalled the network change (Clash Meta for Android calls ClearCache and ResetConnection on every DNS change). So all requests for that name fail together when the stale lookup times out. Reset the singleflight group both before and after resetting the clients. Before, so requests made while a client reset is blocked (DoH's ResetConnection waits for clientMu) start a fresh lookup instead of joining one stuck on the old network. After, so requests made once ResetConnection returns do not join a lookup that started during the reset on a connection from before the change. --- dns/resolver.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dns/resolver.go b/dns/resolver.go index b8742f24..eb3f3569 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -398,6 +398,10 @@ func (r *Resolver) ClearCache() { func (r *Resolver) ResetConnection() { if r != nil { + // The network changed: lookups still in flight went out on the old network and may never be + // answered. Forget them before resetting the clients, which can block (DoH waits for clientMu), + // so new requests start a fresh lookup right away instead of joining the old ones. + r.group.Reset() for _, c := range r.main { c.ResetConnection() } @@ -407,6 +411,9 @@ func (r *Resolver) ResetConnection() { if dr := r.defaultResolver; dr != nil { dr.ResetConnection() } + // Forget them again after: a lookup that started while the clients were being reset may be using + // a connection from before the change, and requests made after ResetConnection must not join it. + r.group.Reset() } }