From 9906c1ae52e44f2e8ed45873ea36cd83a9e9bcc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 13 Apr 2023 11:44:22 +0200 Subject: [PATCH] Prevent the global error collector to panic when sending on closed channel --- deps/deps.go | 11 +++++++++++ hugolib/hugo_sites_build.go | 7 +------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/deps/deps.go b/deps/deps.go index 7b252d020..511ee885c 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -119,6 +119,8 @@ type Deps struct { type globalErrHandler struct { // Channel for some "hard to get to" build errors buildErrors chan error + // Used to signal that the build is done. + quit chan struct{} } // SendErr sends the error on a channel to be handled later. @@ -127,6 +129,7 @@ type globalErrHandler struct { func (e *globalErrHandler) SendError(err error) { if e.buildErrors != nil { select { + case <-e.quit: case e.buildErrors <- err: default: } @@ -137,10 +140,18 @@ func (e *globalErrHandler) SendError(err error) { } func (e *globalErrHandler) StartErrorCollector() chan error { + e.quit = make(chan struct{}) e.buildErrors = make(chan error, 10) return e.buildErrors } +func (e *globalErrHandler) StopErrorCollector() { + if e.buildErrors != nil { + close(e.quit) + close(e.buildErrors) + } +} + // Listeners represents an event listener. type Listeners struct { sync.Mutex diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index 66abf4f16..e61dc9876 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -163,12 +163,7 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error { h.Log.Println(b.String()) } - select { - // Make sure the channel always gets something. - case errCollector <- nil: - default: - } - close(errCollector) + h.StopErrorCollector() err := <-errs if err != nil {