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

Fix potential deadlock in ByParam #11042

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 11 additions & 0 deletions langs/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,14 @@ type Collator struct {
func (c *Collator) CompareStrings(a, b string) int {
return c.c.CompareString(a, b)
}

// CompareStrings compares a and b using the Collator in l.
// It returns -1 if a < b, 1 if a > b and 0 if a == b.
// Note that the Collator is not thread safe, so you may want
// to acquire a lock on it before calling this method.
func CompareStrings(l *Language, a, b string) int {
l.collator.Lock()
defer l.collator.Unlock()
return l.collator.CompareStrings(a, b)

}
6 changes: 2 additions & 4 deletions resources/page/pages_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,6 @@ func (p Pages) ByParam(paramsKey any) Pages {
paramsKeyStr := cast.ToString(paramsKey)
key := "pageSort.ByParam." + paramsKeyStr

stringLess, close := collatorStringLess(p[0])
defer close()

paramsKeyComparator := func(p1, p2 Page) bool {
v1, _ := p1.Param(paramsKeyStr)
v2, _ := p2.Param(paramsKeyStr)
Expand Down Expand Up @@ -403,7 +400,8 @@ func (p Pages) ByParam(paramsKey any) Pages {
s1 := cast.ToString(v1)
s2 := cast.ToString(v2)

return stringLess(s1, s2)
// Hold the lock as short as possible, see #11039.
return langs.CompareStrings(p1.CurrentSection().Language(), s1, s2) < 0

}

Expand Down