Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.0-stable] pillar/devicenetwork: fix goroutine leak #4423

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 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,28 @@ 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 func() {
close(quit)
<-wgChan
}()
select {
case <-wgChan:
var responses []DNSResponse
Expand All @@ -183,7 +196,7 @@ func ResolveWithPortsLambda(domain string,
}
return responses, errs
case ip := <-resolvedIPsChan:
close(quit)
return ip, nil
}

}
3 changes: 2 additions & 1 deletion pkg/pillar/devicenetwork/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/lf-edge/eve/pkg/pillar/devicenetwork"
"github.com/lf-edge/eve/pkg/pillar/netmonitor"
"github.com/lf-edge/eve/pkg/pillar/types"
"go.uber.org/goleak"
)

func createNetmonitorMockInterface() []netmonitor.MockInterface {
Expand Down Expand Up @@ -160,7 +161,7 @@ func TestDnsResolveTimeout(t *testing.T) {
}

func TestResolveWithPortsLambda(t *testing.T) {
t.Parallel()
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("go.opencensus.io/stats/view.(*worker).start"), goleak.IgnoreCurrent())

expectedIP := net.IP{1, 2, 3, 4}

Expand Down
1 change: 1 addition & 0 deletions pkg/pillar/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/pillar/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,8 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
Expand Down
5 changes: 5 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pkg/pillar/vendor/go.uber.org/goleak/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading