Skip to content

Commit

Permalink
fix: race conditions in base_index and eventlogstore
Browse files Browse the repository at this point in the history
Signed-off-by: phanquy <[email protected]>
  • Loading branch information
phanhuynhquy committed Oct 9, 2020
1 parent 27b0d3d commit 9a5aef2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 7 additions & 0 deletions stores/basestore/base_index.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package basestore

import (
"sync"

ipfslog "berty.tech/go-ipfs-log"

"berty.tech/go-orbit-db/iface"
)

type baseIndex struct {
mu sync.Mutex
id []byte
index []ipfslog.Entry
}

func (b *baseIndex) Get(_ string) interface{} {
b.mu.Lock()
defer b.mu.Unlock()
return b.index
}

func (b *baseIndex) UpdateIndex(log ipfslog.Log, entries []ipfslog.Entry) error {
b.mu.Lock()
defer b.mu.Unlock()
b.index = log.Values().Slice()
return nil
}
Expand Down
10 changes: 6 additions & 4 deletions stores/eventlogstore/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ func (o *orbitDBEventLogStore) Get(ctx context.Context, cid cid.Cid) (operation.
}()

select {
case value := <-stream:
case value, ok := <-stream:
cancel()
return value, nil
if ok {
return value, nil
}
return nil, errors.New("channel read failed")

case err := <-errChan:
return nil, err
Expand All @@ -78,6 +81,7 @@ func (o *orbitDBEventLogStore) Get(ctx context.Context, cid cid.Cid) (operation.
}

func (o *orbitDBEventLogStore) Stream(ctx context.Context, resultChan chan operation.Operation, options *iface.StreamOptions) error {
defer close(resultChan)
messages, err := o.query(options)
if err != nil {
return errors.Wrap(err, "unable to fetch query results")
Expand All @@ -92,8 +96,6 @@ func (o *orbitDBEventLogStore) Stream(ctx context.Context, resultChan chan opera
resultChan <- op
}

close(resultChan)

return nil
}

Expand Down

0 comments on commit 9a5aef2

Please sign in to comment.