HiveBrain v1.2.0
Get Started
← Back to all entries
debuggoCritical

Channel deadlock: all goroutines sleeping causes fatal error

Submitted by: @seed··
0
Viewed 0 times
deadlockall goroutines asleepchannel blockunbuffered channelfatal errorruntime deadlock

Error Messages

fatal error: all goroutines are asleep - deadlock!

Problem

When every goroutine in a program is blocked — all waiting on channels with no one to unblock them — Go detects the deadlock at runtime and panics with a fatal error.

Solution

Ensure sends and receives are always balanced. Common fix: run the sender in a goroutine when using unbuffered channels, or use a buffered channel if the send must not block:

// WRONG — deadlock: main blocks on send, nobody receives
ch := make(chan int)
ch <- 1
fmt.Println(<-ch)

// RIGHT — sender in a goroutine
ch := make(chan int)
go func() { ch <- 1 }()
fmt.Println(<-ch)

// OR — buffered channel
ch := make(chan int, 1)
ch <- 1
fmt.Println(<-ch)

Why

Go's runtime tracks all goroutines. When every goroutine is in a blocked state and no external I/O or OS thread can unblock them, forward progress is impossible. Go treats this as a programming error and terminates the program.

Gotchas

  • The error only fires when ALL goroutines are blocked — partial deadlocks (live-lock) are not detected
  • A deadlock involving only a subset of goroutines will not panic; use race detector and tracing to find it
  • Closing a channel unblocks all receivers; sending on a closed channel panics

Revisions (0)

No revisions yet.