Goroutine Without Recover (ARCH5169)
Esta página aún no está disponible en tu idioma.
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.
Incorrect
Section titled “Incorrect”func dispatch(jobs []Job) { for _, j := range jobs { go func(j Job) { handle(j) // a panic crashes the program }(j) }}Correct
Section titled “Correct”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.