Home >Backend Development >Golang >How to Avoid 'fatal error: all goroutines are asleep - deadlock!' with sync.WaitGroup?
Understanding "fatal error: all goroutines are asleep - deadlock!" with sync.WaitGroup
When attempting to utilize the sync.WaitGroup package for managing concurrent goroutines, it is crucial to avoid the dreaded "fatal error: all goroutines are asleep - deadlock!". This error arises from an incorrect usage of the WaitGroup, specifically when passing the value by copy rather than by reference.
The Cause of Deadlock
The issue stems from the fact that when you pass the actual WaitGroup object to the doWork function, Go makes a copy of the value. This means that the original WaitGroup will have incremented the counter by ten Add() calls without any corresponding Done() calls. In contrast, each copy of the WaitGroup passed to the goroutines will have only one Done() call and the number of Add() calls that were present when the WaitGroup was copied.
The Solution: Passing a Pointer to WaitGroup
To resolve this deadlock, it is essential to pass a pointer to the WaitGroup rather than the WaitGroup object itself. This ensures that all goroutines reference the same WaitGroup, and when they call Done(), the decrementing counter is applied to the correct WaitGroup.
Modified Code
The following modified code demonstrates the correct usage of a pointer to WaitGroup:
import "sync" func doWork(wg *sync.WaitGroup) error { defer wg.Done() // Do some heavy lifting... request URL's or similar return nil } func main() { wg := &sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go doWork(wg) } wg.Wait() }
By adopting this approach, you can confidently manage goroutines with WaitGroup, avoiding the pitfalls of deadlocks and ensuring efficient concurrent execution.
The above is the detailed content of How to Avoid 'fatal error: all goroutines are asleep - deadlock!' with sync.WaitGroup?. For more information, please follow other related articles on the PHP Chinese website!