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

test/integration: goroutine leak check #115456

Merged
merged 1 commit into from Feb 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions LICENSES/vendor/go.uber.org/goleak/LICENSE

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

1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -74,6 +74,7 @@ require (
go.opentelemetry.io/otel/sdk v1.10.0
go.opentelemetry.io/otel/trace v1.10.0
go.opentelemetry.io/proto/otlp v0.19.0
go.uber.org/goleak v1.2.1
go.uber.org/zap v1.19.0
golang.org/x/crypto v0.1.0
golang.org/x/net v0.5.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Expand Up @@ -762,7 +762,8 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
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.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiextensions-apiserver/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiserver/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/cloud-provider/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/component-base/go.mod
Expand Up @@ -62,7 +62,7 @@ require (
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/goleak v1.2.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
Expand Down
4 changes: 2 additions & 2 deletions staging/src/k8s.io/component-base/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/controller-manager/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/kube-aggregator/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/pod-security-admission/go.sum

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

2 changes: 1 addition & 1 deletion staging/src/k8s.io/sample-apiserver/go.sum

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

57 changes: 34 additions & 23 deletions test/integration/framework/etcd.go
Expand Up @@ -29,10 +29,10 @@ import (
"syscall"
"time"

"go.uber.org/goleak"
"google.golang.org/grpc/grpclog"
"k8s.io/klog/v2"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/env"
)

Expand Down Expand Up @@ -182,37 +182,48 @@ func EtcdMain(tests func() int) {
// Bail out early when -help was given as parameter.
flag.Parse()

before := runtime.NumGoroutine()
// Must be called *before* creating new goroutines.
goleakOpts := IgnoreBackgroundGoroutines()

goleakOpts = append(goleakOpts,
// lumberjack leaks a goroutine:
// https://github.com/natefinch/lumberjack/issues/56 This affects tests
// using --audit-log-path (like
// ./test/integration/apiserver/admissionwebhook/reinvocation_test.go).
// In normal production that should be harmless. We don't know here
// whether the test is using that, so we have to suppress reporting
// this leak for all tests.
//
// Both names occurred in practice.
goleak.IgnoreTopFunction("k8s.io/kubernetes/vendor/gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"),
goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"),
)

stop, err := startEtcd()
if err != nil {
klog.Fatalf("cannot run integration tests: unable to start etcd: %v", err)
}
result := tests()
stop() // Don't defer this. See os.Exit documentation.

checkNumberOfGoroutines := func() (bool, error) {
// We leave some room for leaked goroutines as there are
// still some leaks, mostly:
// - leak from lumberjack package we're vendoring
// - leak from apiserve healthz
// - leak from opencensus library
// Once fixed, we should be able to bring it down to zero.
if dg := runtime.NumGoroutine() - before; dg <= 3 {
return true, nil
klog.StopFlushDaemon()

// Several tests don't wait for goroutines to stop. goleak.Find retries
// internally, but not long enough. 5 seconds seemed to be enough for
// most tests, even when testing in the CI.
timeout := 5 * time.Second
start := time.Now()
for {
err := goleak.Find(goleakOpts...)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop can get removed if uber-go/goleak#94 gets merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like that PR got merged, can we drop this loop now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, looks like that didn't make it into the 1.2.1 tag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep an eye on that and update once it's in a release.

if err == nil {
break
}
if time.Now().Sub(start) >= timeout {
klog.ErrorS(err, "EtcdMain goroutine check")
result = 1
break
}
// Allow goroutines to schedule and die off.
runtime.Gosched()
return false, nil
}

// It generally takes visibly less than 1s to finish all goroutines.
// But we keep the limit higher to account for cpu-starved environments.
if err := wait.Poll(100*time.Millisecond, 5*time.Second, checkNumberOfGoroutines); err != nil {
after := runtime.NumGoroutine()
stacktraces := make([]byte, 1<<20)
runtime.Stack(stacktraces, true)
klog.Fatalf("unexpected number of goroutines: before: %d after %d\n%sd", before, after, string(stacktraces))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that replacing this code also fixes a slight bug: the string that gets written here is 1<<20 bytes long.

The code should have used:

len := runtime.Stack(stacktraces, true)
... string(stacktraces[:len]) ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are awesome 👏

}
os.Exit(result)
}

Expand Down
36 changes: 36 additions & 0 deletions test/integration/framework/goleak.go
@@ -0,0 +1,36 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package framework

import (
"go.uber.org/goleak"
"k8s.io/apiserver/pkg/server/healthz"
)

// IgnoreBackgroundGoroutines returns options for goleak.Find
// which ignore goroutines created by "go test" and init functions,
// like the one from go.opencensus.io/stats/view/worker.go.
//
// Goroutines that normally get created later when using the apiserver
// get created already when calling this function, therefore they
// also get ignored.
func IgnoreBackgroundGoroutines() []goleak.Option {
// Ensure that on-demand goroutines are running.
_ = healthz.LogHealthz.Check(nil)

return []goleak.Option{goleak.IgnoreCurrent()}
}
5 changes: 5 additions & 0 deletions vendor/go.uber.org/goleak/.gitignore

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

59 changes: 59 additions & 0 deletions 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 vendor/go.uber.org/goleak/LICENSE

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

41 changes: 41 additions & 0 deletions vendor/go.uber.org/goleak/Makefile

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