Skip to content

Commit

Permalink
Merge pull request #4 from annismckenzie/fix_potential_data_race
Browse files Browse the repository at this point in the history
Fix potential data race in query iterator
  • Loading branch information
kristoiv committed Jan 19, 2016
2 parents 24e66bc + 766911d commit 50cb774
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,18 @@ func (i *Iterator) Next() interface{} {

func (i *Iterator) Range() <-chan interface{} {
rangeChan := make(chan interface{})
i.done = make(chan bool)
done := make(chan bool)
i.done = done
go func() {
for {
next := i.Next()
if next == nil { // We clean up when we're done
close(rangeChan)
close(i.done)
i.done = nil
return
}
select {
case <-i.done: // We clean up when we're done
case <-done: // We clean up when we're done
close(rangeChan)
close(i.done)
i.done = nil
return
case rangeChan <- next:
}
Expand All @@ -87,7 +84,8 @@ func (i *Iterator) Range() <-chan interface{} {

func (i *Iterator) Close() error {
if done := i.done; done != nil {
done <- true
close(i.done)
i.done = nil
}
return i.iter.Close()
}
Expand Down

0 comments on commit 50cb774

Please sign in to comment.