Skip to content

x/text/collate: Collator.CompareString and Compare are not safe for concurrent use #77927

@carli2

Description

@carli2

The Collator type in golang.org/x/text/collate stores two iterator
slots as struct fields (_iter [2]iter). Both Compare and CompareString
write into these shared fields during comparison, making concurrent
calls on the same Collator instance unsafe.

A data race occurs when a single Collator is used as a comparison
closure across multiple goroutines, for example as a Less() function
passed to parallel sort workers in a database engine.

The race can be detected with go test -race on any program that
calls CompareString or Compare from two goroutines sharing one
Collator.

A fix is proposed in golang/text#60 which
removes the shared iterator state and declares iter variables as
goroutine-local stack allocations in Compare and CompareString.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions