Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

HashOnRead Has A Race Condition #49

Closed
bonedaddy opened this issue Apr 12, 2020 · 0 comments · Fixed by #50
Closed

HashOnRead Has A Race Condition #49

bonedaddy opened this issue Apr 12, 2020 · 0 comments · Fixed by #50

Comments

@bonedaddy
Copy link
Contributor

bonedaddy commented Apr 12, 2020

While working on some tests to detect race conditions with a blockstore wrapper I'm working on, I create multiple goroutines to execute the various blockstore functions. I noticed that when I had each goroutine call Blockstore::HashOnRead(num%2==0) where num is the goroutine number, that a race condition is triggered.

Subsequently I looked into the blockstore and found this:

type blockstore struct {
	datastore ds.Batching

	rehash bool
}

func (bs *blockstore) HashOnRead(enabled bool) {
	bs.rehash = enabled
}

This seems a little unsafe, since there's no documentation explicitly saying that this shouldn't be called by multiple goroutines. It should either be guarded by a mutex, or, alternatively use an atomic boolean

edit:

Doing some more digging, this race condition will not only effect HashOnRead, but also Get, GetSize

Stebalien added a commit that referenced this issue Apr 12, 2020
Instead, provide a simple `ValidatingBlockstore` wrapper. This:

1. Isn't racy (fixes #49).
2. Keeps the base blockstore simple.
Stebalien added a commit that referenced this issue Apr 13, 2020
Instead, provide a simple `ValidatingBlockstore` wrapper. This:

1. Isn't racy (fixes #49).
2. Keeps the base blockstore simple.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant