Skip to content

Commit

Permalink
Prevent the global error collector to panic when sending on closed ch…
Browse files Browse the repository at this point in the history
…annel
  • Loading branch information
bep committed Apr 13, 2023
1 parent 5596dc2 commit 9906c1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 11 additions & 0 deletions deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
}
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9906c1a

Please sign in to comment.