Skip to content

Commit

Permalink
Fix potential deadlock in ByParam
Browse files Browse the repository at this point in the history
Fixes #11039
  • Loading branch information
bep committed May 29, 2023
1 parent d47225c commit 3258569
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
30 changes: 22 additions & 8 deletions langs/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ type Language struct {
translator locales.Translator
timeFormatter htime.TimeFormatter
tag language.Tag
collator *Collator
location *time.Location
// collator1 and collator2 are the same, we have 2 to prevent deadlocks.
collator1 *Collator
collator2 *Collator

location *time.Location

// This is just an alias of Site.Params.
params maps.Params
Expand All @@ -58,14 +61,20 @@ func NewLanguage(lang, defaultContentLanguage, timeZone string, languageConfig L
}
}

var coll *Collator
var coll1, coll2 *Collator
tag, err := language.Parse(lang)
if err == nil {
coll = &Collator{
coll1 = &Collator{
c: collate.New(tag),
}
coll2 = &Collator{
c: collate.New(tag),
}
} else {
coll = &Collator{
coll1 = &Collator{
c: collate.New(language.English),
}
coll2 = &Collator{
c: collate.New(language.English),
}
}
Expand All @@ -76,7 +85,8 @@ func NewLanguage(lang, defaultContentLanguage, timeZone string, languageConfig L
translator: translator,
timeFormatter: htime.NewTimeFormatter(translator),
tag: tag,
collator: coll,
collator1: coll1,
collator2: coll2,
}

return l, l.loadLocation(timeZone)
Expand Down Expand Up @@ -165,8 +175,12 @@ func GetLocation(l *Language) *time.Location {
return l.location
}

func GetCollator(l *Language) *Collator {
return l.collator
func GetCollator1(l *Language) *Collator {
return l.collator1
}

func GetCollator2(l *Language) *Collator {
return l.collator2
}

type Collator struct {
Expand Down
8 changes: 5 additions & 3 deletions resources/page/pages_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var collatorStringSort = func(getString func(Page) string) func(p Pages) {
// Pages may be a mix of multiple languages, so we need to use the language
// for the currently rendered Site.
currentSite := p[0].Site().Current()
coll := langs.GetCollator(currentSite.Language())
coll := langs.GetCollator1(currentSite.Language())
coll.Lock()
defer coll.Unlock()

Expand All @@ -173,7 +173,7 @@ var collatorStringSort = func(getString func(Page) string) func(p Pages) {

var collatorStringCompare = func(getString func(Page) string, p1, p2 Page) int {
currentSite := p1.Site().Current()
coll := langs.GetCollator(currentSite.Language())
coll := langs.GetCollator1(currentSite.Language())
coll.Lock()
c := coll.CompareStrings(getString(p1), getString(p2))
coll.Unlock()
Expand All @@ -182,7 +182,9 @@ var collatorStringCompare = func(getString func(Page) string, p1, p2 Page) int {

var collatorStringLess = func(p Page) (less func(s1, s2 string) bool, close func()) {
currentSite := p.Site().Current()
coll := langs.GetCollator(currentSite.Language())
// Make sure to use the second collator to prevent deadlocks.
// See issue 11039.
coll := langs.GetCollator2(currentSite.Language())
coll.Lock()
return func(s1, s2 string) bool {
return coll.CompareStrings(s1, s2) < 1
Expand Down
2 changes: 1 addition & 1 deletion resources/page/taxonomy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (i Taxonomy) Alphabetical() OrderedTaxonomy {
return ia
}
currentSite := p.Site().Current()
coll := langs.GetCollator(currentSite.Language())
coll := langs.GetCollator1(currentSite.Language())
coll.Lock()
defer coll.Unlock()
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
Expand Down
2 changes: 1 addition & 1 deletion tpl/collections/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (ns *Namespace) Sort(l any, args ...any) (any, error) {
return nil, errors.New("can't sort " + reflect.ValueOf(l).Type().String())
}

collator := langs.GetCollator(ns.deps.Conf.Language())
collator := langs.GetCollator1(ns.deps.Conf.Language())

// Create a list of pairs that will be used to do the sort
p := pairList{Collator: collator, sortComp: ns.sortComp, SortAsc: true, SliceType: sliceType}
Expand Down

0 comments on commit 3258569

Please sign in to comment.