From 06bc8cf90128cb6a1ed7633f6fc4cadfb66526d1 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 6 Dec 2024 14:56:54 -0500 Subject: [PATCH] fix(daemon): use atomic.Bool for server --- pkg/daemon/daemon.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 10449901..e4e6340d 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strings" "sync" + "sync/atomic" "time" "github.com/charmbracelet/log" @@ -51,7 +52,7 @@ type GitDaemon struct { wg sync.WaitGroup once sync.Once logger *log.Logger - done bool // indicates if the server has been closed + done atomic.Bool // indicates if the server has been closed } // NewDaemon returns a new Git daemon. @@ -72,7 +73,7 @@ func NewGitDaemon(ctx context.Context) (*GitDaemon, error) { // ListenAndServe starts the Git TCP daemon. func (d *GitDaemon) ListenAndServe() error { - if d.done { + if d.done.Load() { return ErrServerClosed } listener, err := net.Listen("tcp", d.addr) @@ -84,7 +85,7 @@ func (d *GitDaemon) ListenAndServe() error { // Serve listens on the TCP network address and serves Git requests. func (d *GitDaemon) Serve(listener net.Listener) error { - if d.done { + if d.done.Load() { return ErrServerClosed } @@ -317,19 +318,19 @@ func (d *GitDaemon) Close() error { // closeListener closes the listener and the finished channel. func (d *GitDaemon) closeListener() error { - if d.done { + if d.done.Load() { return ErrServerClosed } d.once.Do(func() { close(d.finished) - d.done = true + d.done.Store(true) }) return nil } // Shutdown gracefully shuts down the daemon. func (d *GitDaemon) Shutdown(ctx context.Context) error { - if d.done { + if d.done.Load() { return ErrServerClosed }