/errors/go/all-goroutines-are-asleep-deadlock
Go fatal error: all goroutines are asleep - deadlock!
The commands on this page have not been executed by our verification runner yet.updated
- tool
- Go
- exit code
- 2
- versions
- Go 1.x
- os
- any
fatal error: all goroutines are asleep - deadlock! means every goroutine in the program is blocked, waiting on a channel operation, a WaitGroup or a lock that nothing can ever release, so the Go runtime stops the program. The classic case is sending on an unbuffered channel from main with no goroutine receiving. Start the other side in a goroutine, use a buffered channel, or close the channel when sending is done.
Example
package main
import "fmt"
func main() {
ch := make(chan int) // unbuffered: a send waits for a receiver
ch <- 1 // no other goroutine exists to receive
fmt.Println(<-ch)
}The program prints the message followed by a dump of the blocked goroutines and exits with status 2:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()Fixes
// 1. Receive in another goroutine (or send from one)
ch := make(chan int)
go func() { ch <- 1 }()
fmt.Println(<-ch)
// 2. Buffered channel: the send does not wait while there is room
buf := make(chan int, 1)
buf <- 1
fmt.Println(<-buf)
// 3. Close the channel so range stops
jobs := make(chan int)
go func() {
defer close(jobs)
for i := 0; i < 3; i++ {
jobs <- i
}
}()
for j := range jobs {
fmt.Println(j)
}Reading the goroutine states
| State in the dump | Blocked on |
|---|---|
| [chan send] | A send with no receiver (or a full buffered channel) |
| [chan receive] | A receive with no sender and no close |
| [select (no cases)] | An empty select {} with nothing else running |
| [semacquire] / [sync.WaitGroup.Wait] | WaitGroup.Wait or Mutex.Lock that is never released |
Sources
Related pages
errors · goGo: declared and not used: xGo refuses to compile a function that declares a local variable it never reads. Use the variable, delete it, or assign it to the blank identifier _ to debug.errors · goGo error: "os" imported and not usedGo refuses to compile a file that imports a package it never uses. Delete the import, let goimports or gopls manage imports, or use _ for side-effect imports.commands · goGOOS=linux GOARCH=amd64 go build: cross-compile Go binariesSet GOOS and GOARCH to build Go binaries for another OS or CPU, with no extra toolchain. Targets, PowerShell syntax, CGO_ENABLED=0 and smaller static binaries.