Skip to content

Commit

Permalink
pillar/devicenetwork: fix goroutine leak
Browse files Browse the repository at this point in the history
introduced in 8573372

without this fix, the goroutine is blocked by trying to send
into a channel that has no receiver

Signed-off-by: Christoph Ostarek <christoph@zededa.com>
(cherry picked from commit d93cf8b)
  • Loading branch information
christoph-zededa authored and OhmSpectator committed Nov 6, 2024
1 parent cf17a57 commit 5c0c521
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pkg/pillar/devicenetwork/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -183,7 +193,7 @@ func ResolveWithPortsLambda(domain string,
}
return responses, errs
case ip := <-resolvedIPsChan:
close(quit)
return ip, nil
}

}

0 comments on commit 5c0c521

Please sign in to comment.