Setting Up the WaitGroup
The first step is to declare aWaitGroup in the main function. The WaitGroup helps synchronize the completion of multiple goroutines:
WaitGroup counter is increased by two with wg.Add(2). Then, a goroutine is started by calling the leak function and passing a pointer to the WaitGroup. The main function then calls wg.Wait(), which blocks until all goroutines have signaled completion.
Creating the Leak Function
Within theleak function, a channel for integer values is created. This channel is local to the function, and its scope is limited to within the leak function:
ch, prints it using fmt.Println, and then calls s.Done() on the WaitGroup. Meanwhile, the main leak function prints a message and also calls s.Done():
Analyzing the Goroutine Leak
When you run this program, the output is similar to the following:leak function is permanently blocked waiting for a value from the channel ch. Since no value is ever sent into ch, the goroutine remains stuck, leading to a deadlock.
Be cautious when designing goroutines that wait on channels. Always ensure that the expected value is sent, or include a timeout or cancellation mechanism to prevent indefinite blocking.
Key Takeaways
- Always ensure that every goroutine you launch has an exit strategy.
- Goroutines that wait on channels must receive the necessary data, or else they will block indefinitely.
- Use synchronization tools like
WaitGroupresponsibly to safely manage goroutine lifecycles.
If you plan to use channels for synchronization, consider implementing graceful shutdowns or incorporating timeouts to avoid goroutine leaks.