Skip to content

Commit

Permalink
fix: data race (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
yottahmd authored Apr 24, 2024
1 parent 952b12d commit aade14f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/sock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"os"
"strings"
"sync"
"sync/atomic"

"github.com/dagu-dev/dagu/internal/utils"
)
Expand All @@ -17,7 +19,8 @@ import (
type Server struct {
*Config
listener net.Listener
quit bool
quit atomic.Bool
mu sync.Mutex
}

type Config struct {
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit aade14f

Please sign in to comment.