Skip to content

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

FieldValue
IDCODE-0255
CategoryConcurrency
SeverityMEDIUM
CWECWE-667
ConfidenceMEDIUM
ImpactMEDIUM
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagsgo, concurrency, synchronization
OWASPN/A

References