Skip to content

Commit

Permalink
fix race condition when persisting to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
tranvictor committed Jun 14, 2017
1 parent af511a2 commit 3dcf32a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ethereum/stat/stat_recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"
)

var rig *ethereum.Rig = ethereum.NewRig("testrig")
var rig2 *ethereum.Rig = ethereum.NewRig("anotherrig")
var rig *ethereum.Rig = ethereum.NewRig("testrig", "192.168.1.2")
var rig2 *ethereum.Rig = ethereum.NewRig("anotherrig", "192.168.1.3")

func newStatRecorder() *StatRecorder {
return &StatRecorder{
Expand Down
4 changes: 4 additions & 0 deletions protocol/test_rig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ type testRig struct {
func (r *testRig) ID() string {
return "rig"
}

func (r *testRig) IP() string {
return "127.0.0.1"
}
2 changes: 1 addition & 1 deletion run_test.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build/env.sh go test -ldflags -s `go list ./... | grep -v vendor | grep -v experiment`
build/env.sh go test -ldflags -s `go list ./... | grep -v vendor | grep -v experiment | grep -v ethash`
6 changes: 4 additions & 2 deletions storage/gob_file_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ func getName(value interface{}) string {
}

func (gfs *GobFileStorage) register(value interface{}) {
gfs.mu.Lock()
defer gfs.mu.Unlock()
name := getName(value)
if _, found := gfs.registeredType[name]; !found {
gob.Register(value)
Expand All @@ -102,13 +100,17 @@ func (gfs *GobFileStorage) persistToFile(data interface{}, id string) error {
}

func (gfs *GobFileStorage) Persist(data interface{}, id string) error {
gfs.mu.Lock()
defer gfs.mu.Unlock()
if err := gfs.persistToFile(data, id); err != nil {
return err
}
return os.Rename(getTempFile(id), getFile(id))
}

func (gfs *GobFileStorage) Load(data interface{}, id string) (interface{}, error) {
gfs.mu.Lock()
defer gfs.mu.Unlock()
f, err := os.Open(getFile(id))
if err != nil {
return data, err
Expand Down

1 comment on commit 3dcf32a

@tranvictor
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #40

Please sign in to comment.