Skip to content

Latest commit

 

History

History
57 lines (39 loc) · 2.09 KB

concurrency.debug.md

File metadata and controls

57 lines (39 loc) · 2.09 KB

Overview

  1. Debugging goroutines
  2. Debugging concurrent & parallel go programs

Symptoms

  1. Hanging goroutines (your program should end but some goroutines are stuck)
  2. Unexpected variable values (Race conditions)

Anti-patterns

  1. littering your code with log statements
  2. Adding extra complexity
  3. Adding locks/mutexes

Solution-1: Run locally

  1. Set env var: GOTRACEBACK="all"
  2. Run the program
  3. Get your process id
  4. Wait for it to reach a point of non-responsiveness
  5. Run kill -3 <pid>
  6. See the live goroutines in the console
  7. ag/ripgrep/grep for your source directory
  8. Use the file and line numbers to see where goroutines are stuck

Solution-2: Add observability (works locally and on servers)

  1. See tracing doc

Solution-3: Run locally and Use a debugger

  1. See https://www.jetbrains.com/help/go/debugging-code.html

Solution-4: Use the race detector

  1. https://go.dev/blog/race-detector
  2. https://go.dev/doc/articles/race_detector
  3. https://yourbasic.org/golang/detect-data-races/

Solution-5: Add tests

  1. Writing test friendly code forces you to write modular code
  2. Modular code is simple to reason about
  3. Tests can help you pin-point issues
  4. see unit test doc and e2e doc

Solution-6: Follow best practices

  1. Use a static analyzer (like golangci-lint or goland's inspections or sonarqube)
  2. static analyzers can help you find and fix anti-patterns
  3. Some anti-patterns cause concurrency issues, others make it harder to solve them

Solution-7: Use the tools correctly

  1. Channels, Goroutines, select blocks and mutexes all have a few gotchas related to correct usage
  2. Use your resources to learn idiomatic usage (books, articles, youtube videos, ai tools, Goland docs, ...)