How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Go · Go

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

fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan send]: main.main() /app/main.go:12 +0x94 goroutine 6 [chan receive]: main.worker(0x14000196040) /app/worker.go:8 +0x48 created by main.main in goroutine 1 /app/main.go:10 +0x70

Here's what each line means:

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.

Before (broken)
func main() {
	ch := make(chan int)
	ch <- 42
	fmt.Println(<-ch)
}
After (fixed)
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:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. 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:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. 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)

  1. Run go test ./... -timeout 10s locally to confirm no deadlocks.
  2. Open a pull request with the channel fix.
  3. Wait for CI checks to pass on the PR.
  4. Have a teammate review and approve the PR.
  5. 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.