diff --git a/lib/atexit/atexit.go b/lib/atexit/atexit.go index 363d3267b..fa1dcc8b7 100644 --- a/lib/atexit/atexit.go +++ b/lib/atexit/atexit.go @@ -115,12 +115,18 @@ func Run() { // // So cancelFunc will be run if the function exits with an error or // at exit. +// +// cancelFunc will only be run once. func OnError(perr *error, fn func()) func() { - handle := Register(fn) + var once sync.Once + onceFn := func() { + once.Do(fn) + } + handle := Register(onceFn) return func() { defer Unregister(handle) if *perr != nil { - fn() + onceFn() } }