Skip to content

Commit

Permalink
Fix potential deadlock in ByParam
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 29, 2023
1 parent d47225c commit 742fae6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
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

0 comments on commit 742fae6

Please sign in to comment.