diff --git a/dns/client.go b/dns/client.go index 939ca48c..2982d11c 100644 --- a/dns/client.go +++ b/dns/client.go @@ -144,7 +144,11 @@ func (c *Client) Exchange(ctx context.Context, transport adapter.DNSTransport, m if c.cache != nil { cond, loaded := c.cacheLock.LoadOrStore(question, make(chan struct{})) if loaded { - <-cond + select { + case <-cond: + case <-ctx.Done(): + return nil, ctx.Err() + } } else { defer func() { c.cacheLock.Delete(question) @@ -154,7 +158,11 @@ func (c *Client) Exchange(ctx context.Context, transport adapter.DNSTransport, m } else if c.transportCache != nil { cond, loaded := c.transportCacheLock.LoadOrStore(question, make(chan struct{})) if loaded { - <-cond + select { + case <-cond: + case <-ctx.Done(): + return nil, ctx.Err() + } } else { defer func() { c.transportCacheLock.Delete(question)