WaitGroup Add called inside goroutine, race
I'm spawning goroutines in a loop and calling wg.Add(1) inside each goroutine before defer wg.Done(). Sometimes wg.Wait() returns early and I get partial results. I think it's because Add races with Wait. What's the correct ordering? This is blocking a release and I'd rather understand the ordering guarantee than just cargo-cult the fix. Right now I can get the results to come back, but I don't trust that it will hold under load.
4 replies