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

x/text/collate: Collator is not concurrency safe #67286

Closed
flimzy opened this issue May 10, 2024 · 1 comment
Closed

x/text/collate: Collator is not concurrency safe #67286

flimzy opened this issue May 10, 2024 · 1 comment
Milestone

Comments

@flimzy
Copy link
Contributor

flimzy commented May 10, 2024

Go version

1.22.0

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jonhall/.cache/go-build'
GOENV='/home/jonhall/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jonhall/go/pkg/mod'
GONOPROXY='gitlab.com/FlashbackSRS/priv,gitlab.com/flimzy/hacker-portfolio'
GONOSUMDB='gitlab.com/FlashbackSRS/priv,gitlab.com/flimzy/hacker-portfolio'
GOOS='linux'
GOPATH='/home/jonhall/go'
GOPRIVATE='gitlab.com/FlashbackSRS/priv,gitlab.com/flimzy/hacker-portfolio'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='/usr/bin/gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/jonhall/src/kivik/kivik/x/sqlite/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build697470253=/tmp/go-build -gno-record-gcc-switches'

What did you do?

go test -race with this file:

package main

import (
	"testing"

	"golang.org/x/text/collate"
	"golang.org/x/text/language"
)

func TestCollate(t *testing.T) {
	collator := collate.New(language.Und)

	go collator.CompareString("a", "b")
	go collator.CompareString("a", "b")
}

And my go.mod:

module github.com/flimzy/test

go 1.22.0

require golang.org/x/text v0.15.0

What did you see happen?

A data race:

$ go test -race .
PASS
==================
WARNING: DATA RACE
Write at 0x00c0001fa8b0 by goroutine 8:
  golang.org/x/text/internal/colltab.(*Iter).SetInputString()
      /home/jonhall/go/pkg/mod/golang.org/x/text@v0.15.0/internal/colltab/iter.go:67 +0x52
  golang.org/x/text/collate.(*Collator).CompareString()
      /home/jonhall/go/pkg/mod/golang.org/x/text@v0.15.0/collate/collate.go:124 +0x4a
  github.com/flimzy/test.TestCollate.gowrap1()
      /home/jonhall/src/test/test_test.go:13 +0x49

<snip>

What did you expect to see?

I was not expecting a race.

While it's perhaps reasonable to mutate internal state whe calling a comparison function, it's not at all obvious that it would or should, at least not to someone like me who is unfamiliar with the mechanics of collation logic.

I would like to request that, at minimum, the package is documented to not be concurrency-safe.

As a bonus, making it concurrency-safe would be even better, if feasible.

@gopherbot gopherbot added this to the Unreleased milestone May 10, 2024
@seankhliao
Copy link
Member

In general, the expectation is that most objects with pointer methods aren't concurrency safe unless explicitly documented to be so. E.g. regexp.Regexp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants