mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #111444 from wojtek-t/prevent_leaking_goroutines
Prevent from future leaks of goroutines in integration tests
This commit is contained in:
commit
ce336550f1
@ -32,6 +32,7 @@ import (
|
|||||||
"google.golang.org/grpc/grpclog"
|
"google.golang.org/grpc/grpclog"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/kubernetes/pkg/util/env"
|
"k8s.io/kubernetes/pkg/util/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -192,22 +193,25 @@ func EtcdMain(tests func() int) {
|
|||||||
result := tests()
|
result := tests()
|
||||||
stop() // Don't defer this. See os.Exit documentation.
|
stop() // Don't defer this. See os.Exit documentation.
|
||||||
|
|
||||||
// Log the number of goroutines leaked.
|
checkNumberOfGoroutines := func() (bool, error) {
|
||||||
// TODO: we should work on reducing this as much as possible.
|
|
||||||
var dg int
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
// Leave some room for goroutines we can not get rid of
|
// Leave some room for goroutines we can not get rid of
|
||||||
// like k8s.io/klog/v2.(*loggingT).flushDaemon()
|
// like k8s.io/klog/v2.(*loggingT).flushDaemon()
|
||||||
// TODO: adjust this number based on a more exhaustive analysis.
|
// TODO(#108483): Reduce this number once we address the
|
||||||
if dg = runtime.NumGoroutine() - before; dg <= 4 {
|
// couple remaining issues.
|
||||||
os.Exit(result)
|
if dg := runtime.NumGoroutine() - before; dg <= 10 {
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
// Allow goroutines to schedule and die off.
|
// Allow goroutines to schedule and die off.
|
||||||
runtime.Gosched()
|
runtime.Gosched()
|
||||||
time.Sleep(100 * time.Millisecond)
|
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()
|
||||||
|
klog.Fatalf("unexpected number of goroutines: before: %d after %d", before, after)
|
||||||
}
|
}
|
||||||
after := runtime.NumGoroutine()
|
|
||||||
klog.Infof("unexpected number of goroutines: before: %d after %d", before, after)
|
|
||||||
os.Exit(result)
|
os.Exit(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user