Aller au contenu

Goroutine Without Recover (ARCH5169)

Ce contenu n’est pas encore disponible dans votre langue.

Key go.logging.goroutine-no-recover · Severity info · Category logging

A panic inside a goroutine crashes the whole program if it isn’t recovered and logged, and the error vanishes silently. In fire-and-forget goroutines, errors should be caught and logged or reported back via a channel.

func dispatch(jobs []Job) {
for _, j := range jobs {
go func(j Job) {
handle(j) // a panic crashes the program
}(j)
}
}
func dispatch(log *slog.Logger, jobs []Job) {
for _, j := range jobs {
go func(j Job) {
defer func() {
if r := recover(); r != nil {
log.Error("job panicked", "id", j.ID, "panic", r)
}
}()
handle(j)
}(j)
}
}

Add a defer with recover at the top of each goroutine and log or report the panic.