sync.Pool behaviour under -race

279 views
Skip to first unread message

Matt Joiner

unread,
May 11, 2016, 10:38:31 PM5/11/16
to golang-nuts
I have some tests that check the behaviour of sync.Pool. When running the tests with -race, those tests fail. Specifically, putting an item into a sync.Pool does not mean it will be available immediately after. While this might not be a guarantee of sync.Pool anyway, what causes this differing behaviour here? How can I determine if the race detector is running, and skip this test in that instance?

Roberto Zanotto

unread,
May 11, 2016, 10:45:42 PM5/11/16
to golang-nuts
from: https://golang.org/src/sync/pool.go
// Under race detector the Pool degenerates into no-op.
// It's conforming, simple and does not introduce excessive
// happens-before edges between unrelated goroutines.

Basically you get a new item (or nil) each time you ask for one and putting back things into the pool does nothing.
When you have doubts, read the code :)

Matt Joiner

unread,
May 11, 2016, 10:47:29 PM5/11/16
to Roberto Zanotto, golang-nuts
Ah, thanks! How about the other part, detecting if the race detector is running? I'd assume something like testing.Race(), in line with testing.Verbose().

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/RU8rV_DwR5A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roberto Zanotto

unread,
May 11, 2016, 10:48:57 PM5/11/16
to golang-nuts
You should be able to check if the race detector is enabled with the "race" build tag
https://golang.org/src/internal/race/race.go


On Thursday, May 12, 2016 at 4:38:31 AM UTC+2, Matt Joiner wrote:

Caleb Spare

unread,
May 11, 2016, 10:49:00 PM5/11/16
to Matt Joiner, Roberto Zanotto, golang-nuts
You should instead fix your tests not to require any behavior that isn't guaranteed by the interface you're using.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Matt Joiner

unread,
May 16, 2016, 2:08:16 AM5/16/16
to Roberto Zanotto, golang-nuts
Thanks for that, it worked well.

--

Roberto Zanotto

unread,
May 16, 2016, 11:13:36 AM5/16/16
to golang-nuts, roby...@gmail.com
Glad I could help.
Consider also Caleb's advice, it's not your responsibility to test that Pool does its job.
Reply all
Reply to author
Forward
0 new messages