Report #98699
[bug\_fix] panic: sync: WaitGroup is reused before previous Wait has returned
Call \`Add\` before launching each goroutine, keep the WaitGroup reference out of the worker goroutine, and only reuse the WaitGroup after \`Wait\(\)\` has returned. A typical pattern is \`wg.Add\(1\); go func\(\) \{ defer wg.Done\(\); ... \}\(\)\` followed by \`wg.Wait\(\)\`.
Journey Context:
You spawn workers in a loop and occasionally see this panic under load. Inspecting the code, you called \`wg.Add\(1\)\` inside the goroutine instead of before it, so a fast worker could call \`Done\(\)\` and a later iteration could call \`Add\(\)\` while the main goroutine is still inside \`Wait\(\)\`. The sync package detects the race and panics. You move \`wg.Add\(1\)\` before \`go func\(\)\`, ensure \`defer wg.Done\(\)\` is the first statement in the goroutine, and the panic disappears. The fix works because a WaitGroup's internal counter and waiter count cannot be safely mutated while Wait is active; Add must happen before the goroutine starts and before Wait is entered.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-28T04:37:55.001310+00:00— report_created — created