hugolib: Prevent parallel server rebuilds
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 18 May 2019 06:38:58 +0000 (08:38 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 18 May 2019 07:01:40 +0000 (09:01 +0200)
There have been reports about infrequent paginator crashes when running the Hugo server since 0.55.0.

The reason have been narrowed down to that of parallel rebuilds.

This isn't a new thing, but the changes in 0.55.0 made it extra important to serialize the page initialization.

This commit fixes that by protecting the `Build` method with a lock when running in server mode.

Fixes #5885
Fixes #5968

hugolib/hugo_sites.go
hugolib/hugo_sites_build.go

index 6f95dbb12c3f2b280215d4d3c1998437ab7968f5..5be4ec02b75b324ed80064a6317b3e0a4ba97a3b 100644 (file)
@@ -62,6 +62,9 @@ type HugoSites struct {
        // If this is running in the dev server.
        running bool
 
+       // Serializes rebuilds when server is running.
+       runningMu sync.Mutex
+
        // Render output formats for all sites.
        renderFormats output.Formats
 
index d748a0169dd815ace68aed471317cd14971c80ba..7f725def2f952d3f17223ded7bd117177b2b1371 100644 (file)
@@ -31,6 +31,12 @@ import (
 // Build builds all sites. If filesystem events are provided,
 // this is considered to be a potential partial rebuild.
 func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {
+       if h.running {
+               // Make sure we don't trigger rebuilds in parallel.
+               h.runningMu.Lock()
+               defer h.runningMu.Unlock()
+       }
+
        ctx, task := trace.NewTask(context.Background(), "Build")
        defer task.End()