This code runs concurrently in many goroutines,the following code is a key and relevant part extracted from the production environment code.:
func check() {
.......check condition......
//skipEnsure and skipNative will not both false here
var wg sync.WaitGroup
if !skipEnsure {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
dosomething1()
}(&wg)
}
if !skipNative {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
dosomething2()
}(&wg)
}
wg.Wait() //panic here
}
func worker() {
......
go check()
}
func main() {
for i:=0;i<32;i++ {
go worker()
}
ch := make(chan any, 0)
<-ch
}
I can reproduce the issue when WaitGroup is a global variable, but not in the code provided. It can cause panic after running for a while in the production environment.