From c37d6fc01a8b70ea3686a81c2acf9d3da0b5ef09 Mon Sep 17 00:00:00 2001 From: Konstantin Lebedev <9497591+kmlebedev@users.noreply.github.com> Date: Thu, 1 Sep 2022 22:34:06 +0500 Subject: [PATCH] avoid data race on grace.hooks (#3572) https://github.com/seaweedfs/seaweedfs/issues/3564 --- weed/util/grace/signal_handling.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/weed/util/grace/signal_handling.go b/weed/util/grace/signal_handling.go index fc7afcad9..24d2b184f 100644 --- a/weed/util/grace/signal_handling.go +++ b/weed/util/grace/signal_handling.go @@ -12,7 +12,7 @@ import ( var signalChan chan os.Signal var hooks = make([]func(), 0) -var hookLock sync.Mutex +var hookLock sync.RWMutex func init() { signalChan = make(chan os.Signal, 1) @@ -27,7 +27,9 @@ func init() { // syscall.SIGQUIT, ) go func() { - for _ = range signalChan { + hookLock.RLock() + defer hookLock.RUnlock() + for range signalChan { for _, hook := range hooks { hook() }