SlideShare a Scribd company logo
 Debugging concurrent
programs in Go
Andrii Soldatenko
• Gopher, OSS
contributor
• father of 👧 👶
• debuggers fan
• Speaker at many
conferences
@a_soldatenko
Concurrency Programming
is Challenging!
Debugging
concurrent
programs is Hard!
@a_soldatenko
Debugging sequential
programs
dlv test -- -test.run TestFibonacciBig
(dlv) b main_test.go:6
Breakpoint 1 set at 0x115887f for github.com/andriisoldatenko/
debug_test.TestFibonacciBig() ./main_test.go:6
(dlv) c
> github.com/andriisoldatenko/debug_test.TestFibonacciBig() ./
main_test.go:6 (hits goroutine(17):1 total:1) (PC: 0x115887f)
1: package main
2:
3: import "testing"
4:
5: func TestFibonacciBig(t *testing.T) {
=> 6: var want int64 = 55
7: got := FibonacciBig(10)
8: if got.Int64() != want {
9: t.Errorf("Invalid Fibonacci value for N: %d, got: %d,
want: %d", 10, got.Int64(), want)
10: }
11: }
(dlv)
@a_soldatenko
Debugging concurrent
programs
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
gowayfest git:(master) ✗ go run main.go
hello
world
world
hello
hello
world
world
hello
world
hello
gowayfest git:(master) ✗ go run main.go
world
hello
hello
world
hello
world
world
hello
hello
world
Debugging concurrency programs in go
What is a goroutine?
https://tpaschalis.github.io/goroutines-size/
Do you know why
gorotines?
How can I debug
concurrent Go program?
Playground
GOMAXPROCS is 1
visualize goroutines🐞 ?
func main() {
c := coloredgoroutine.Colors(os.Stdout)
fmt.Fprintln(c, "Hi, I am go routine", goid.ID(), "from main routine")
count := 10
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
i := i
go func() {
fmt.Fprintln(c, "Hi, I am go routine", goid.ID(), "from
loop i =", i)
wg.Done()
}()
}
wg.Wait()
}
visualize goroutines🐞 ?
https://github.com/xiegeo/coloredgoroutine
visualize goroutines🐞 ?
https://divan.dev/posts/go_concurrency_visualize/
Print scheduling events?
$ GODEBUG=schedtrace=5000 <binary>
scheduling events
@a_soldatenko
- delve
- gdb
using debuggers:
 Advanced debugging techniques
of Go code
@a_soldatenko
How to set breakpoint
inside goroutine?package main
import (
"fmt"
)
func say(s string, r chan string) {
fmt.Println(s)
r <- s
}
func main() {
chan1 := make(chan string)
chan2 := make(chan string)
go say("world", chan1)
go say("hello", chan2)
res1 := <-chan1
res2 := <-chan2
fmt.Printf("Channel 1: %snChannel 2: %sn", res1, res2)
}
@a_soldatenko
> main.say() ./main.go:9 (hits goroutine(7):1 total:1) (PC:
0x10c46c9)
4: "fmt"
5: )
6:
7: func say(s string, r chan string) {
8: fmt.Println(s)
=> 9: r <- s
10: }
11:
12: func main() {
13: chan1 := make(chan string)
14: chan2 := make(chan string)
How to set breakpoint
inside goroutine?
@a_soldatenko
@a_soldatenko
How to debug channel?
package main
func main() {
ch := make(chan int, 4)
ch <- 1
ch <- 2
ch <- 3
ch <- 4
close(ch)
}
@a_soldatenko
@a_soldatenko
How to debug channel?(dlv) p ch
chan int {
qcount: 0,
dataqsiz: 4,
buf: *[4]int [0,0,0,0],
elemsize: 8,
closed: 0,
elemtype: *runtime._type {size: 8, ptrdata: 0, hash: 4149441018, tflag: tflagUncommon|tflagExtraStar|
tflagNamed|tflagRegularMemory (15), align: 8, fieldAlign: 8, kind: 2, equal: runtime.memequal64, gcdata: *1,
str: 663, ptrToThis: 22432},
sendx: 0,
recvx: 0,
recvq: waitq<int> {
first: *sudog<int> nil,
last: *sudog<int> nil,},
sendq: waitq<int> {
first: *sudog<int> nil,
last: *sudog<int> nil,},
lock: runtime.mutex {key: 0},}
(dlv) n
> main.main() ./main_ch.go:7 (PC: 0x105e8d9)
2:
3:
4: func main() {
5: ch := make(chan int, 4)
6: ch <- 1
=> 7: ch <- 2
8: ch <- 3
9: ch <- 4
10: close(ch)
11: }
(dlv) p ch
@a_soldatenko
dlv send to channel value
similar to set variable.
@a_soldatenko
GDB
t.me/golang_for_two
@a_soldatenko
Gdb and golang
go build -ldflags=-compressdwarf=false -
gcflags=all="-N -l" -o main main.go
@a_soldatenko
Gdb and goroutines
@a_soldatenko
Gdb and goroutines
@a_soldatenko
Deadlocks happen and are
painful to debug.
@a_soldatenko
How to detect deadlocks
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
/Users/andrii/workspace/src/github.com/andriisoldatenko/
gowayfest/main_deadlock.go:5 +0x50
exit status 2
@a_soldatenko
Real world examples
complicated scenario & tools
https://github.com/sasha-s/go-deadlock
https://github.com/cockroachdb/cockroach/issues/7972
@a_soldatenko
@a_soldatenko
7 simple rules for debugging
concurrency applications
- Never assume a particular order of execution
- Implement concurrency at the highest level possible
- Don’t forget Go only detects when the program as a
whole freezes, not when a subset of goroutines get
stuck.
- STRACE
@a_soldatenko
7 simple rules for debugging
concurrency applications
- conditional breakpoints your best friend
- DEBUG=schedtrace=5000
- go-deadlock
@a_soldatenko
References 1
- https://github.com/golang/go/blob/release-
branch.go1.14/src/runtime/HACKING.md
- https://github.com/golang/go/wiki/LearnConcurrency
- https://rakyll.org/go-cloud/
- https://yourbasic.org/golang/detect-deadlock/
@a_soldatenko
References 2
- https://blog.minio.io/debugging-go-routine-leaks-
a1220142d32c
- https://golang.org/src/cmd/link/internal/ld/dwarf.go
- https://golang.org/src/runtime/runtime-gdb.py
- https://cseweb.ucsd.edu/~yiying/GoStudy-
ASPLOS19.pdf
- https://golang.org/doc/articles/race_detector.html
Telegram channel
https://t.me/golang_for_two
Slides:
@a_soldatenko
Thank You
@a_soldatenko
Questions ?
@a_soldatenko

More Related Content

PDF
1. Docker Introduction.pdf
PPTX
DEVSECOPS.pptx
PPTX
Docker Security workshop slides
PDF
What is Jenkins | Jenkins Tutorial for Beginners | Edureka
PDF
CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
PPTX
Présentation DEVOPS.pptx
PPTX
The Journey to DevSecOps
PDF
Microsoft Hyper-V explained
1. Docker Introduction.pdf
DEVSECOPS.pptx
Docker Security workshop slides
What is Jenkins | Jenkins Tutorial for Beginners | Edureka
CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
Présentation DEVOPS.pptx
The Journey to DevSecOps
Microsoft Hyper-V explained

What's hot (20)

PPTX
Introduction to GItlab CICD Presentation.pptx
PDF
Advanced debugging  techniques in different environments
PDF
Docker 101: Introduction to Docker
PDF
From DevOps to GitOps with GitLab
PPTX
Meetup 23 - 03 - Application Delivery on K8S with GitOps
PDF
Gitlab CI : Integration et Déploiement Continue
PDF
Docker Online Meetup #22: Docker Networking
PPTX
Docker Networking
PDF
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
PDF
Rapport de base de données gaci cui
PDF
Introduction to GitHub Actions
PDF
Introduction à OpenStack
PDF
Docker, Linux Containers (LXC), and security
PDF
파이썬 TDD 101
PDF
Introduction à DevOps
PPTX
Introduction to DevOps
PDF
Who Is A DevOps Engineer? | DevOps Skills You Must Master | DevOps Engineer M...
PDF
Kubernetes Networking with Cilium - Deep Dive
PPTX
Salesforce DevOps using GitHub Action
PDF
Etude et Mise en oeuvre d'une architecture de téléphonie sur IP sécurisée au ...
Introduction to GItlab CICD Presentation.pptx
Advanced debugging  techniques in different environments
Docker 101: Introduction to Docker
From DevOps to GitOps with GitLab
Meetup 23 - 03 - Application Delivery on K8S with GitOps
Gitlab CI : Integration et Déploiement Continue
Docker Online Meetup #22: Docker Networking
Docker Networking
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
Rapport de base de données gaci cui
Introduction to GitHub Actions
Introduction à OpenStack
Docker, Linux Containers (LXC), and security
파이썬 TDD 101
Introduction à DevOps
Introduction to DevOps
Who Is A DevOps Engineer? | DevOps Skills You Must Master | DevOps Engineer M...
Kubernetes Networking with Cilium - Deep Dive
Salesforce DevOps using GitHub Action
Etude et Mise en oeuvre d'une architecture de téléphonie sur IP sécurisée au ...
Ad

Similar to Debugging concurrency programs in go (20)

PDF
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
PDF
Debugging of (C)Python applications
PDF
Debugging Hung Python Processes With GDB
PDF
Golang dot-testing-lite
PPTX
PDF
eBPF Tooling and Debugging Infrastructure
PDF
A CTF Hackers Toolbox
PPT
为什么 rust-lang 吸引我?
PDF
Windbg랑 친해지기
PPTX
A Replay Approach to Software Validation
PDF
Python于Web 2.0网站的应用 - QCon Beijing 2010
PPTX
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
PPTX
C Programming Homework Help
PDF
Go 1.10 Release Party - PDX Go
ODP
Отладка в GDB
PPT
C Tutorials
PPTX
Introduction to Debuggers
ODP
The why and how of moving to php 5.4/5.5
PDF
2 debugging-c
PPTX
Go Native : Squeeze the juice out of your 64-bit processor using C++
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
Debugging of (C)Python applications
Debugging Hung Python Processes With GDB
Golang dot-testing-lite
eBPF Tooling and Debugging Infrastructure
A CTF Hackers Toolbox
为什么 rust-lang 吸引我?
Windbg랑 친해지기
A Replay Approach to Software Validation
Python于Web 2.0网站的应用 - QCon Beijing 2010
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
C Programming Homework Help
Go 1.10 Release Party - PDX Go
Отладка в GDB
C Tutorials
Introduction to Debuggers
The why and how of moving to php 5.4/5.5
2 debugging-c
Go Native : Squeeze the juice out of your 64-bit processor using C++
Ad

More from Andrii Soldatenko (12)

PDF
Building robust and friendly command line applications in go
PDF
Origins of Serverless
PDF
Building serverless-applications
PDF
Building Serverless applications with Python
PDF
Building social network with Neo4j and Python
PDF
What is the best full text search engine for Python?
PDF
Practical continuous quality gates for development process
PDF
Kyiv.py #16 october 2015
PDF
PyCon Russian 2015 - Dive into full text search with python.
PDF
PyCon 2015 Belarus Andrii Soldatenko
PDF
PyCon Ukraine 2014
PDF
SeleniumCamp 2015 Andrii Soldatenko
Building robust and friendly command line applications in go
Origins of Serverless
Building serverless-applications
Building Serverless applications with Python
Building social network with Neo4j and Python
What is the best full text search engine for Python?
Practical continuous quality gates for development process
Kyiv.py #16 october 2015
PyCon Russian 2015 - Dive into full text search with python.
PyCon 2015 Belarus Andrii Soldatenko
PyCon Ukraine 2014
SeleniumCamp 2015 Andrii Soldatenko

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
L1 - Introduction to python Backend.pptx
PPTX
assetexplorer- product-overview - presentation
PDF
Digital Strategies for Manufacturing Companies
PDF
top salesforce developer skills in 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Systems & Binary Numbers (comprehensive )
history of c programming in notes for students .pptx
Nekopoi APK 2025 free lastest update
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Softaken Excel to vCard Converter Software.pdf
iTop VPN Free 5.6.0.5262 Crack latest version 2025
L1 - Introduction to python Backend.pptx
assetexplorer- product-overview - presentation
Digital Strategies for Manufacturing Companies
top salesforce developer skills in 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Odoo Companies in India – Driving Business Transformation.pdf
Transform Your Business with a Software ERP System
PTS Company Brochure 2025 (1).pdf.......
Operating system designcfffgfgggggggvggggggggg
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Systems & Binary Numbers (comprehensive )

Debugging concurrency programs in go