From 2c47053cfe127e2a2b98160e1528dfcc0f8db445 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Fri, 19 Jun 2020 14:27:23 +0300 Subject: [PATCH] - autohosts: fix crash on startup if filesystem watcher couldn't be initialized Close #1814 Squashed commit of the following: commit ba17a5b3c61a8a5282beb0cca470e2302e83c0d6 Author: Simon Zolin Date: Fri Jun 19 10:08:44 2020 +0300 - autohosts: fix crash on startup if filesystem watcher couldn't be initialized --- util/auto_hosts.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/util/auto_hosts.go b/util/auto_hosts.go index f36ee1e7..34a979da 100644 --- a/util/auto_hosts.go +++ b/util/auto_hosts.go @@ -76,17 +76,19 @@ func (a *AutoHosts) Start() { go a.updateLoop() a.updateChan <- true - go a.watcherLoop() + if a.watcher != nil { + go a.watcherLoop() - err := a.watcher.Add(a.hostsFn) - if err != nil { - log.Error("Error while initializing watcher for a file %s: %s", a.hostsFn, err) - } - - for _, dir := range a.hostsDirs { - err = a.watcher.Add(dir) + err := a.watcher.Add(a.hostsFn) if err != nil { - log.Error("Error while initializing watcher for a directory %s: %s", dir, err) + log.Error("Error while initializing watcher for a file %s: %s", a.hostsFn, err) + } + + for _, dir := range a.hostsDirs { + err = a.watcher.Add(dir) + if err != nil { + log.Error("Error while initializing watcher for a directory %s: %s", dir, err) + } } } } @@ -95,7 +97,9 @@ func (a *AutoHosts) Start() { func (a *AutoHosts) Close() { a.updateChan <- false close(a.updateChan) - _ = a.watcher.Close() + if a.watcher != nil { + _ = a.watcher.Close() + } } // update table