sourcegraph/lib/background/goroutine.go
James McNamara 960d97bf8b
bazel: first pass at moving moving logging linting into nogo (#58910)
* First pass at moving moving logging linting into Bazel

* fixed negation operators

* Update dev/linters/logging/logging.go

Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>

* added more exceptions and refactored one or two impls

* added nogo lint pragmas to offending files

* ran configure

* reverted git-combine refactor

* ran configure

* reverted test as well

---------

Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>
2024-01-02 10:07:25 -08:00

27 lines
727 B
Go

package background
import (
"log" //nolint:logging // Legacy and special case handling of panics in background routines
"runtime/debug"
)
// Go runs the given function in a goroutine and catches and logs panics.
//
// This prevents a single panicking goroutine from crashing the entire binary,
// which is undesirable for services with many different components, like our
// frontend service, where one location of code panicking could be catastrophic.
//
// More advanced use cases should copy this implementation and modify it.
func Go(f func()) {
go func() {
defer func() {
if err := recover(); err != nil {
stack := debug.Stack()
log.Printf("goroutine panic: %v\n%s", err, stack)
}
}()
f()
}()
}