Closed mvdan closed 3 years ago
Yep, you're absolutely right. Bolthold is already written this way, but I haven't made the change over here yet:
This was biting us and preventing us from writing parallel tests, so I sent https://github.com/timshannon/badgerhold/pull/58.
I've been digging into a data race caught by
go test -race
in a downstream project, summarized below:This seems to be because
Open
sets globals likeencode
anddecode
, which are used by methods onStore
later on.This feels wrong - the moment you open a bunch of badgerhold DBs and try to use them, at best you're going to get the wrong behavior (only one wins the write to the global), and at worst you're going to get a panic like the one above.
I think the fix here is simple; the
encode
anddecode
variables shouldn't be globals, but rather fields on eachStore
struct.I also strongly suggest you to set up a test that creates many DBs in parallel and reads+writes to them, and ensure that
go test -race
is happy. If you haven't done any concurrency safety testing before, I imagine there will be a few more data races lurking in your code :)