Skip to content

Commit

Permalink
fix(profiler): data race accessing s.server
Browse files Browse the repository at this point in the history
Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang committed Feb 2, 2024
1 parent b4fc1db commit 67ca394
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,37 @@ func (s *Server) EnableProfiler(portNumber int32) (string, error) {
}

profilerAddr := fmt.Sprintf(":%d", profilerPort)
s.server = &http.Server{
newServer := &http.Server{
Addr: profilerAddr,
ReadHeaderTimeout: 10 * time.Second,
}
go func() {
if err := s.server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logrus.WithError(err).Warnf("Get error when start profiler server %v", s.server.Addr)
s.server = nil
if err := newServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logrus.WithError(err).Warnf("Get error when start profiler server %v", newServer.Addr)
s.errMsg = err.Error()
return
}
logrus.Infof("Profiler server (%v) is closed", s.server.Addr)
logrus.Infof("Profiler server (%v) is closed", newServer.Addr)
}()

logrus.Infof("Waiting the profiler server(%v) to start", s.server.Addr)
logrus.Infof("Waiting the profiler server(%v) to start", newServer.Addr)
// Wait for the profiler server to start, and check the profiler server.
retryCount := 3
for i := 0; i < retryCount; i++ {
conn, err := net.DialTimeout("tcp", s.server.Addr, 1*time.Second)
conn, err := net.DialTimeout("tcp", newServer.Addr, 1*time.Second)
if err == nil {
_ = conn.Close()
break
}
}
if s.server == nil {

if s.errMsg != "" {
_ = newServer.Close()
return s.errMsg, fmt.Errorf("failed to start profiler server(%v)", profilerAddr)
}

s.server = newServer

defer func() {
s.errMsg = ""
}()
Expand Down

0 comments on commit 67ca394

Please sign in to comment.