Prevent the global error collector to panic when sending on closed channel

This commit is contained in:
Bjørn Erik Pedersen 2023-04-13 11:44:22 +02:00
parent 5596dc24a0
commit 9906c1ae52
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 12 additions and 6 deletions

11
deps/deps.go vendored
View file

@ -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

View file

@ -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 {