Skip to content
This repository has been archived by the owner on Aug 9, 2018. It is now read-only.

Commit

Permalink
Fix race in iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
mildred committed Feb 10, 2016
1 parent 1dfacbd commit 69102e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test
test:
go test ./...
go test -race -cpu 5 ./...

22 changes: 11 additions & 11 deletions stream/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import (
"math/big"
)

type NodeIterator struct {
*ReadItem
LastError error
needFeedback bool
items chan *ReadItem
feedback chan error
feedbackClosed bool
}

type ReadItem struct {
Path []interface{}
TokenType ReaderToken
Expand Down Expand Up @@ -152,6 +143,15 @@ func (s *ReadItem) ToFloat() (res float64, ok bool) {
return res, ok
}

type NodeIterator struct {
*ReadItem
LastError error
needFeedback bool
items chan *ReadItem
feedback chan error
feedbackClosed bool
}

// Read from a NodeReader using a channel of ReadItem.
func Iterate(r NodeReader, res_error *error) *NodeIterator {
it := make(chan *ReadItem)
Expand All @@ -160,12 +160,10 @@ func Iterate(r NodeReader, res_error *error) *NodeIterator {

go func() {
err := r.Read(func(path []interface{}, tokenType ReaderToken, value interface{}) error {
res.needFeedback = true
item := &ReadItem{path, tokenType, value}
it <- item
return <-feedback
})
res.needFeedback = false
close(it)
if err != nil && res_error != nil {
res.LastError = err
Expand All @@ -182,13 +180,15 @@ func (s *NodeIterator) Iter() bool {

item := <-s.items
if item == nil {
s.needFeedback = false
if !s.feedbackClosed {
close(s.feedback)
s.feedbackClosed = true
}
return false
}

s.needFeedback = true
s.ReadItem = item
return true
}
Expand Down

0 comments on commit 69102e4

Please sign in to comment.