Fix server data race/nil pointer in withMaps

Fixes #7392
This commit is contained in:
Bjørn Erik Pedersen 2020-06-16 10:42:41 +02:00
parent 522ba1cd98
commit 6408c1cbc8
4 changed files with 15 additions and 10 deletions

View file

@ -47,7 +47,6 @@ func newPageMaps(h *HugoSites) *pageMaps {
workers: para.New(h.numWorkers), workers: para.New(h.numWorkers),
pmaps: mps, pmaps: mps,
} }
} }
type pageMap struct { type pageMap struct {

View file

@ -78,6 +78,7 @@ type HugoSites struct {
// As loaded from the /data dirs // As loaded from the /data dirs
data map[string]interface{} data map[string]interface{}
contentInit sync.Once
content *pageMaps content *pageMaps
// Keeps track of bundle directories and symlinks to enable partial rebuilding. // Keeps track of bundle directories and symlinks to enable partial rebuilding.
@ -92,6 +93,13 @@ type HugoSites struct {
*testCounters *testCounters
} }
func (h *HugoSites) getContentMaps() *pageMaps {
h.contentInit.Do(func() {
h.content = newPageMaps(h)
})
return h.content
}
// Only used in tests. // Only used in tests.
type testCounters struct { type testCounters struct {
contentRenderCounter uint64 contentRenderCounter uint64
@ -253,7 +261,7 @@ func (h *HugoSites) PrintProcessingStats(w io.Writer) {
func (h *HugoSites) GetContentPage(filename string) page.Page { func (h *HugoSites) GetContentPage(filename string) page.Page {
var p 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 { if b.p == nil || b.fi == nil {
return false return false
} }
@ -706,7 +714,7 @@ func (h *HugoSites) renderCrossSitesArtifacts() error {
} }
func (h *HugoSites) removePageByFilename(filename string) { 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 { m.deleteBundleMatching(func(b *contentNode) bool {
if b.p == nil { if b.p == nil {
return false return false
@ -897,7 +905,7 @@ func (h *HugoSites) findPagesByKindIn(kind string, inPages page.Pages) page.Page
} }
func (h *HugoSites) resetPageState() { func (h *HugoSites) resetPageState() {
h.content.walkBundles(func(n *contentNode) bool { h.getContentMaps().walkBundles(func(n *contentNode) bool {
if n.p == nil { if n.p == nil {
return false return false
} }
@ -914,7 +922,7 @@ func (h *HugoSites) resetPageState() {
} }
func (h *HugoSites) resetPageStateFromEvents(idset identity.Identities) { 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 { if n.p == nil {
return false return false
} }

View file

@ -263,7 +263,7 @@ func (h *HugoSites) assemble(bcfg *BuildCfg) error {
return nil return nil
} }
if err := h.content.AssemblePages(); err != nil { if err := h.getContentMaps().AssemblePages(); err != nil {
return err return err
} }

View file

@ -1296,14 +1296,12 @@ func (s *Site) readAndProcessContent(filenames ...string) error {
proc := newPagesProcessor(s.h, sourceSpec) 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 { if err := c.Collect(); err != nil {
return err return err
} }
s.h.content = newPageMaps(s.h)
return nil return nil
} }