A goroutine leak occurs when a goroutine remains active after it is no longer needed, consuming resources without doing useful work. Repeatedly creating a goroutine and channel without returning or closing them can make a long-running application leaky. section 5.3.2
Prevent leaks by coordinating shutdown:
- Make the goroutine return when its work ends. section 5.3.2
- Close a channel only when it is safe; closing it while another goroutine sends causes a panic. section 5.3.2
- Use a separate notification channel to tell the sending goroutine to close its channel and exit. section 5.3.2
Coordinated shutdown
A sender can select on a done channel alongside its normal work. When the receiver signals done, the sender closes the message channel and returns instead of remaining blocked or sending after closure.
