Fix ChannelDeadlock: fatal error: all goroutines are asleep - deadlock! in Go
This fatal error occurs when all goroutines in the program are blocked waiting on each other, creating a circular dependency that can never be resolved. The Go runtime detects this and crashes the program. Fix it by using buffered channels, adding timeouts with select statements, or restructuring the communication pattern to avoid circular waits.
Reading the Stack Trace
Here's what each line means:
- goroutine 1 [chan send]:: The main goroutine is blocked trying to send to a channel, but no other goroutine is available to receive.
- main.main() /app/main.go:12 +0x94: The deadlock originates at line 12 of main.go where a channel send blocks because the receiver is also blocked.
- goroutine 6 [chan receive]: main.worker(0x14000196040) /app/worker.go:8 +0x48: The worker goroutine is blocked on a channel receive, creating a circular wait with the main goroutine.
Common Causes
1. Send and receive on unbuffered channel in same goroutine
An unbuffered channel requires a sender and receiver in separate goroutines. Doing both in one goroutine deadlocks.
func main() {
ch := make(chan int)
ch <- 42 // blocks forever — no receiver
fmt.Println(<-ch)
}
2. Circular channel dependency
Two goroutines wait for each other to send, creating a circular dependency.
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
go func() {
v := <-ch1 // waits for ch1
ch2 <- v * 2 // then sends to ch2
}()
v := <-ch2 // waits for ch2 — but ch2 waits for ch1
ch1 <- v // never reached
}
3. WaitGroup Add/Done mismatch
wg.Wait() blocks forever because wg.Done() is called fewer times than wg.Add().
var wg sync.WaitGroup
wg.Add(3)
for i := 0; i < 2; i++ { // only 2, not 3
go func() {
defer wg.Done()
doWork()
}()
}
wg.Wait() // blocks forever — one Done missing
The Fix
Use a buffered channel when send and receive happen in the same goroutine, or move the send to a separate goroutine. For more complex patterns, use select with a default case or timeout to prevent indefinite blocking.
func main() {
ch := make(chan int)
ch <- 42
fmt.Println(<-ch)
}
func main() {
ch := make(chan int, 1) // buffered channel
ch <- 42
fmt.Println(<-ch)
}
// Or use a separate goroutine for the sender:
func mainV2() {
ch := make(chan int)
go func() {
ch <- 42
}()
fmt.Println(<-ch)
}
Testing the Fix
package main_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestChannel_NoDeadlock(t *testing.T) {
done := make(chan bool, 1)
go func() {
ch := make(chan int, 1)
ch <- 42
result := <-ch
assert.Equal(t, 42, result)
done <- true
}()
select {
case <-done:
// success
case <-time.After(time.Second):
t.Fatal("deadlock detected — test timed out")
}
}
func TestChannel_SeparateGoroutines(t *testing.T) {
ch := make(chan int)
go func() { ch <- 42 }()
select {
case v := <-ch:
assert.Equal(t, 42, v)
case <-time.After(time.Second):
t.Fatal("timed out waiting for value")
}
}
Run your tests:
go test ./... -v -timeout 10s
Pushing Through CI/CD
git checkout -b fix/go-channel-deadlock,git add main.go worker.go,git commit -m "fix: use buffered channel to prevent deadlock on same-goroutine send/receive",git push origin fix/go-channel-deadlock
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- run: go mod download
- run: go vet ./...
- run: go test ./... -race -coverprofile=coverage.out -timeout 30s
- run: go build ./...
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
go get github.com/bugstack/sdk
Step 2: Initialize
import "github.com/bugstack/sdk"
func init() {
bugstack.Init(os.Getenv("BUGSTACK_API_KEY"))
}
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run go test ./... -timeout 10s locally to confirm no deadlocks.
- Open a pull request with the channel fix.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify in staging.
Frequently Asked Questions
BugStack runs tests with strict timeouts, analyzes channel communication patterns for circular dependencies, and validates that no goroutine blocks indefinitely before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Deadlock detection is a runtime problem. The Go runtime only detects when ALL goroutines are blocked. Partial deadlocks (some goroutines still running) are not detected.
Use unbuffered for synchronization (guaranteeing the receiver gets the value). Use buffered when you want fire-and-forget or to decouple producer and consumer speeds.