Why This Happens
In Go, a panic that propagates to the top of a goroutine's call stack terminates the entire program, not just that goroutine. Unlike main, goroutines do not have a default panic handler. You must add your own recover for goroutines that could encounter panics.
The Problem
func main() {
go func() {
panic("something went wrong") // crashes entire program
}()
time.Sleep(time.Second)
}The Fix
func main() {
go func() {
defer func() {
if r := recover(); r != nil {
log.Printf("goroutine recovered: %v", r)
}
}()
panic("something went wrong") // recovered
}()
time.Sleep(time.Second)
}Step-by-Step Fix
- 1
Identify unprotected goroutines
Find goroutines that do not have a deferred recover and could potentially panic.
- 2
Add deferred recover
Add a defer func with recover at the start of the goroutine function.
- 3
Log and handle
Log the recovered panic value and take appropriate action like sending an error to a channel.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free