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.
This commit is contained in:
Abhijeet Chugh authored and GitHub committed 2026-11-29 08:52:49 +08:00
1 parent 4ffa6eea4b
commit 5807ee08fd
1 file changed
+7
+7
View File
@@ -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()
}
}