diff --git a/common/service/config/pprof.go b/common/service/config/pprof.go index 7d2c04196fe..26f70deebf2 100644 --- a/common/service/config/pprof.go +++ b/common/service/config/pprof.go @@ -23,6 +23,7 @@ package config import ( "fmt" "net/http" + "sync/atomic" // DO NOT REMOVE THE LINE BELOW _ "net/http/pprof" @@ -37,6 +38,15 @@ type ( } ) +const ( + pprofNotInitialized int32 = 0 + pprofInitialized int32 = 1 +) + +// the pprof should only be initialized once per process +// otherwise, the caller / worker will experience weird issue +var pprofStatus = pprofNotInitialized + // NewInitializer create a new instance of PProf Initializer func (cfg *PProf) NewInitializer(logger bark.Logger) *PProfInitializerImpl { return &PProfInitializerImpl{ @@ -53,10 +63,11 @@ func (initializer *PProfInitializerImpl) Start() error { return nil } - go func() { - initializer.Logger.Info("PProf listen on %d", port) - http.ListenAndServe(fmt.Sprintf("localhost:%d", port), nil) - }() - + if atomic.CompareAndSwapInt32(&pprofStatus, pprofNotInitialized, pprofInitialized) { + go func() { + initializer.Logger.Infof("PProf listen on %d", port) + http.ListenAndServe(fmt.Sprintf("localhost:%d", port), nil) + }() + } return nil }