You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
jnml@4670:~/src/github.com/cznic/token_issue$ go test -race
PASS
ok github.com/cznic/token_issue 1.005s
jnml@4670:~/src/github.com/cznic/token_issue$
What did you see instead?
jnml@4670:~/src/github.com/cznic/token_issue$ go test -race
==================
WARNING: DATA RACE
Write at 0x00c4200660e8 by goroutine 7:
go/token.(*File).AddLine()
/home/jnml/go/src/go/token/position.go:132 +0x15b
github.com/cznic/token_issue.TestRace.func1()
/home/jnml/src/github.com/cznic/token_issue/race_test.go:20 +0x6a
Previous read at 0x00c4200660e8 by goroutine 8:
go/token.(*File).unpack()
/home/jnml/go/src/go/token/position.go:271 +0x83
go/token.(*File).position()
/home/jnml/go/src/go/token/position.go:290 +0x9d
go/token.(*FileSet).PositionFor()
/home/jnml/go/src/go/token/position.go:449 +0xc7
github.com/cznic/token_issue.TestRace.func2()
/home/jnml/src/github.com/cznic/token_issue/race_test.go:28 +0xa3
Goroutine 7 (running) created at:
github.com/cznic/token_issue.TestRace()
/home/jnml/src/github.com/cznic/token_issue/race_test.go:23 +0x6e
testing.tRunner()
/home/jnml/go/src/testing/testing.go:610 +0xc9
Goroutine 8 (running) created at:
github.com/cznic/token_issue.TestRace()
/home/jnml/src/github.com/cznic/token_issue/race_test.go:31 +0x90
testing.tRunner()
/home/jnml/go/src/testing/testing.go:610 +0xc9
==================
PASS
Found 1 data race(s)
exit status 66
FAIL github.com/cznic/token_issue 1.021s
jnml@4670:~/src/github.com/cznic/token_issue$
Discussion
Methods of FileSet are documented to be safe for concurrent use by multiple goroutines, so FileSet is protected by a mutex and all its methods use it to prevent concurrent mutations. All methods of File that mutate the respective FileSet, including AddLine, do also lock its mutex, but that does not help when PositionFor is invoked concurrently and reads without synchronization what AddLine mutates.
I think a mutex [r]lock is missing in PositionFor.
The text was updated successfully, but these errors were encountered:
go version
)?go version go1.7rc3 linux/amd64
go env
)?Copy this code to somewhere in your $GOPATH and name it, for example,
race_test.go
.cd
to that place and invoke$ go test -race
Methods of
FileSet
are documented to be safe for concurrent use by multiple goroutines, soFileSet
is protected by a mutex and all its methods use it to prevent concurrent mutations. All methods ofFile
that mutate the respectiveFileSet
, includingAddLine
, do also lock its mutex, but that does not help whenPositionFor
is invoked concurrently and reads without synchronization whatAddLine
mutates.I think a mutex [r]lock is missing in
PositionFor
.The text was updated successfully, but these errors were encountered: