Concurrency Patterns and Problems in Go
Fix all goroutines are asleep deadlocks, build worker pools, and run failing tasks concurrently with errgroup.
Knowing goroutines, channels and mutexes is the start. Putting them together correctly is where most real bugs and most useful patterns are. This section covers the most common failure, deadlocks, and two patterns you will use constantly.
Deadlocks and Their Messages
fatal error: all goroutines are asleep - deadlock!
Five causes explain almost every case: a send with no receiver, an unclosed range, Wait before receiving, an unbalanced WaitGroup, and double locking.
Servers hang silently:
Go only reports deadlocks when every goroutine is stuck, so a stuck handler in a server just hangs.
Worker Pools
A jobs channel, a fixed number of workers and a results channel closed after the workers finish.
errgroup for Concurrent Tasks
errgroup waits for tasks, returns the first error, cancels the rest through a context and limits concurrency with SetLimit.
Summary
Close channels from the sender side, keep goroutine counts bounded, and let errgroup handle errors and cancellation for groups of tasks.
Go Deadlocks - all goroutines are asleep and How to Fix It
What fatal error all goroutines are asleep - deadlock! means, the five most common causes with runnable fixes, and how to find deadlocks that Go cannot detect.
Worker Pools in Go
Build a worker pool in Go with channels and a WaitGroup to process many jobs with a fixed number of goroutines, collect results and handle errors.
errgroup for Concurrent Tasks in Go
Run goroutines that can fail with golang.org/x/sync/errgroup, get the first error back, cancel the rest with a context, and limit concurrency with SetLimit.