Deadlocks
Definition
Deadlock
- In plain English
- A deadlock is when goroutines wait for each other in a cycle, so none can ever continue.
- Think of it like
- It is like two cars on a narrow bridge, each waiting for the other to reverse.
Why it exists
One branch holds lock A and waits for B. Another holds B and waits for A.
Neither can finish. Go can report a global deadlock, but some stuck requests may remain hidden inside a running server.
How it works
Coffman conditions
- In plain English
- The Coffman conditions are four situations that must all hold for a deadlock: exclusive resources, holding while waiting, no forced taking, and a circular wait.
- Think of it like
- Like four ingredients that together make the bridge jam.
Lock ordering
- In plain English
- Lock ordering means every goroutine acquires multiple locks in the same agreed order.
- Think of it like
- Like a rule that everyone crosses the bridge north to south.
Goroutine 1 holds A and waits for B; Goroutine 2 holds B and waits for A.
| Condition | Meaning | Possible change |
|---|---|---|
| Exclusive resources | One holder at a time | Remove unnecessary shared ownership |
| Hold while waiting | Keep A while requesting B | Restructure to avoid overlapping holds |
| No forced taking | Cannot seize a held resource | Use cooperative release when the design permits |
| Circular wait | A waits for B, B for A | Use one global lock order |
package main
import (
"sync"
)
func main() {
var a, b sync.Mutex
var ready, finished sync.WaitGroup
ready.Add(2)
finished.Add(2)
transfer := func(first, second *sync.Mutex) {
defer finished.Done()
first.Lock()
defer first.Unlock()
ready.Done()
ready.Wait()
second.Lock()
defer second.Unlock()
}
go transfer(&a, &b)
go transfer(&b, &a)
finished.Wait()
}
package main
import (
"fmt"
"sync"
)
type Branch struct {
id int
mu sync.Mutex
}
func transfer(a, b *Branch) {
if a == b {
return
}
if a.id > b.id {
a, b = b, a
}
a.mu.Lock()
defer a.mu.Unlock()
b.mu.Lock()
defer b.mu.Unlock()
fmt.Println("transfer", a.id, b.id)
}
func main() {
a, b := &Branch{id: 1}, &Branch{id: 2}
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); transfer(a, b) }()
go func() { defer wg.Done(); transfer(b, a) }()
wg.Wait()
}
- An unbuffered send blocks until a receiver participates.
- A WaitGroup with missing Done calls can wait forever.
- A timeout stops waiting only where code checks it; Mutex.Lock does not accept cancellation.
- An unmatched channel operation can also block forever without two locks; the runtime can report that as a deadlock.
Examples
Order branch locks
Two transfers run in opposite directions. Their locks must still follow one global order.
package main
import (
"sync"
)
func main() {
var a, b sync.Mutex
var ready, finished sync.WaitGroup
ready.Add(2)
finished.Add(2)
transfer := func(first, second *sync.Mutex) {
defer finished.Done()
first.Lock()
defer first.Unlock()
ready.Done()
ready.Wait()
second.Lock()
defer second.Unlock()
}
go transfer(&a, &b)
go transfer(&b, &a)
finished.Wait()
}
package main
import (
"fmt"
"sync"
)
type Branch struct {
id int
mu sync.Mutex
}
func transfer(a, b *Branch) {
if a == b {
return
}
if a.id > b.id {
a, b = b, a
}
a.mu.Lock()
defer a.mu.Unlock()
b.mu.Lock()
defer b.mu.Unlock()
fmt.Println("transfer", a.id, b.id)
}
func main() {
a, b := &Branch{id: 1}, &Branch{id: 2}
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); transfer(a, b) }()
go func() { defer wg.Done(); transfer(b, a) }()
wg.Wait()
}
The Before program waits until both first locks are held, making the cycle reproducible. Use unique, stable branch IDs.
The insight
Consistent lock order breaks circular waiting.
Keep cabin and seat together
A booking needs both a cabin and a seat. One lock avoids a two-lock cycle.
package main
import (
"fmt"
"sync"
)
func main() {
var mu sync.Mutex
cabin, seat := 1, 1
book := func() {
mu.Lock()
defer mu.Unlock()
if cabin > 0 && seat > 0 {
cabin--
seat--
fmt.Println("booked")
}
}
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() { defer wg.Done(); book() }()
}
wg.Wait()
}
All booking changes use one mutex. This reduces possible overlap but makes the rule easier to inspect.
The insight
One broader lock can be easier than several narrow locks.
Start a matching receiver
Main sends a lobby number through an unbuffered channel. No receiver can run yet.
package main
import (
"fmt"
)
func main() {
lobby := make(chan int)
lobby <- 7
fmt.Println(<-lobby)
}
package main
import (
"fmt"
"sync"
)
func main() {
lobby := make(chan int)
var wg sync.WaitGroup
wg.Add(1)
go func() { defer wg.Done(); lobby <- 7 }()
fmt.Println(<-lobby)
wg.Wait()
}
The Before program reports a deadlock. The After program prints 7 and joins its helper.
The insight
An unbuffered send needs a participating receiver.
Trade-offs
Pros
- One lock order prevents lock cycles.
- Broader locks simplify related updates.
Cons
- Every caller must follow the order.
- Broader locks reduce overlapping work.
Use when
- An operation needs several locks.
- Shared updates must remain consistent.
Avoid when
- Separate ownership removes shared locks.
- A single goroutine can perform the whole operation.
Quick check
Takeaways
- Deadlock prevents progress through waiting dependencies.
- Acquire multiple locks in one agreed order.
- Start matching channel operations before waiting for them.