Skip to content

Commit

Permalink
fixes data race in state.Slot (#5067)
Browse files Browse the repository at this point in the history
* fixes data race in state/getters
  • Loading branch information
farazdagi committed Mar 11, 2020
1 parent 8d3fc1a commit 46eb228
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions beacon-chain/state/BUILD.bazel
Expand Up @@ -34,6 +34,7 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"getters_test.go",
"references_test.go",
"types_test.go",
],
Expand Down
3 changes: 3 additions & 0 deletions beacon-chain/state/getters.go
Expand Up @@ -134,6 +134,9 @@ func (b *BeaconState) Slot() uint64 {
if !b.HasInnerState() {
return 0
}
b.lock.RLock()
defer b.lock.RUnlock()

return b.state.Slot
}

Expand Down
25 changes: 25 additions & 0 deletions beacon-chain/state/getters_test.go
@@ -0,0 +1,25 @@
package state

import (
"sync"
"testing"

pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)

func TestBeaconState_SlotDataRace(t *testing.T) {
headState, _ := InitializeFromProto(&pb.BeaconState{Slot: 1})

wg := sync.WaitGroup{}
wg.Add(2)
go func() {
headState.SetSlot(uint64(0))
wg.Done()
}()
go func() {
headState.Slot()
wg.Done()
}()

wg.Wait()
}

0 comments on commit 46eb228

Please sign in to comment.