WaitGroup Deadlock: Understanding "All Goroutines Are Asleep - Deadlock!"
When attempting to coordinate goroutines using a WaitGroup, it's possible to encounter the error "fatal error: all goroutines are asleep - deadlock!". This occurs when the goroutines responsible for decrementing the WaitGroup's counter inadvertently end up sleeping.
The Problem:
The following Go code illustrates how this error can arise:
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func() { defer wg.Done() fmt.Println("Hello", i) }() } wg.Wait() }
Upon execution, this code should print "Hello" for each goroutine. However, instead, the program exits with the dreaded "deadlock" error.
The Reason:
The issue lies in the way the WaitGroup is passed to the goroutines. Go makes a copy of the value when it's passed to the anonymous function. So, each goroutine operates on a separate copy of the WaitGroup, none of which have their counter decremented, resulting in a perpetual sleep of the goroutines.
The Solution:
To resolve the deadlock, a pointer to the WaitGroup must be passed instead of the WaitGroup itself. Thus, all goroutines will reference the same WaitGroup, enabling them to decrement its counter correctly:
package main import ( "fmt" "sync" ) func main() { wg := &sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go func() { defer wg.Done() fmt.Println("Hello", i) }() } wg.Wait() }
By passing a pointer, the goroutines share the same WaitGroup object and can successfully coordinate their execution. This version of the code will correctly print "Hello" for each goroutine without triggering the deadlock.
The above is the detailed content of Why Does My Go WaitGroup Cause a 'All Goroutines Are Asleep - Deadlock!' Error?. For more information, please follow other related articles on the PHP Chinese website!