test etcd: avoid potential data race

If the caller needs to reconfigure klog, then calling klog without proper
synchronizing while stopping causes a data race. We have to ensure that
the goroutine has terminated before stop returns.
This commit is contained in:
Patrick Ohly 2024-08-23 14:23:03 +02:00
parent 6ee2c63135
commit cc9234cffd

View File

@ -26,6 +26,7 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"testing" "testing"
"time" "time"
@ -146,7 +147,10 @@ func RunCustomEtcd(dataDir string, customFlags []string, output io.Writer) (url
// try to exit etcd gracefully // try to exit etcd gracefully
defer cancel() defer cancel()
cmd.Process.Signal(syscall.SIGTERM) cmd.Process.Signal(syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer wg.Done()
select { select {
case <-ctx.Done(): case <-ctx.Done():
klog.Infof("etcd exited gracefully, context cancelled") klog.Infof("etcd exited gracefully, context cancelled")
@ -156,6 +160,7 @@ func RunCustomEtcd(dataDir string, customFlags []string, output io.Writer) (url
} }
}() }()
err := cmd.Wait() err := cmd.Wait()
wg.Wait()
klog.Infof("etcd exit status: %v", err) klog.Infof("etcd exit status: %v", err)
err = os.RemoveAll(etcdDataDir) err = os.RemoveAll(etcdDataDir)
if err != nil { if err != nil {