From cf9a748159fcf62e8d65e40c513cbdc9f213ac5c Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Sat, 4 Oct 2025 13:58:15 +0800 Subject: [PATCH] fix(watcher): Prevent infinite reload loop on rapid config changes --- internal/watcher/watcher.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 7011ce81..0d966ae0 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -430,8 +430,15 @@ func (w *Watcher) handleEvent(event fsnotify.Event) { } fmt.Printf("config file changed, reloading: %s\n", w.configPath) if w.reloadConfig() { + finalHash := newHash + if updatedData, errRead := os.ReadFile(w.configPath); errRead == nil && len(updatedData) > 0 { + sumUpdated := sha256.Sum256(updatedData) + finalHash = hex.EncodeToString(sumUpdated[:]) + } else if errRead != nil { + log.WithError(errRead).Debug("failed to compute updated config hash after reload") + } w.clientsMutex.Lock() - w.lastConfigHash = newHash + w.lastConfigHash = finalHash w.clientsMutex.Unlock() } return @@ -533,7 +540,14 @@ func (w *Watcher) reloadConfig() bool { log.Debugf(" remote-management.allow-remote: %t -> %t", oldConfig.RemoteManagement.AllowRemote, newConfig.RemoteManagement.AllowRemote) } if oldConfig.RemoteManagement.SecretKey != newConfig.RemoteManagement.SecretKey { - log.Debug(" remote-management.secret-key: updated") + switch { + case oldConfig.RemoteManagement.SecretKey == "" && newConfig.RemoteManagement.SecretKey != "": + log.Debug(" remote-management.secret-key: created") + case oldConfig.RemoteManagement.SecretKey != "" && newConfig.RemoteManagement.SecretKey == "": + log.Debug(" remote-management.secret-key: deleted") + default: + log.Debug(" remote-management.secret-key: updated") + } if newConfig.RemoteManagement.SecretKey == "" { log.Info("management routes will be disabled after secret key removal") } else {