Calling Wait() inside a loop blocks Done()
Description
Calls to `sync.WaitGroup.Wait` inside a loop can block the call to `sync.WaitGroup.Done`, leading to improper locking and potential deadlocks.
Examples
Insecure Code
go
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
}()
wg.Wait()
}Secure Code
go
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
}()
}
wg.Wait()Remediation
Move the call to `sync.WaitGroup.Wait` outside the loop to ensure proper synchronization.
Rule Details
| Field | Value |
|---|---|
| ID | CODE-0255 |
| Category | Concurrency |
| Severity | MEDIUM |
| CWE | CWE-667 |
| Confidence | MEDIUM |
| Impact | MEDIUM |
| Likelihood | MEDIUM |
| Exploitability | MODERATE |
| Tags | go, concurrency, synchronization |
| OWASP | N/A |