shithub: hugo

Download patch

ref: 6408c1cbc87f03fc9e92471165886ddbe7cdbeae
parent: 522ba1cd98ac67482061448c3a7528e68a720f0d
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Tue Jun 16 06:42:41 EDT 2020

Fix server data race/nil pointer in withMaps

Fixes #7392

--- a/hugolib/content_map_page.go
+++ b/hugolib/content_map_page.go
@@ -47,7 +47,6 @@
 		workers: para.New(h.numWorkers),
 		pmaps:   mps,
 	}
-
 }
 
 type pageMap struct {
--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -78,7 +78,8 @@
 	// As loaded from the /data dirs
 	data map[string]interface{}
 
-	content *pageMaps
+	contentInit sync.Once
+	content     *pageMaps
 
 	// Keeps track of bundle directories and symlinks to enable partial rebuilding.
 	ContentChanges *contentChangeMap
@@ -92,6 +93,13 @@
 	*testCounters
 }
 
+func (h *HugoSites) getContentMaps() *pageMaps {
+	h.contentInit.Do(func() {
+		h.content = newPageMaps(h)
+	})
+	return h.content
+}
+
 // Only used in tests.
 type testCounters struct {
 	contentRenderCounter uint64
@@ -253,7 +261,7 @@
 func (h *HugoSites) GetContentPage(filename string) page.Page {
 	var p page.Page
 
-	h.content.walkBundles(func(b *contentNode) bool {
+	h.getContentMaps().walkBundles(func(b *contentNode) bool {
 		if b.p == nil || b.fi == nil {
 			return false
 		}
@@ -706,7 +714,7 @@
 }
 
 func (h *HugoSites) removePageByFilename(filename string) {
-	h.content.withMaps(func(m *pageMap) error {
+	h.getContentMaps().withMaps(func(m *pageMap) error {
 		m.deleteBundleMatching(func(b *contentNode) bool {
 			if b.p == nil {
 				return false
@@ -897,7 +905,7 @@
 }
 
 func (h *HugoSites) resetPageState() {
-	h.content.walkBundles(func(n *contentNode) bool {
+	h.getContentMaps().walkBundles(func(n *contentNode) bool {
 		if n.p == nil {
 			return false
 		}
@@ -914,7 +922,7 @@
 }
 
 func (h *HugoSites) resetPageStateFromEvents(idset identity.Identities) {
-	h.content.walkBundles(func(n *contentNode) bool {
+	h.getContentMaps().walkBundles(func(n *contentNode) bool {
 		if n.p == nil {
 			return false
 		}
--- a/hugolib/hugo_sites_build.go
+++ b/hugolib/hugo_sites_build.go
@@ -263,7 +263,7 @@
 		return nil
 	}
 
-	if err := h.content.AssemblePages(); err != nil {
+	if err := h.getContentMaps().AssemblePages(); err != nil {
 		return err
 	}
 
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1296,13 +1296,11 @@
 
 	proc := newPagesProcessor(s.h, sourceSpec)
 
-	c := newPagesCollector(sourceSpec, s.h.content, s.Log, s.h.ContentChanges, proc, filenames...)
+	c := newPagesCollector(sourceSpec, s.h.getContentMaps(), s.Log, s.h.ContentChanges, proc, filenames...)
 
 	if err := c.Collect(); err != nil {
 		return err
 	}
-
-	s.h.content = newPageMaps(s.h)
 
 	return nil
 }