diff --git a/test/integration/framework/etcd.go b/test/integration/framework/etcd.go index b6fe289f4dc..a0128352ed6 100644 --- a/test/integration/framework/etcd.go +++ b/test/integration/framework/etcd.go @@ -172,12 +172,30 @@ func EtcdMain(tests func() int) { // Bail out early when -help was given as parameter. flag.Parse() + before := runtime.NumGoroutine() 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. + + // Log the number of goroutines leaked. + // 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 + // like k8s.io/klog/v2.(*loggingT).flushDaemon() + // TODO: adjust this number based on a more exhaustive analysis. + if dg = runtime.NumGoroutine() - before; dg <= 4 { + os.Exit(result) + } + // Allow goroutines to schedule and die off. + runtime.Gosched() + time.Sleep(100 * time.Millisecond) + } + after := runtime.NumGoroutine() + klog.Infof("unexpected number of goroutines: before: %d after %d", before, after) os.Exit(result) }