From aade14ffce4dde136b57101925c188833f947b89 Mon Sep 17 00:00:00 2001 From: Yota Hamada Date: Wed, 24 Apr 2024 19:26:41 +0900 Subject: [PATCH] fix: data race (#560) --- internal/sock/server.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/sock/server.go b/internal/sock/server.go index 27f89d3ff..b2b9f2468 100644 --- a/internal/sock/server.go +++ b/internal/sock/server.go @@ -9,6 +9,8 @@ import ( "net/http" "os" "strings" + "sync" + "sync/atomic" "github.com/dagu-dev/dagu/internal/utils" ) @@ -17,7 +19,8 @@ import ( type Server struct { *Config listener net.Listener - quit bool + quit atomic.Bool + mu sync.Mutex } type Config struct { @@ -32,7 +35,6 @@ type HttpHandlerFunc func(w http.ResponseWriter, r *http.Request) func NewServer(c *Config) (*Server, error) { return &Server{ Config: c, - quit: false, }, nil } @@ -61,7 +63,7 @@ func (svr *Server) Serve(listen chan error) error { }() for { conn, err := svr.listener.Accept() - if svr.quit { + if svr.quit.Load() { return ErrServerRequestedShutdown } if err == nil { @@ -79,8 +81,10 @@ func (svr *Server) Serve(listen chan error) error { // Shutdown stops the frontend. func (svr *Server) Shutdown() error { - if !svr.quit { - svr.quit = true + svr.mu.Lock() + defer svr.mu.Unlock() + if !svr.quit.Load() { + svr.quit.Store(true) if svr.listener != nil { err := svr.listener.Close() utils.LogErr("close listener", err)