From 5c0c52147cd68935a5e21d1ef6b22f2e9e3b3c9e Mon Sep 17 00:00:00 2001 From: Christoph Ostarek Date: Thu, 31 Oct 2024 12:49:54 +0100 Subject: [PATCH] pillar/devicenetwork: fix goroutine leak introduced in 85733729e0606f1a0637b1853f34f1fb5c1a3168 without this fix, the goroutine is blocked by trying to send into a channel that has no receiver Signed-off-by: Christoph Ostarek (cherry picked from commit d93cf8b87a15522081282c5561699c03a2bedd7d) --- pkg/pillar/devicenetwork/dns.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/pkg/pillar/devicenetwork/dns.go b/pkg/pillar/devicenetwork/dns.go index 7f31eb11e8..3a88de9d3e 100644 --- a/pkg/pillar/devicenetwork/dns.go +++ b/pkg/pillar/devicenetwork/dns.go @@ -104,7 +104,11 @@ func ResolveWithPortsLambda(domain string, quit := make(chan struct{}) work := make(chan struct{}, DNSMaxParallelRequests) - resolvedIPsChan := make(chan []DNSResponse) + defer close(work) + + resolvedIPsChan := make(chan []DNSResponse, 1) + defer close(resolvedIPsChan) + countDNSRequests := 0 var errs []error var errsMutex sync.Mutex @@ -131,10 +135,13 @@ func ResolveWithPortsLambda(domain string, copy(srcIPCopy, srcIP) countDNSRequests++ go func(dnsIP, srcIP net.IP) { - select { - case work <- struct{}{}: - // if writable, means less than dnsMaxParallelRequests goroutines are currently running - } + defer func() { + wg.Done() + <-work + }() + // if writable, means less than dnsMaxParallelRequests goroutines are currently running + work <- struct{}{} + select { case <-quit: // will return in case the quit chan has been closed, @@ -149,22 +156,25 @@ func ResolveWithPortsLambda(domain string, defer errsMutex.Unlock() errs = append(errs, err) } - if response != nil { - resolvedIPsChan <- response + if response != nil && len(response) > 0 { + select { + case resolvedIPsChan <- response: + default: + } } - <-work - wg.Done() }(dnsIPCopy, srcIPCopy) } } } wgChan := make(chan struct{}) + go func() { wg.Wait() close(wgChan) }() + defer close(quit) select { case <-wgChan: var responses []DNSResponse @@ -183,7 +193,7 @@ func ResolveWithPortsLambda(domain string, } return responses, errs case ip := <-resolvedIPsChan: - close(quit) return ip, nil } + }