Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl/partials: Fix partialCached deadlock regression #9592

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tpl/partials/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,44 @@ P2
`)
}

// Issue #588
func TestIncludeCachedRecursionShortcode(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
baseURL = 'http://example.com/'
-- content/_index.md --
---
title: "Index"
---
{{< short >}}
-- layouts/index.html --
{{ partials.IncludeCached "p1.html" . }}
-- layouts/partials/p1.html --
{{ .Content }}
{{ partials.IncludeCached "p2.html" . }}
-- layouts/partials/p2.html --
-- layouts/shortcodes/short.html --
SHORT
{{ partials.IncludeCached "p2.html" . }}
P2

`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

b.AssertFileContent("public/index.html", `
SHORT
P2
`)
}

func TestIncludeCacheHints(t *testing.T) {
t.Parallel()

Expand Down
38 changes: 19 additions & 19 deletions tpl/partials/partials.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,14 @@ func (ns *Namespace) getOrCreate(ctx context.Context, key partialCacheKey, conte
}
}()

// We may already have a write lock.
hasLock := tpl.GetHasLockFromContext(ctx)

if !hasLock {
ns.cachedPartials.RLock()
}
ns.cachedPartials.RLock()
p, ok := ns.cachedPartials.p[key]
if !hasLock {
ns.cachedPartials.RUnlock()
}
ns.cachedPartials.RUnlock()

if ok {
if ns.deps.Metrics != nil {
ns.deps.Metrics.TrackValue(key.templateName(), p, true)
// The templates that gets executed is measued in Execute.
// The templates that gets executed is measured in Execute.
// We need to track the time spent in the cache to
// get the totals correct.
ns.deps.Metrics.MeasureSince(key.templateName(), start)
Expand All @@ -256,21 +249,28 @@ func (ns *Namespace) getOrCreate(ctx context.Context, key partialCacheKey, conte
return p, nil
}

if !hasLock {
ns.cachedPartials.Lock()
defer ns.cachedPartials.Unlock()
ctx = tpl.SetHasLockInContext(ctx, true)
}

var name string
name, p, err = ns.include(ctx, key.name, context)
// This needs to be done outside the lock.
// See #9588
_, p, err = ns.include(ctx, key.name, context)
if err != nil {
return nil, err
}

ns.cachedPartials.Lock()
defer ns.cachedPartials.Unlock()
// Double-check.
if p2, ok := ns.cachedPartials.p[key]; ok {
if ns.deps.Metrics != nil {
ns.deps.Metrics.TrackValue(key.templateName(), p, true)
ns.deps.Metrics.MeasureSince(key.templateName(), start)
}
return p2, nil

}
if ns.deps.Metrics != nil {
ns.deps.Metrics.TrackValue(name, p, false)
ns.deps.Metrics.TrackValue(key.templateName(), p, false)
}

ns.cachedPartials.p[key] = p

return p, nil
Expand Down