Deadlocks in Go: the dark side of concurrency (2021)
a year ago
- #Concurrency
- #Deadlocks
- #Go
- Deadlocks in Go occur when parts of a program stop working because goroutines are waiting on each other to release locks.
- Go's deadlock detector is not always useful in real-world programs because it requires all goroutines to be asleep, which rarely happens.
- Debugging deadlocks involves collecting backtraces using tools like SIGQUIT on Unix or net/http/pprof on Windows.
- Deadlocks can occur with both Mutexes and channels, though Mutexes are more commonly involved.
- Common causes of deadlocks include recursive locking, overlapping locks, and sending/receiving on the same channel in the same goroutine.
- Strategies to avoid deadlocks include shortening lock windows, using read-only variables, and imposing total lock ordering.
- Tools like panic-parse and go-deadlock can help identify and debug deadlocks, though they have limitations.
- Single-threaded engines can eliminate deadlocks but may reduce performance.
- Comments and code reviews are essential for maintaining lock ordering invariants in complex systems.