Investigate deadlocks and goroutine leaks
Investigate deadlocks and goroutine leaks
A deadlock prevents progress because required events cannot occur; a leaked goroutine remains alive after its useful work should end. The runtime can diagnose some global deadlocks, but a single blocked worker may coexist with a responsive program. Inspect the blocking operation and ownership of its release signal.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "context" "runtime" "strings" "testing")
func TestLesson(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ready := make(chan struct{}) done := make(chan struct{}) go func() { defer close(done); close(ready); <-ctx.Done() }() <-ready buf := make([]byte, 64*1024) n := runtime.Stack(buf, true) if n == len(buf) { t.Fatal("stack dump truncated") } if !strings.Contains(string(buf[:n]), "goroutine ") { t.Fatal("missing goroutine stack") } cancel() <-done}runtime.Stack(buf,true) writes a snapshot of all goroutine stacks into a bounded buffer. Check truncation and inspect frames manually; stack wording and goroutine counts vary. The ready signal alone does not guarantee a precise stack state. Cancel and join the worker even though the snapshot itself is only diagnostic.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonMake a blocked send cancellable
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "context" "testing")
func TestLesson(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() out := make(chan int) ready := make(chan struct{}) done := make(chan struct{}) go func() { defer close(done) close(ready) select { case out <- 1: case <-ctx.Done(): } }() <-ready cancel() <-done}Without the cancellation case, an unbuffered send with no receiver cannot complete. Using a context makes the worker responsive when downstream stops reading. Do not fix leaks by adding arbitrary buffer capacity or merely checking runtime.NumGoroutine against a fixed baseline; unrelated goroutines can affect that number.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Build a worker waiting for either input or cancellation; cancel without sending input and assert it signals done. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Concurrency testing 04: Investigate deadlocks and goroutine leaks
Check the lesson objective before moving on.
Why can a leaking worker escape global deadlock detection?
What is the strongest completion check here?