Fix Go "fatal error: all goroutines are asleep - deadlock!" — Unbuffered Channels, Nil Channels, select, and Unclosed Ranges
This error is the runtime's verdict, not the compiler's: the program built, started, and then reached a state where every goroutine is blocked and no event remains that could unblock any of them, so there is nothing left to run and the runtime ends the program instead. Each section below takes one shape that reaches that state, shows the exact output it prints, and gives the change — a partner goroutine, a buffer, a close, a ready case — that gives the blocked operation a way to proceed.
Written November 20, 2026 · Tested with Go 1.26.4 · 22 min read
- The first failure: a receive on an unbuffered channel with no sender
- Reading the message: a fatal error, a goroutine state, and the line that blocked last
- A send on an unbuffered channel with no receiver
- An empty select: blocking with no cases at all
What this guide is
fatal error: all goroutines are asleep - deadlock! is a run-time failure reported by the runtime, not a compile error and not a panic. The program builds cleanly, because nothing about any single channel operation is wrong in the source: the channel type fits, the value type fits, the syntax is ordinary. What is wrong is a fact about the whole program at the moment the last goroutine blocks: every goroutine is waiting for an event — a send, a receive, a close — that no goroutine remains awake to perform, and no timer or outside event is pending that could wake one. The runtime detects that state and ends the program, because continuing is impossible: there is no goroutine left that could ever run.
The fix is never a change to the line the trace names. That line is where the last goroutine blocked, not where the defect is. The defect is the missing counterpart: the sender that was never started, the receiver that already returned, the close nobody calls, the buffer that would have let the send proceed alone. The sections below are organized by which counterpart is missing: a partner for an unbuffered operation, a sender or receiver on the other side of the channel, a case that can proceed in a select, a channel that is not nil, and a close to end a range.
The examples here use Go 1.26.4. Every example lives in a single file named main.go inside one module, created once from an empty directory:
$ mkdir deadlock
$ cd deadlock
$ go mod init example.com/deadlock
go: creating new go.mod: module example.com/deadlock
Each program that follows replaces the contents of that main.go. The fatal-error transcripts are shown as printed by go run -trimpath main.go: the -trimpath flag only changes how file positions print in the trace — ./main.go:5 here, where a plain run prints the same line number behind the full directory path on your machine. Tested with Go 1.26.4.
The first failure: a receive on an unbuffered channel with no sender
The smallest program that earns the fatal error creates a channel and receives from it, with no other goroutine in the program:
// main.go
package main
func main() {
ch := make(chan int)
<-ch
}
$ go run -trimpath main.go
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
./main.go:5 +0x25
exit status 2
An unbuffered channel has no storage: a receive on it can proceed only when a sender is ready at the same moment, and a send can proceed only when a receiver is ready. The specification states the blocking half of that rule directly: the receive expression blocks until a value is available. In this program no sender exists anywhere — main is the only goroutine, and it is the one waiting — so the value can never become available. The runtime sees the single goroutine blocked on a channel receive with nothing else in the program that could wake it, reports the deadlock, and exits with status 2.
The fix for this shape is to give the receive a partner: run the send in another goroutine, so the two operations meet. The fixed program at the end of this guide opens with exactly that pairing. What does not work is waiting longer, retrying the receive, or checking the channel first — there is no length or readiness to check on an unbuffered channel, and no passage of time creates a sender. Either some other goroutine sends, or the channel needs a buffer so a send can complete without a simultaneous partner, or the receive should not be there at all.
Reading the message: a fatal error, a goroutine state, and the line that blocked last
Three things in the output are worth reading closely, because each one tells you where to look and where not to.
First, this is a fatal error, which places it outside the panic mechanism. It is not raised in your code, it does not unwind the stack through your deferred calls looking for a recover, and no handler in the program can intercept it — the contrast section below shows a deferred recover around the blocking receive, and the program still ends with the same fatal error and the same exit status. The runtime is reporting a property of the whole program, and it ends the program on the spot.
Second, the bracketed text after the goroutine number is the state that goroutine blocked in, and it is the most specific clue in the output: chan receive, chan send, select (no cases), and chan receive (nil chan) are different states with different causes, and each section below is keyed to one. In a larger program the dump lists every goroutine and its state; read all of them, because the deadlock is the combination — one goroutine waiting to receive and another waiting to send on a different channel is the same verdict with the cause split across the traces.
Third, the trace position is the operation that blocked last, not the operation that is missing. ./main.go:5 is the receive that had no sender. The sender is absent, so there is no line for it in the trace; finding where it should have been — a goroutine never started, a function that returned before sending, a close that was never written — is the actual debugging, and the trace does not do it for you. The offset at the end of the frame line, +0x25, is a position inside the compiled function; it changes as the code changes and carries no information the line number does not carry better.
A send on an unbuffered channel with no receiver
The mirror image blocks on the send side:
// main.go
package main
func main() {
ch := make(chan int)
ch <- 1
}
$ go run -trimpath main.go
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
./main.go:5 +0x28
exit status 2
The specification's send rule is the same shape as the receive rule: communication blocks until the send can proceed, and a send on an unbuffered channel can proceed if a receiver is ready. Here there is no receiver — the only goroutine is the sender — so the send can never proceed, and the state in the trace reads chan send instead of chan receive. Everything else about the verdict is identical, because the runtime's question is not which operation blocked but whether anything remains that could unblock it.
This shape appears in real programs when the receiving goroutine exits early — it returns on an error path, or it was never started because the code that starts it sits behind a condition — while the sender runs unconditionally. The trace points at the send, and the send is correct; the defect is on the receive side, in the path that let the receiver leave or never arrive. The fixes are the same pairing as the receive shape, seen from the other end: guarantee a receiver for every send, give the channel a buffer sized for sends that must complete without one, or make the send conditional with a select so a sender with no partner does something else instead of blocking forever.
An empty select: blocking with no cases at all
A select with no cases and no default blocks by definition:
// main.go
package main
func main() {
select {}
}
$ go run -trimpath main.go
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [select (no cases)]:
main.main()
./main.go:4 +0xf
exit status 2
The specification's rule for select is that if there is no default case, the statement blocks until at least one of the communications can proceed. An empty select has no communications at all, so none can ever proceed, and the trace says so in the state text: select (no cases). With main as the only goroutine, the runtime's verdict follows immediately.
This shape is rarely written as literally as the program above. It arrives as the select whose cases were all removed or commented out during an edit, or as a deliberate "block forever" at the end of a main that was supposed to keep a program alive for its worker goroutines — where the workers have all finished or all blocked, and the empty wait in main is the last goroutine standing when the detector fires. A select used as a wait needs at least one case that can proceed when there is work to wait for, and a program that must stay alive needs a real shutdown signal to wait on, not an empty block. Where blocking is genuinely unwanted, a default case is what makes a select non-blocking: with a default, the statement takes it immediately when no communication is ready, and the fixed program at the end of this guide ends with that form.
A receive from a nil channel: the channel that can never proceed
A channel declared with var and never made is nil, and an operation on a nil channel blocks forever:
// main.go
package main
func main() {
var ch chan int
<-ch
}
$ go run -trimpath main.go
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive (nil chan)]:
main.main()
./main.go:5 +0x17
exit status 2
The specification states both halves in one sentence each: receiving from a nil channel blocks forever, and a send on a nil channel blocks forever. A nil channel has no buffer, no sender queue, and no receiver queue — there is no channel object behind the variable at all — so no operation on it can ever proceed, and no other goroutine can change that by sending or receiving on its side, because there is no other side. The trace names this state explicitly, chan receive (nil chan), which makes it the easiest shape in this guide to identify: when the state text says nil chan, the channel variable was never assigned a make result.
In a larger program the nil usually arrives the way nil maps do: a channel field inside a zero-value struct, a channel a setup function was supposed to create on a path that did not run, or a channel variable shadowed inside a branch so the outer variable the receive uses is still nil. The fix is the allocation, at the declaration or in the constructor or setup path every execution passes through: ch := make(chan int), with a buffer size where the design calls for one. A nil channel does have one legitimate use — disabling a case in a select, because a case on a nil channel can never proceed and is therefore never chosen — and that use is deliberate and local to the select. A nil channel reaching an ordinary send or receive outside that pattern is a missing make, and the trace's (nil chan) marker is the runtime telling you so.
A range over a channel nobody closes: the values arrive, the loop never ends
A for–range over a channel ends only when the channel is closed, so a sender that sends its values and simply returns leaves the loop waiting for a close that is never coming:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
ch <- 1
ch <- 2
}()
for v := range ch {
fmt.Println(v)
}
}
$ go run -trimpath main.go
1
2
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
./main.go:11 +0xc5
exit status 2
The output shows the shape's signature: the program works, visibly, before it fails. It prints 1 and 2 — both sends met the range's receives, in order — and then the range waits for a third value or a close. The specification's range rule is that for channels, the iteration values produced are the successive values sent on the channel until the channel is closed; the loop has no other exit. By then the sending goroutine has returned, so no goroutine remains that could send or close, main is blocked in a channel receive at the range line, and the detector fires. The trace position, line 11, is the for statement itself, because a range over a channel is a receive repeated.
The fix belongs to the sender, and it is one line: close(ch) when the sends are done — conventionally defer close(ch) at the top of the sending goroutine, or a close after the final send — so the range drains any values already sent and then ends. Closing is the sender's job because the sender is the side that knows no more values are coming; a receiver closing the channel it ranges over would panic any sender that writes afterward. The fixed program at the end of this guide includes this shape repaired: the sender closes after its two sends, and the range prints both values and the program moves on.
A second receive after the worker sent exactly once: the count mismatch
A receive blocks the same way when the sender did its job exactly as written and the receiver asks for more than was ever going to be sent:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
ch <- 1
}()
fmt.Println(<-ch)
fmt.Println(<-ch)
}
$ go run -trimpath main.go
1
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
./main.go:11 +0xd7
exit status 2
The first receive succeeds — the program prints 1 — and the worker goroutine, having completed its one send, returns. The second receive, on line 11, then waits for a value no goroutine will ever send, and with main the only goroutine left, the detector fires at that line. Nothing in either operation is wrong in isolation: one send and two receives is a disagreement about the protocol, not a broken statement, and the compiler has nothing to say about how many values a channel will carry over its life.
This is the shape behind the version of the deadlock that appears only on some inputs: a loop that receives once per item deadlocks on the run where the producer makes one fewer item than the consumer expects — an empty result, an early error, a filtered element. The repairs are protocol repairs. Where the count is genuinely open-ended, the producer closes the channel when it is done and the consumer ranges, which makes "how many" the producer's fact to state rather than the consumer's to guess. Where the consumer must know whether another value is coming, the two-value receive form reports it: v, ok := <-ch yields ok as false when the channel is closed and empty, so a closed channel answers the extra receive immediately instead of blocking. What does not work is adding the missing send to silence the trace — a dummy value makes the count agree for this run and teaches the consumer to expect a value that carries no meaning.
The contrasts: a timer wakes a goroutine, and recover cannot catch a fatal error
Two contrasts close the rule off. First, blocked is not the same as deadlocked. A program whose only goroutine waits on a channel while another goroutine sleeps before sending does not trigger the detector:
// main.go
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
go func() {
time.Sleep(50 * time.Millisecond)
ch <- 1
}()
fmt.Println(<-ch)
}
$ go run main.go
1
The output is 1 and a clean exit. While main sat in the receive, the sleeping goroutine was not blocked on a channel at all — it was waiting on a timer, and a timer is an event that will fire on its own, so a goroutine existed that could still wake and act. The detector's condition is stronger than "the main goroutine is waiting": it is that every goroutine is blocked and nothing — no timer, no network event, no other goroutine — remains that could change that. This is why a program can sit in a receive for an hour without the error, and why the error, when it comes, is a statement about the program's whole state and not about how long any one operation waited.
Second, the fatal error is not a panic, and recover does not apply to it. A deferred recover around the blocking receive changes nothing:
// main.go
package main
import "fmt"
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered:", r)
}
}()
ch := make(chan int)
<-ch
fmt.Println("after receive")
}
$ go run -trimpath main.go
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
./main.go:12 +0x45
exit status 2
Neither recovered: nor after receive prints. The deferred function does not rescue the program, because there is no panic to recover from: the runtime has established that no goroutine can proceed, and it ends the program outright. The practical consequence is that this failure cannot be handled, retried, or logged around from inside the program — the only place to address it is the design of the channel protocol, which is what the fixes below are. Other waits can reach the same all-blocked state — a second Lock on a held mutex, a WaitGroup wait whose counter never reaches zero — and they earn the same fatal error by the same reasoning; the shapes above are the channel forms, which are the ones whose traces name the operation that blocked.
The fixed program
One program that applies each fix at once — a partner goroutine for the unbuffered receive, a buffered channel for a send that must complete before its receive, a close from the sender so the range ends, and a select with a default so an empty channel is a branch taken rather than a block:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
ch <- 1
}()
fmt.Println(<-ch)
buffered := make(chan int, 1)
buffered <- 2
fmt.Println(<-buffered)
numbers := make(chan int)
go func() {
numbers <- 3
numbers <- 4
close(numbers)
}()
for v := range numbers {
fmt.Println(v)
}
empty := make(chan int)
select {
case v := <-empty:
fmt.Println(v)
default:
fmt.Println("default")
}
}
$ go run main.go
1
2
3
4
default
The output is five lines, one per fix. The partner goroutine's send meets the receive and prints 1. The buffered channel accepts its send with no receiver waiting — the buffer is the storage the unbuffered send lacked — and the receive that follows prints 2. The range prints 3 and 4 and then ends, because the sender closed the channel after its last send. The final select finds no value ready on empty and takes its default case, printing default instead of blocking.
The fixes, ranked
Give every unbuffered operation a partner. Start the sending or receiving goroutine before the operation that waits for it, on every path. This is the fix for the no-sender and no-receiver shapes, and it is the strongest form because an unbuffered channel is a meeting point by design: when the protocol really is a handoff, the counterpart goroutine is the protocol, and arranging the meeting is the whole repair.
Close every channel that is ranged over, from the sender. close after the final send, or deferred in the sending goroutine. This is the fix for the unclosed-range shape and for the count-mismatch shape wherever the consumer switches to ranging. It is ranked second because it converts an implicit agreement about counts into a signal the receiver can observe: the loop ends when the producer says it is done, not when the consumer's guess runs out.
Make the channel — a nil channel is a missing allocation. make at the declaration or in the setup every path passes through. This is the fix for the nil-channel shape, and the trace identifies it for you: a state that reads (nil chan) is this defect and no other. The one deliberate exception is a nil channel used to disable a select case, which never reaches a bare send or receive.
Use a buffer when a send must not wait for a receiver. make(chan T, n) lets the first n sends proceed without a partner. This fits a producer that must drop off a known, bounded number of values and continue — and it is a sizing decision, not a deadlock cure: a program that sends more than the buffer holds, with no receiver, blocks at the send that overflows it and earns the same fatal error a few lines later.
Give a select a way out: a ready case or a default. A select that must not block carries a default; a select that waits carries at least one case on a channel some goroutine will actually use, or a timer case that bounds the wait. This is the fix for the empty-select shape and the general guard for sends and receives that have an alternative worth taking.
Do not answer the fatal error with recover, and do not add operations to silence the trace. There is no panic to recover, and a dummy send or an extra receive pasted at the trace line makes one run's counts agree while leaving the protocol disagreement in place for the next input. The defect is a missing counterpart or a missing close, and only supplying it — or restating the protocol so the counts cannot disagree — speaks to it.
Which fix for which cause
| What the trace and code show | Cause | Fix |
|---|---|---|
State chan receive, one goroutine, no sender anywhere |
An unbuffered receive with no partner | Send from another goroutine, or buffer the channel |
State chan send, receiver exited or never started |
An unbuffered send with no partner | Guarantee a receiver on every path, or buffer, or select the send |
State select (no cases) |
An empty select with no default |
Add a case that can proceed, or a default |
State chan receive (nil chan) or chan send (nil chan) |
The channel was never made | make the channel where it is declared or set up |
Values print, then the error at the for–range line |
The sender never closed the channel | close from the sender after the last send |
| One value prints, then the error at a later receive | More receives than the producer ever sends | Close and range, or receive with the two-value form |
| A long wait with no error | A timer or event can still wake a goroutine | Not a deadlock; the detector fires only when nothing can wake |
Checklist
- Read every goroutine in the dump, not only the top one: the state text of each —
chan receive,chan send,select (no cases),(nil chan)— names the operation it blocked in, and the deadlock is the combination of all of them. - For each blocked operation, find its counterpart and ask where it went: a goroutine never started, a goroutine that returned early, a send or receive behind a branch that did not run.
- Check for nil before anything subtler: a state marked
(nil chan)means the channel variable never received amakeresult, and no rearrangement of goroutines fixes that. - For every channel a
for–rangeconsumes, find theclose, and confirm it is in the sender, on a path every run passes through — including error paths that end the sends early. - Count the protocol, not the statements: where sends and receives must match one-for-one, look for the input or branch that changes one side's count, and prefer close-and-range or the two-value receive so the producer's count is observed rather than assumed.
- Check buffers against the real maximum, not the common case: a buffered channel postpones the block to the send past its capacity, so a buffer sized for the usual run deadlocks on the unusual one if no receiver is running.
Related guides on this site
- Finding a Leaked Goroutine with Go 1.27's goroutineleak Profile — the opposite population problem: goroutines that block and never leave, accumulating instead of ending the program, and the profile that finds where they are parked.
- Go errgroup Worker Pool: The Race the Compiler Won't Catch — a worker pool built on goroutines and channels where the failure is a data race rather than a standstill, and where the wait for the workers is the line to read first when either failure appears.
- Testing Cancellation with Go's synctest Bubble: 5 Seconds in 0.00 — cancellation as the deliberate way out of a blocked operation: a context that ends the wait on purpose, tested inside a bubble where the timers this guide's contrast relies on run deterministically.
- Fix Go "assignment to entry in nil map" Panic — Declared Maps, Struct Fields, and Nested Maps — the nil-value failure for maps: a declared-but-never-made map panics on its first write, where a declared-but-never-made channel blocks on its first use and earns this guide's fatal error instead.
- Go Slices and Maps — Append, Length, Capacity, and Lookup — the collection types and their zero values, the background for why a nil channel is a usable value in a
selectcase and an unusable one everywhere else.
Sources
- The Go Programming Language Specification — for the receive operator (the expression blocks until a value is available; receiving from a nil channel blocks forever), for send statements (communication blocks until the send can proceed; a send on an unbuffered channel can proceed if a receiver is ready; a send on a buffered channel can proceed if there is room in the buffer; a send on a nil channel blocks forever), for select statements (if there is no default case, the select statement blocks until at least one of the communications can proceed), and for range over a channel (for channels, the iteration values produced are the successive values sent on the channel until the channel is closed).
Keep Exploring
The full Go hub \u2014 slices and maps, concurrency, and the standard library.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.